mirror of
https://github.com/outline/outline.git
synced 2026-12-05 18:55:10 +03:00
fix: presenters/unfurls
This commit is contained in:
1 parent
2e3b340a1d
commit
8086cfd6ef
9 files changed
+173
-142
No files matched your search
@@ -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;
|
||||
+1
-56
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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<UnfurlType.Document> {
|
||||
return {
|
||||
url: document.url,
|
||||
type: UnfurlType.Document,
|
||||
title: document.titleWithDefault,
|
||||
description: presentLastActivityInfoFor(document, viewer),
|
||||
meta: {
|
||||
id: document.id,
|
||||
summary: document.getSummary(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default presentDocument;
|
||||
@@ -0,0 +1,4 @@
|
||||
import presentDocument from "./document";
|
||||
import presentMention from "./mention";
|
||||
|
||||
export { presentDocument, presentMention };
|
||||
@@ -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<UnfurlType.Mention> {
|
||||
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;
|
||||
@@ -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);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -209,5 +209,19 @@ export const NotificationEventDefaults = {
|
||||
[NotificationEventType.ExportCompleted]: true,
|
||||
};
|
||||
|
||||
export enum UnfurlType {
|
||||
Mention = "mention",
|
||||
Document = "document",
|
||||
}
|
||||
|
||||
export type Unfurl<T = unknown> = {
|
||||
url?: string;
|
||||
type: T;
|
||||
title: string;
|
||||
description: string;
|
||||
thumbnailUrl?: string | null;
|
||||
meta: Record<string, string>;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type ProsemirrorData = Record<string, any>;
|
||||
Reference in new issue
Block a user