Files
outline/server/presenters/unfurl.ts
T
Tom MoorandClaude f680d5dec9 feat: Display links, documents and collections as preview cards (#14511)
* feat: Display links as preview cards

Adds a display attribute to mentions so a link can render either inline
or as a card with image, title and description from its unfurl. Pasting
a URL offers a "Card" option, and selecting a link mention shows a
toolbar to switch between the two displays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFgeTUqKhSTriUHfG3z

* feat: Arrow key selection, full width and Return for link cards

Cards fill the document width, hide the blank line below them, are selected
by the up and down arrow keys like images, and Return on a selected card
adds a paragraph below it.

* feat: Display document and collection mentions as cards

Document and collection mentions can be switched to a card from the
selection toolbar. Document cards show the summary, collection and the
first image larger than 100x100. Collection cards show the first line of
the description. On public shares the preview image is signed, as the
share page already signs the attachments of that document. Card images
are hidden until they load and removed if they fail.

* feat: Keep mention cards through Markdown

The card display is written as the link title, so URLs are not changed.
Document and collection cards are written as @ links to their id and
parsed back into mentions.

* refactor: Move mention cards to their own file

Document cards show the collection after the title instead of below it.

* fix: Address review feedback on mention cards

- Include the share id in the document card unfurl URL for root shares.
- Return on a card in a heading adds a paragraph.
- URL cards keep their URL in Markdown, and the server does not narrow
  their type.
- Unfurl results apply to the current node, not the node at first render.
- Collection card descriptions stop at the first line break.

* refactor: Reuse existing helpers for mention cards

- Sign the preview image with TextHelper.attachmentsToSignedUrls.
- Check document and collection ids with isUUID.
- Return lines from getFirstText without changing an input array.

* fix: Preserve JSON output for inline mentions

* refactor: Centralize mention selection classes

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-08 08:11:31 -05:00

257 lines
7.7 KiB
TypeScript

import { differenceInMinutes, formatDistanceToNowStrict } from "date-fns";
import { t } from "i18next";
import type { UnfurlResponse } from "@shared/types";
import { UnfurlResourceType } from "@shared/types";
import { dateLocale } from "@shared/utils/date";
import { Hour } from "@shared/utils/time";
import type { Document, User, Group } from "@server/models";
import { View } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { TextHelper } from "@server/models/helpers/TextHelper";
import { opts } from "@server/utils/i18n";
// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- heterogeneous payload from internal callers and third-party unfurl plugins.
type UnfurlData = Record<string, any>;
interface UnfurlOptions {
/** Whether the mentioned user's email may be included in the response. */
includeEmail: boolean;
/** Whether the mentioned user's viewing activity may be included in the response. */
includeLastViewed: boolean;
}
async function presentUnfurl(data: UnfurlData, options?: UnfurlOptions) {
switch (data.type) {
case UnfurlResourceType.Mention:
return presentMention(data, options);
case UnfurlResourceType.Group:
return presentGroup(data);
case UnfurlResourceType.Document:
return presentDocument(data);
case UnfurlResourceType.PR:
return presentPR(data);
case UnfurlResourceType.Issue:
return presentIssue(data);
case UnfurlResourceType.Project:
return presentProject(data);
default:
return presentURL(data);
}
}
// The data will have been transformed by the unfurl plugin, fields are picked
// individually so that additional metadata is never exposed in the response.
const presentURL = (
data: UnfurlData
): UnfurlResponse[UnfurlResourceType.URL] => ({
type: UnfurlResourceType.URL,
url: data.url,
title: data.title,
description: data.description,
color: data.color,
thumbnailUrl: data.thumbnailUrl,
faviconUrl: data.faviconUrl,
});
const presentMention = async (
data: UnfurlData,
options?: UnfurlOptions
): Promise<UnfurlResponse[UnfurlResourceType.Mention]> => {
const user: User = data.user;
const document: Document = data.document;
const lastOnlineInfo = presentLastOnlineInfoFor(user);
const lastViewedInfo =
options && options.includeLastViewed
? await presentLastViewedInfoFor(user, document)
: undefined;
return {
type: UnfurlResourceType.Mention,
name: user.name,
email: options && options.includeEmail ? user.email : null,
avatarUrl: user.avatarUrl,
color: user.color,
lastActive: lastViewedInfo
? `${lastOnlineInfo} • ${lastViewedInfo}`
: lastOnlineInfo,
};
};
const presentGroup = async (
data: UnfurlData
): Promise<UnfurlResponse[UnfurlResourceType.Group]> => {
const group: Group = data.group;
const memberCount = await group.memberCount;
return {
type: UnfurlResourceType.Group,
name: group.name,
description: group.description,
memberCount,
users: (data.users as User[]).map((user) => ({
id: user.id,
name: user.name,
avatarUrl: user.avatarUrl,
color: user.color,
})),
};
};
const presentDocument = async (
data: UnfurlData
): Promise<UnfurlResponse[UnfurlResourceType.Document]> => {
const document: Document = data.document;
const viewer: User | undefined = data.viewer;
const url: string | undefined = data.url;
const anchor: string | undefined = data.anchor;
// When the URL targets a specific heading, preview the content of that
// section rather than the top of the document.
const sectionSummary = anchor
? DocumentHelper.getAnchorContent(document, anchor)
: undefined;
return {
url: url ?? `${document.url}${anchor ?? ""}`,
type: UnfurlResourceType.Document,
id: document.id,
title: document.displayTitle,
summary: sectionSummary || document.getSummary(),
thumbnailUrl: await presentThumbnailUrl(document, !!data.signedUrls),
lastActivityByViewer: viewer
? presentLastActivityInfoFor(document, viewer)
: undefined,
};
};
const presentPR = (data: UnfurlData): UnfurlResponse[UnfurlResourceType.PR] =>
data as UnfurlResponse[UnfurlResourceType.PR]; // this would have been transformed by the unfurl plugin.
const presentIssue = (
data: UnfurlData
): UnfurlResponse[UnfurlResourceType.Issue] =>
data as UnfurlResponse[UnfurlResourceType.Issue]; // this would have been transformed by the unfurl plugin.
const presentProject = (
data: UnfurlData
): UnfurlResponse[UnfurlResourceType.Project] =>
data as UnfurlResponse[UnfurlResourceType.Project]; // this would have been transformed by the unfurl plugin.
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 {{ timeAgo }}", {
timeAgo: formatDistanceToNowStrict(user.lastActiveAt, {
addSuffix: true,
locale,
}),
...opts(user),
});
}
return info;
};
const presentLastViewedInfoFor = async (user: User, document: Document) => {
const lastView = await View.findOne({
where: {
userId: user.id,
documentId: document.id,
},
order: [["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 {{ timeAgo }}", {
timeAgo: formatDistanceToNowStrict(lastViewedAt, {
addSuffix: true,
locale,
}),
...opts(user),
});
}
return info;
};
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 {{ timeAgo }}", {
timeAgo: formatDistanceToNowStrict(document.updatedAt, {
addSuffix: true,
locale,
}),
...opts(viewer),
});
} else {
info = t("{{ user }} updated {{ timeAgo }}", {
user: document.updatedBy.name,
timeAgo: formatDistanceToNowStrict(document.updatedAt, {
addSuffix: true,
locale,
}),
...opts(viewer),
});
}
} else {
const lastCreatedByViewer = document.createdById === viewer.id;
if (lastCreatedByViewer) {
info = t("You created {{ timeAgo }}", {
timeAgo: formatDistanceToNowStrict(document.createdAt, {
addSuffix: true,
locale,
}),
...opts(viewer),
});
} else {
info = t("{{ user }} created {{ timeAgo }}", {
user: document.createdBy.name,
timeAgo: formatDistanceToNowStrict(document.createdAt, {
addSuffix: true,
locale,
}),
...opts(viewer),
});
}
}
return info;
};
/**
* Returns the preview image of a document. Signed URLs are used for viewers of
* a public share, who cannot load attachments through the redirect URL.
*/
const presentThumbnailUrl = async (
document: Document,
signedUrls: boolean
): Promise<string | undefined> => {
const src = DocumentHelper.getPreviewImage(document);
if (!src || !signedUrls) {
return src;
}
return TextHelper.attachmentsToSignedUrls(src, document.teamId, Hour.seconds);
};
export default presentUnfurl;