mirror of
https://github.com/outline/outline.git
synced 2026-12-02 01:34:47 +03:00
Fix misalignment between email and comment mentions (#9234)
* Fix misalignment between email and comment mentions * Add test * Update ProsemirrorHelper.tsx * Optimize user mention processing with batch loading * Add test for multiple mentions --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Tom Moor <tom@getoutline.com>
This commit is contained in:
1 parent
784d075233
commit
e5ded0a6a5
3 files changed
+261
-2
No files matched your search
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
@@ -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<ProsemirrorData> = {
|
||||
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<ProsemirrorData> = {
|
||||
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<ProsemirrorData> = {
|
||||
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 = {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user