fix: Align mention unfurl with document insights settings (#13420)

The view activity included in a mention preview was not subject to the
document's insights setting, nor to the ability check that guards the same
data on the other endpoints that expose it. Both are now applied, and the
preview falls back to online status alone when they do not pass.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom MoorandClaude Opus 5 authored and GitHub committed 2026-08-13 20:50:02 -04:00
1 parent 15daf2e8a7
commit 46e3acf9ed
3 files changed
+68 -8

No files matched your search

+16 -7
View File
@@ -11,10 +11,14 @@ import { opts } from "@server/utils/i18n";
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- heterogeneous payload from internal callers and third-party unfurl plugins.
type UnfurlData = Record<string, any>;
async function presentUnfurl(
data: UnfurlData,
options?: { includeEmail: boolean }
) {
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);
@@ -49,13 +53,16 @@ const presentURL = (
const presentMention = async (
data: UnfurlData,
options?: { includeEmail: boolean }
options?: UnfurlOptions
): Promise<UnfurlResponse[UnfurlResourceType.Mention]> => {
const user: User = data.user;
const document: Document = data.document;
const lastOnlineInfo = presentLastOnlineInfoFor(user);
const lastViewedInfo = await presentLastViewedInfoFor(user, document);
const lastViewedInfo =
options && options.includeLastViewed
? await presentLastViewedInfoFor(user, document)
: undefined;
return {
type: UnfurlResourceType.Mention,
@@ -63,7 +70,9 @@ const presentMention = async (
email: options && options.includeEmail ? user.email : null,
avatarUrl: user.avatarUrl,
color: user.color,
lastActive: `${lastOnlineInfo} • ${lastViewedInfo}`,
lastActive: lastViewedInfo
? `${lastOnlineInfo} • ${lastViewedInfo}`
: lastOnlineInfo,
};
};
+47
View File
@@ -1,8 +1,10 @@
import type { Mock } from "vitest";
import { randomString } from "@shared/random";
import { UnfurlResourceType } from "@shared/types";
import { createContext } from "@server/context";
import env from "@server/env";
import type { User } from "@server/models";
import { View } from "@server/models";
import {
buildCollection,
buildDocument,
@@ -155,6 +157,51 @@ describe("#urls.unfurl", () => {
expect(body.name).toEqual(mentionedUser.name);
});
it("should include the mentioned user's viewing activity when insights are enabled", async () => {
const mentionedUser = await buildUser({ teamId: user.teamId });
const document = await buildDocument({
teamId: user.teamId,
insightsEnabled: true,
});
await View.incrementOrCreate(createContext({ user: mentionedUser }), {
documentId: document.id,
userId: mentionedUser.id,
});
const res = await server.post("/api/urls.unfurl", user, {
body: {
url: `mention://2767ba0e-ac5c-4533-b9cf-4f5fc456600e/user/${mentionedUser.id}`,
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.lastActive).toContain("Viewed");
});
it("should not include the mentioned user's viewing activity when insights are disabled", async () => {
const mentionedUser = await buildUser({ teamId: user.teamId });
const document = await buildDocument({
teamId: user.teamId,
insightsEnabled: false,
});
await View.incrementOrCreate(createContext({ user: mentionedUser }), {
documentId: document.id,
userId: mentionedUser.id,
});
const res = await server.post("/api/urls.unfurl", user, {
body: {
url: `mention://2767ba0e-ac5c-4533-b9cf-4f5fc456600e/user/${mentionedUser.id}`,
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.lastActive).not.toContain("Viewed");
expect(body.lastActive).not.toContain("Never viewed");
});
it("should return 204 when internal document url points to non-existent document", async () => {
const res = await server.post("/api/urls.unfurl", user, {
body: {
+5 -1
View File
@@ -122,7 +122,11 @@ router.post(
user,
document,
},
{ includeEmail: !!can(actor, "readEmail", user) }
{
includeEmail: !!can(actor, "readEmail", user),
includeLastViewed:
document.insightsEnabled && !!can(actor, "listViews", document),
}
);
} else if (mentionType === MentionType.Group) {
const [group, document] = await Promise.all([