From 8086cfd6ef657c17158334b9b4e4752b7118d733 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Thu, 20 Jul 2023 23:38:16 +0530 Subject: [PATCH] fix: presenters/unfurls --- server/models/Document.ts | 54 ----------- server/models/User.ts | 57 +----------- server/presenters/unfurls/common.ts | 103 +++++++++++++++++++++ server/presenters/unfurls/document.ts | 21 +++++ server/presenters/unfurls/index.ts | 4 + server/presenters/unfurls/mention.ts | 23 +++++ server/routes/api/urls/urls.ts | 31 +------ shared/i18n/locales/en_US/translation.json | 8 +- shared/types.ts | 14 +++ 9 files changed, 173 insertions(+), 142 deletions(-) create mode 100644 server/presenters/unfurls/common.ts create mode 100644 server/presenters/unfurls/document.ts create mode 100644 server/presenters/unfurls/index.ts create mode 100644 server/presenters/unfurls/mention.ts diff --git a/server/models/Document.ts b/server/models/Document.ts index 6e1e2a5acc..db2898cd15 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -1,5 +1,3 @@ -import { formatDistanceToNowStrict } from "date-fns"; -import { t } from "i18next"; import { compact, uniq } from "lodash"; import randomstring from "randomstring"; import type { SaveOptions } from "sequelize"; @@ -33,12 +31,10 @@ 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"; @@ -797,56 +793,6 @@ 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; diff --git a/server/models/User.ts b/server/models/User.ts index 1e4017574b..719cd03c35 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -1,15 +1,7 @@ import crypto from "crypto"; -import { - addHours, - addMinutes, - differenceInMinutes, - formatDistanceToNowStrict, - subMinutes, -} from "date-fns"; -import { t } from "i18next"; +import { addHours, addMinutes, subMinutes } from "date-fns"; import JWT from "jsonwebtoken"; import { Context } from "koa"; -import { head, orderBy } from "lodash"; import { Transaction, QueryTypes, SaveOptions, Op } from "sequelize"; import { Table, @@ -41,10 +33,8 @@ 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"; @@ -52,7 +42,6 @@ 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"; @@ -426,50 +415,6 @@ 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; diff --git a/server/presenters/unfurls/common.ts b/server/presenters/unfurls/common.ts new file mode 100644 index 0000000000..e77cc68caa --- /dev/null +++ b/server/presenters/unfurls/common.ts @@ -0,0 +1,103 @@ +import { differenceInMinutes, formatDistanceToNowStrict } from "date-fns"; +import { t } from "i18next"; +import { head, orderBy } from "lodash"; +import { dateLocale } from "@shared/utils/date"; +import { Document, User } from "@server/models"; +import { opts } from "@server/utils/i18n"; + +export const presentLastOnlineInfoFor = (user: User) => { + const locale = dateLocale(user.language); + + let info: string; + if (!user.lastActiveAt) { + info = t("Never logged in", { ...opts(user) }); + } else if (differenceInMinutes(new Date(), user.lastActiveAt) < 5) { + info = t("Online now", { ...opts(user) }); + } else { + info = t("Online {{ someTimeAgo }}", { + someTimeAgo: formatDistanceToNowStrict(user.lastActiveAt, { + addSuffix: true, + locale, + }), + ...opts(user), + }); + } + + return info; +}; + +export const presentLastViewedInfoFor = (user: User, document: Document) => { + const lastView = head(orderBy(document.views, ["updatedAt"], ["desc"])); + const lastViewedAt = lastView ? lastView.updatedAt : undefined; + const locale = dateLocale(user.language); + + let info: string; + if (!lastViewedAt) { + info = t("Never viewed", { ...opts(user) }); + } else if (differenceInMinutes(new Date(), lastViewedAt) < 5) { + info = t("Viewed just now", { ...opts(user) }); + } else { + info = t("Viewed {{ someTimeAgo }}", { + someTimeAgo: formatDistanceToNowStrict(lastViewedAt, { + addSuffix: true, + locale, + }), + ...opts(user), + }); + } + + return info; +}; + +export const presentLastActivityInfoFor = ( + document: Document, + viewer: User +) => { + const locale = dateLocale(viewer.language); + const wasUpdated = document.createdAt !== document.updatedAt; + + let info: string; + if (wasUpdated) { + const lastUpdatedByViewer = document.updatedBy.id === viewer.id; + if (lastUpdatedByViewer) { + info = t("You updated {{ someTimeAgo }}", { + someTimeAgo: formatDistanceToNowStrict(document.updatedAt, { + addSuffix: true, + locale, + }), + ...opts(viewer), + }); + } else { + info = t("{{ user }} updated {{ someTimeAgo }}", { + user: document.updatedBy.name, + someTimeAgo: formatDistanceToNowStrict(document.updatedAt, { + addSuffix: true, + locale, + }), + ...opts(viewer), + }); + } + } else { + const lastCreatedByViewer = document.createdById === viewer.id; + if (lastCreatedByViewer) { + info = t("You created {{ someTimeAgo }}", { + someTimeAgo: formatDistanceToNowStrict(document.createdAt, { + addSuffix: true, + locale, + }), + ...opts(viewer), + }); + } else { + info = t("{{ user }} created {{ someTimeAgo }}", { + user: document.createdBy.name, + someTimeAgo: formatDistanceToNowStrict(document.createdAt, { + addSuffix: true, + locale, + }), + ...opts(viewer), + }); + } + } + + return info; +}; diff --git a/server/presenters/unfurls/document.ts b/server/presenters/unfurls/document.ts new file mode 100644 index 0000000000..5cdbc264b6 --- /dev/null +++ b/server/presenters/unfurls/document.ts @@ -0,0 +1,21 @@ +import { Unfurl, UnfurlType } from "@shared/types"; +import { User, Document } from "@server/models"; +import { presentLastActivityInfoFor } from "./common"; + +function presentDocument( + document: Document, + viewer: User +): Unfurl { + return { + url: document.url, + type: UnfurlType.Document, + title: document.titleWithDefault, + description: presentLastActivityInfoFor(document, viewer), + meta: { + id: document.id, + summary: document.getSummary(), + }, + }; +} + +export default presentDocument; diff --git a/server/presenters/unfurls/index.ts b/server/presenters/unfurls/index.ts new file mode 100644 index 0000000000..dbf49bb0cf --- /dev/null +++ b/server/presenters/unfurls/index.ts @@ -0,0 +1,4 @@ +import presentDocument from "./document"; +import presentMention from "./mention"; + +export { presentDocument, presentMention }; diff --git a/server/presenters/unfurls/mention.ts b/server/presenters/unfurls/mention.ts new file mode 100644 index 0000000000..3733e8a486 --- /dev/null +++ b/server/presenters/unfurls/mention.ts @@ -0,0 +1,23 @@ +import { Unfurl, UnfurlType } from "@shared/types"; +import { Document, User } from "@server/models"; +import { presentLastOnlineInfoFor, presentLastViewedInfoFor } from "./common"; + +function presentMention( + user: User, + document: Document +): Unfurl { + return { + type: UnfurlType.Mention, + title: user.name, + description: `${presentLastOnlineInfoFor( + user + )} • ${presentLastViewedInfoFor(user, document)}`, + thumbnailUrl: user.avatarUrl, + meta: { + id: user.id, + color: user.color, + }, + }; +} + +export default presentMention; diff --git a/server/routes/api/urls/urls.ts b/server/routes/api/urls/urls.ts index 2c27de9342..f84c915db4 100644 --- a/server/routes/api/urls/urls.ts +++ b/server/routes/api/urls/urls.ts @@ -7,6 +7,7 @@ import { transaction } from "@server/middlewares/transaction"; import validate from "@server/middlewares/validate"; import { Document, User } from "@server/models"; import { authorize } from "@server/policies"; +import { presentDocument, presentMention } from "@server/presenters/unfurls"; import { APIContext } from "@server/types"; import * as T from "./schema"; @@ -41,22 +42,7 @@ router.post( authorize(user, "read", mentionedUser); authorize(user, "read", document); - const lastOnlineInfo = mentionedUser.lastOnlineInfo(); - const lastViewedInfo = mentionedUser.lastViewedInfoFor(document); - - const description = `${lastOnlineInfo} • ${lastViewedInfo}`; - - ctx.body = { - url, - type: "mention", - title: mentionedUser.name, - description, - thumbnailUrl: mentionedUser.avatarUrl, - meta: { - id: mentionedUser.id, - color: mentionedUser.color, - }, - }; + ctx.body = presentMention(mentionedUser, document); return; } @@ -68,18 +54,7 @@ router.post( } authorize(user, "read", document); - const description = document.lastActivityInfo({ viewer: user }); - - ctx.body = { - url: document.url, - type: "document", - title: document.titleWithDefault, - description, - meta: { - id: document.id, - summary: document.getSummary(), - }, - }; + ctx.body = presentDocument(document, user); } ); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 6a403cabad..95871c7418 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -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", "Viewed {{ someTimeAgo }}": "Viewed {{ someTimeAgo }}", + "You updated {{ someTimeAgo }}": "You updated {{ someTimeAgo }}", + "{{ user }} updated {{ someTimeAgo }}": "{{ user }} updated {{ someTimeAgo }}", + "You created {{ someTimeAgo }}": "You created {{ someTimeAgo }}", + "{{ user }} created {{ someTimeAgo }}": "{{ user }} created {{ someTimeAgo }}", "Uploading": "Uploading" } diff --git a/shared/types.ts b/shared/types.ts index 9c2e8f9a58..f61807898a 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -209,5 +209,19 @@ export const NotificationEventDefaults = { [NotificationEventType.ExportCompleted]: true, }; +export enum UnfurlType { + Mention = "mention", + Document = "document", +} + +export type Unfurl = { + url?: string; + type: T; + title: string; + description: string; + thumbnailUrl?: string | null; + meta: Record; +}; + // eslint-disable-next-line @typescript-eslint/no-explicit-any export type ProsemirrorData = Record;