diff --git a/server/emails/templates/BaseEmail.tsx b/server/emails/templates/BaseEmail.tsx index 9433337363..b81aa5ced9 100644 --- a/server/emails/templates/BaseEmail.tsx +++ b/server/emails/templates/BaseEmail.tsx @@ -296,7 +296,12 @@ export default abstract class BaseEmail< return undefined; } - let content = ProsemirrorHelper.toHTML(node, { + // Process user mentions to ensure they are uptodate with database + const processedNode = ProsemirrorHelper.toProsemirror( + await ProsemirrorHelper.processMentions(node) + ); + + let content = ProsemirrorHelper.toHTML(processedNode, { centered: false, }); diff --git a/server/models/helpers/ProseMirrorHelper.test.ts b/server/models/helpers/ProseMirrorHelper.test.ts index e61a60b4f7..90c019351e 100644 --- a/server/models/helpers/ProseMirrorHelper.test.ts +++ b/server/models/helpers/ProseMirrorHelper.test.ts @@ -1,9 +1,187 @@ +import { faker } from "@faker-js/faker"; import { DeepPartial } from "utility-types"; import { MentionType, ProsemirrorData } from "@shared/types"; -import { buildProseMirrorDoc } from "@server/test/factories"; +import { buildProseMirrorDoc, buildUser } from "@server/test/factories"; import { MentionAttrs, ProsemirrorHelper } from "./ProsemirrorHelper"; describe("ProsemirrorHelper", () => { + describe("processMentions", () => { + it("should handle deleted users", async () => { + const user = await buildUser(); + const mentionAttrs: MentionAttrs = { + id: "9a17c1c8-d178-4350-9001-203a73070fcb", + type: MentionType.User, + label: "test.user", + actorId: user.id, + modelId: user.id, + }; + + await user.destroy(); + + const mentionedParagraph: DeepPartial = { + type: "paragraph", + content: [ + { + type: "text", + text: "a paragraph with ", + }, + { + type: "mention", + attrs: mentionAttrs, + }, + { + type: "text", + text: " mentioned", + }, + ], + }; + + const doc = buildProseMirrorDoc([ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + ], + }, + mentionedParagraph, + ]); + + const newDoc = await ProsemirrorHelper.processMentions(doc); + expect(newDoc.content?.[1]?.content?.[1].attrs?.label).toEqual("Unknown"); + }); + + it("should handle updated users", async () => { + const user = await buildUser(); + const mentionAttrs: MentionAttrs = { + id: "9a17c1c8-d178-4350-9001-203a73070fcb", + type: MentionType.User, + label: "test.user", + actorId: user.id, + modelId: user.id, + }; + + await user.update({ + name: faker.name.firstName(), + }); + + const mentionedParagraph: DeepPartial = { + type: "paragraph", + content: [ + { + type: "text", + text: "a paragraph with ", + }, + { + type: "mention", + attrs: mentionAttrs, + }, + { + type: "text", + text: " mentioned", + }, + ], + }; + + const doc = buildProseMirrorDoc([ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + ], + }, + mentionedParagraph, + ]); + + const newDoc = await ProsemirrorHelper.processMentions(doc); + expect(newDoc.content?.[1]?.content?.[1].attrs?.label).toEqual(user.name); + }); + + it("should handle multiple renamed users", async () => { + const firstUser = await buildUser(); + const secondUser = await buildUser(); + + const firstMentionAttrs: MentionAttrs = { + id: "9a17c1c8-d178-4350-9001-203a73070fcb", + type: MentionType.User, + label: "first.user", + actorId: firstUser.id, + modelId: firstUser.id, + }; + + const secondMentionAttrs: MentionAttrs = { + id: "31d5899f-e544-4ff6-b6d3-c49dd6b81901", + type: MentionType.User, + label: "second.user", + actorId: secondUser.id, + modelId: secondUser.id, + }; + + const firstNewName = faker.name.firstName(); + const secondNewName = faker.name.firstName(); + + await firstUser.update({ + name: firstNewName, + }); + + await secondUser.update({ + name: secondNewName, + }); + + const mentionedParagraph: DeepPartial = { + type: "paragraph", + content: [ + { + type: "text", + text: "a paragraph with ", + }, + { + type: "mention", + attrs: firstMentionAttrs, + }, + { + type: "text", + text: " and ", + }, + { + type: "mention", + attrs: secondMentionAttrs, + }, + { + type: "text", + text: " mentioned", + }, + ], + }; + + const doc = buildProseMirrorDoc([ + { + type: "paragraph", + content: [ + { + type: "text", + text: "some content in a paragraph", + }, + ], + }, + mentionedParagraph, + ]); + + const newDoc = await ProsemirrorHelper.processMentions(doc); + expect(newDoc.content?.[1]?.content?.[1].attrs?.label).toEqual( + firstNewName + ); + expect(newDoc.content?.[1]?.content?.[3].attrs?.label).toEqual( + secondNewName + ); + }); + }); + describe("getNodeForMentionEmail", () => { it("should return the paragraph node", () => { const mentionAttrs: MentionAttrs = { diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index 2883a2345b..7e08d860ae 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -21,6 +21,7 @@ import { schema, parser } from "@server/editor"; import Logger from "@server/logging/Logger"; import { trace } from "@server/logging/tracing"; import Attachment from "@server/models/Attachment"; +import User from "@server/models/User"; import FileStorage from "@server/storage/files"; export type HTMLOptions = { @@ -558,4 +559,79 @@ export class ProsemirrorHelper { return dom.serialize(); } + + /** + * Processes mentions in the Prosemirror data, ensuring that mentions + * for deleted users are displayed as "@unknown" and updated names are + * displayed correctly. + * + * @param data The ProsemirrorData object to process + * @returns The processed ProsemirrorData with updated mentions + */ + static async processMentions(data: ProsemirrorData | Node) { + const json = "toJSON" in data ? (data.toJSON() as ProsemirrorData) : data; + + // First pass: collect all user IDs from mentions + const userIds: string[] = []; + + function collectUserIds(node: ProsemirrorData) { + if ( + node.type === "mention" && + node.attrs?.type === MentionType.User && + node.attrs?.modelId + ) { + userIds.push(node.attrs.modelId as string); + } + + if (node.content) { + for (const child of node.content) { + collectUserIds(child); + } + } + } + + collectUserIds(json); + + // Load all users in a single query + const uniqueUserIds = [...new Set(userIds)]; + const users = uniqueUserIds.length + ? await User.findAll({ + where: { + id: uniqueUserIds, + }, + attributes: ["id", "name"], + }) + : []; + + // Create a map for quick lookup + const userMap = new Map(); + users.forEach((user) => { + userMap.set(user.id, user.name); + }); + + // Second pass: transform mentions with loaded user data + function transformMentions(node: ProsemirrorData) { + if ( + node.type === "mention" && + node.attrs?.type === MentionType.User && + node.attrs?.modelId + ) { + const userId = node.attrs.modelId as string; + node.attrs = { + ...node.attrs, + label: userMap.get(userId) || "Unknown", + }; + } + + if (node.content) { + for (const child of node.content) { + transformMentions(child); + } + } + + return node; + } + + return transformMentions(json); + } }