From 46e3acf9ed1a3d84f4e1e405cd29cd2b3f002af3 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 13 Aug 2026 20:50:02 -0400 Subject: [PATCH] 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) --- server/presenters/unfurl.ts | 23 +++++++++----- server/routes/api/urls/urls.test.ts | 47 +++++++++++++++++++++++++++++ server/routes/api/urls/urls.ts | 6 +++- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/server/presenters/unfurl.ts b/server/presenters/unfurl.ts index 1b437cc08c..3633fb4cca 100644 --- a/server/presenters/unfurl.ts +++ b/server/presenters/unfurl.ts @@ -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; -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 => { 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, }; }; diff --git a/server/routes/api/urls/urls.test.ts b/server/routes/api/urls/urls.test.ts index 16d39ff18d..f9bed3d23d 100644 --- a/server/routes/api/urls/urls.test.ts +++ b/server/routes/api/urls/urls.test.ts @@ -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: { diff --git a/server/routes/api/urls/urls.ts b/server/routes/api/urls/urls.ts index 10295c0b2e..740a754e73 100644 --- a/server/routes/api/urls/urls.ts +++ b/server/routes/api/urls/urls.ts @@ -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([