mirror of
https://github.com/outline/outline.git
synced 2026-11-24 23:00:18 +03:00
fix: extract description part as utils in models
This commit is contained in:
1 parent
e6f1a06e07
commit
2e3b340a1d
4 files changed
+123
-94
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { formatDistanceToNowStrict } from "date-fns";
|
||||
import { t } from "i18next";
|
||||
import { compact, uniq } from "lodash";
|
||||
import randomstring from "randomstring";
|
||||
import type { SaveOptions } from "sequelize";
|
||||
@@ -31,10 +33,12 @@ import {
|
||||
} from "sequelize-typescript";
|
||||
import isUUID from "validator/lib/isUUID";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { dateLocale } from "@shared/utils/date";
|
||||
import getTasks from "@shared/utils/getTasks";
|
||||
import parseTitle from "@shared/utils/parseTitle";
|
||||
import { SLUG_URL_REGEX } from "@shared/utils/urlHelpers";
|
||||
import { DocumentValidation } from "@shared/validations";
|
||||
import { opts } from "@server/utils/i18n";
|
||||
import slugify from "@server/utils/slugify";
|
||||
import Backlink from "./Backlink";
|
||||
import Collection from "./Collection";
|
||||
@@ -793,6 +797,56 @@ class Document extends ParanoidModel {
|
||||
children,
|
||||
};
|
||||
};
|
||||
|
||||
lastActivityInfo = ({ viewer }: { viewer: User }) => {
|
||||
const locale = dateLocale(viewer.language);
|
||||
const wasUpdated = this.createdAt !== this.updatedAt;
|
||||
|
||||
let info: string;
|
||||
if (wasUpdated) {
|
||||
const lastUpdatedByViewer = this.updatedBy.id === viewer.id;
|
||||
if (lastUpdatedByViewer) {
|
||||
info = t("You updated {{ someTimeAgo }}", {
|
||||
someTimeAgo: formatDistanceToNowStrict(this.updatedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(viewer),
|
||||
});
|
||||
} else {
|
||||
info = t("{{ user }} updated {{ someTimeAgo }}", {
|
||||
user: this.updatedBy.name,
|
||||
someTimeAgo: formatDistanceToNowStrict(this.updatedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(viewer),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const lastCreatedByViewer = this.createdById === viewer.id;
|
||||
if (lastCreatedByViewer) {
|
||||
info = t("You created {{ someTimeAgo }}", {
|
||||
someTimeAgo: formatDistanceToNowStrict(this.createdAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(viewer),
|
||||
});
|
||||
} else {
|
||||
info = t("{{ user }} created {{ someTimeAgo }}", {
|
||||
user: this.createdBy.name,
|
||||
someTimeAgo: formatDistanceToNowStrict(this.createdAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(viewer),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return info;
|
||||
};
|
||||
}
|
||||
|
||||
export default Document;
|
||||
+56
-1
@@ -1,7 +1,15 @@
|
||||
import crypto from "crypto";
|
||||
import { addHours, addMinutes, subMinutes } from "date-fns";
|
||||
import {
|
||||
addHours,
|
||||
addMinutes,
|
||||
differenceInMinutes,
|
||||
formatDistanceToNowStrict,
|
||||
subMinutes,
|
||||
} from "date-fns";
|
||||
import { t } from "i18next";
|
||||
import JWT from "jsonwebtoken";
|
||||
import { Context } from "koa";
|
||||
import { head, orderBy } from "lodash";
|
||||
import { Transaction, QueryTypes, SaveOptions, Op } from "sequelize";
|
||||
import {
|
||||
Table,
|
||||
@@ -33,8 +41,10 @@ import {
|
||||
NotificationEventDefaults,
|
||||
} from "@shared/types";
|
||||
import { stringToColor } from "@shared/utils/color";
|
||||
import { dateLocale } from "@shared/utils/date";
|
||||
import env from "@server/env";
|
||||
import DeleteAttachmentTask from "@server/queues/tasks/DeleteAttachmentTask";
|
||||
import { opts } from "@server/utils/i18n";
|
||||
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
|
||||
import { ValidationError } from "../errors";
|
||||
import ApiKey from "./ApiKey";
|
||||
@@ -42,6 +52,7 @@ import Attachment from "./Attachment";
|
||||
import AuthenticationProvider from "./AuthenticationProvider";
|
||||
import Collection from "./Collection";
|
||||
import CollectionUser from "./CollectionUser";
|
||||
import Document from "./Document";
|
||||
import Star from "./Star";
|
||||
import Team from "./Team";
|
||||
import UserAuthentication from "./UserAuthentication";
|
||||
@@ -415,6 +426,50 @@ class User extends ParanoidModel {
|
||||
});
|
||||
};
|
||||
|
||||
lastOnlineInfo = () => {
|
||||
const locale = dateLocale(this.language);
|
||||
|
||||
let info: string;
|
||||
if (!this.lastActiveAt) {
|
||||
info = t("Never logged in", { ...opts(this) });
|
||||
} else if (differenceInMinutes(new Date(), this.lastActiveAt) < 5) {
|
||||
info = t("Online now", { ...opts(this) });
|
||||
} else {
|
||||
info = t("Online {{ someTimeAgo }}", {
|
||||
someTimeAgo: formatDistanceToNowStrict(this.lastActiveAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(this),
|
||||
});
|
||||
}
|
||||
|
||||
return info;
|
||||
};
|
||||
|
||||
lastViewedInfoFor = (document: Document) => {
|
||||
const lastView = head(orderBy(document.views, ["updatedAt"], ["desc"]));
|
||||
const lastViewedAt = lastView ? lastView.updatedAt : undefined;
|
||||
const locale = dateLocale(this.language);
|
||||
|
||||
let info: string;
|
||||
if (!lastViewedAt) {
|
||||
info = t("Never viewed", { ...opts(this) });
|
||||
} else if (differenceInMinutes(new Date(), lastViewedAt) < 5) {
|
||||
info = t("Viewed just now", { ...opts(this) });
|
||||
} else {
|
||||
info = t("Viewed {{ someTimeAgo }}", {
|
||||
someTimeAgo: formatDistanceToNowStrict(lastViewedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(this),
|
||||
});
|
||||
}
|
||||
|
||||
return info;
|
||||
};
|
||||
|
||||
updateSignedIn = (ip: string) => {
|
||||
const now = new Date();
|
||||
this.lastActiveAt = now;
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import { differenceInMinutes, formatDistanceToNowStrict } from "date-fns";
|
||||
import { t } from "i18next";
|
||||
import Router from "koa-router";
|
||||
import { head, orderBy } from "lodash";
|
||||
import { dateLocale } from "@shared/utils/date";
|
||||
import parseDocumentSlug from "@shared/utils/parseDocumentSlug";
|
||||
import parseMentionUrl from "@shared/utils/parseMentionUrl";
|
||||
import { NotFoundError } from "@server/errors";
|
||||
@@ -12,7 +8,6 @@ import validate from "@server/middlewares/validate";
|
||||
import { Document, User } from "@server/models";
|
||||
import { authorize } from "@server/policies";
|
||||
import { APIContext } from "@server/types";
|
||||
import { opts } from "@server/utils/i18n";
|
||||
import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
@@ -27,7 +22,6 @@ router.post(
|
||||
const { user } = ctx.state.auth;
|
||||
const { transaction } = ctx.state;
|
||||
const urlObj = new URL(url);
|
||||
const locale = dateLocale(user.language);
|
||||
if (urlObj.protocol === "mention:") {
|
||||
const { modelId: userId } = parseMentionUrl(url);
|
||||
|
||||
@@ -47,46 +41,10 @@ router.post(
|
||||
authorize(user, "read", mentionedUser);
|
||||
authorize(user, "read", document);
|
||||
|
||||
const lastView = head(orderBy(document.views, ["updatedAt"], ["desc"]));
|
||||
const lastViewedAt = lastView ? lastView.updatedAt : undefined;
|
||||
const lastActiveAt = mentionedUser.lastActiveAt;
|
||||
let description;
|
||||
if (lastViewedAt && differenceInMinutes(new Date(), lastViewedAt) < 5) {
|
||||
description = t("Viewed just now", { ...opts(user) });
|
||||
} else if (
|
||||
lastActiveAt &&
|
||||
differenceInMinutes(new Date(), lastActiveAt) < 5
|
||||
) {
|
||||
description = lastViewedAt
|
||||
? t("Online now • Viewed {{ lastViewedAt }}", {
|
||||
lastViewedAt: formatDistanceToNowStrict(lastViewedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
})
|
||||
: t("Online now • Never viewed", { ...opts(user) });
|
||||
} else {
|
||||
description = lastViewedAt
|
||||
? t("Online {{ lastActiveAt }} • Viewed {{ lastViewedAt }}", {
|
||||
lastActiveAt: formatDistanceToNowStrict(lastActiveAt!, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
lastViewedAt: formatDistanceToNowStrict(lastViewedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
})
|
||||
: t("Online {{ lastActiveAt }} • Never viewed", {
|
||||
lastActiveAt: formatDistanceToNowStrict(lastActiveAt!, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
});
|
||||
}
|
||||
const lastOnlineInfo = mentionedUser.lastOnlineInfo();
|
||||
const lastViewedInfo = mentionedUser.lastViewedInfoFor(document);
|
||||
|
||||
const description = `${lastOnlineInfo} • ${lastViewedInfo}`;
|
||||
|
||||
ctx.body = {
|
||||
url,
|
||||
@@ -110,45 +68,7 @@ router.post(
|
||||
}
|
||||
authorize(user, "read", document);
|
||||
|
||||
let description;
|
||||
|
||||
if (document.createdAt === document.updatedAt) {
|
||||
description =
|
||||
document.createdById === user.id
|
||||
? t("You created {{ createdAt }}", {
|
||||
createdAt: formatDistanceToNowStrict(document.createdAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
})
|
||||
: t("{{ username }} created {{ createdAt }}", {
|
||||
userName: document.createdBy.name,
|
||||
createdAt: formatDistanceToNowStrict(document.createdAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
});
|
||||
} else {
|
||||
description =
|
||||
document.updatedBy.id === user.id
|
||||
? t("You updated {{ updatedAt }}", {
|
||||
updatedAt: formatDistanceToNowStrict(document.updatedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
})
|
||||
: t("{{ username }} updated {{ updatedAt }}", {
|
||||
username: document.updatedBy.name,
|
||||
updatedAt: formatDistanceToNowStrict(document.updatedAt, {
|
||||
addSuffix: true,
|
||||
locale,
|
||||
}),
|
||||
...opts(user),
|
||||
});
|
||||
}
|
||||
const description = document.lastActivityInfo({ viewer: user });
|
||||
|
||||
ctx.body = {
|
||||
url: document.url,
|
||||
|
||||
@@ -899,14 +899,14 @@
|
||||
"Webhooks can be used to notify your application when events happen in {{appName}}. Events are sent as a https request with a JSON payload in near real-time.": "Webhooks can be used to notify your application when events happen in {{appName}}. Events are sent as a https request with a JSON payload in near real-time.",
|
||||
"Inactive": "Inactive",
|
||||
"Create a webhook": "Create a webhook",
|
||||
"You updated {{ someTimeAgo }}": "You updated {{ someTimeAgo }}",
|
||||
"{{ user }} updated {{ someTimeAgo }}": "{{ user }} updated {{ someTimeAgo }}",
|
||||
"You created {{ someTimeAgo }}": "You created {{ someTimeAgo }}",
|
||||
"{{ user }} created {{ someTimeAgo }}": "{{ user }} created {{ someTimeAgo }}",
|
||||
"Never logged in": "Never logged in",
|
||||
"Online now": "Online now",
|
||||
"Online {{ someTimeAgo }}": "Online {{ someTimeAgo }}",
|
||||
"Viewed just now": "Viewed just now",
|
||||
"Online now • Viewed {{ lastViewedAt }}": "Online now • Viewed {{ lastViewedAt }}",
|
||||
"Online now • Never viewed": "Online now • Never viewed",
|
||||
"Online {{ lastActiveAt }} • Viewed {{ lastViewedAt }}": "Online {{ lastActiveAt }} • Viewed {{ lastViewedAt }}",
|
||||
"Online {{ lastActiveAt }} • Never viewed": "Online {{ lastActiveAt }} • Never viewed",
|
||||
"You created {{ createdAt }}": "You created {{ createdAt }}",
|
||||
"{{ username }} created {{ createdAt }}": "{{ username }} created {{ createdAt }}",
|
||||
"You updated {{ updatedAt }}": "You updated {{ updatedAt }}",
|
||||
"{{ username }} updated {{ updatedAt }}": "{{ username }} updated {{ updatedAt }}",
|
||||
"Viewed {{ someTimeAgo }}": "Viewed {{ someTimeAgo }}",
|
||||
"Uploading": "Uploading"
|
||||
}
|
||||
Reference in new issue
Block a user