From 152852172f338d751b58583cc614511728da8dfd Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 31 Oct 2026 14:32:21 -0400 Subject: [PATCH] refactor: Check only deleted document subscriptions for thread unsubscribe --- server/models/helpers/NotificationHelper.ts | 46 +++++---------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/server/models/helpers/NotificationHelper.ts b/server/models/helpers/NotificationHelper.ts index 9801011ce7..d513cdb921 100644 --- a/server/models/helpers/NotificationHelper.ts +++ b/server/models/helpers/NotificationHelper.ts @@ -154,9 +154,8 @@ export default class NotificationHelper { }; /** - * Get the users involved in a comment thread who unsubscribed from the - * document after they were last involved in the thread. A user is involved - * in the thread when they author a comment, or are mentioned in one. + * Get the users who unsubscribed from the document and did not comment in + * the thread after they unsubscribed. * * @param documentId The id of the document the thread belongs to. * @param comments The comments in the thread. @@ -166,36 +165,11 @@ export default class NotificationHelper { documentId: string, comments: Comment[] ): Promise> => { - const lastInvolvedAt = new Map(); - const track = (userId: string, date: Date) => { - const current = lastInvolvedAt.get(userId); - if (!current || date > current) { - lastInvolvedAt.set(userId, date); - } - }; - - for (const comment of comments) { - track(comment.createdById, comment.createdAt); - - const mentions = ProsemirrorHelper.parseMentions( - ProsemirrorHelper.toProsemirror(comment.data), - { type: MentionType.User } - ); - for (const mention of mentions) { - track(mention.modelId, comment.createdAt); - } - } - - if (!lastInvolvedAt.size) { - return new Set(); - } - const deletedSubscriptions = await Subscription.findAll({ attributes: ["userId", "deletedAt"], where: { documentId, event: SubscriptionType.Document, - userId: { [Op.in]: [...lastInvolvedAt.keys()] }, deletedAt: { [Op.ne]: null }, }, paranoid: false, @@ -203,14 +177,14 @@ export default class NotificationHelper { return new Set( deletedSubscriptions - .filter((subscription) => { - const involvedAt = lastInvolvedAt.get(subscription.userId); - return ( - !!subscription.deletedAt && - !!involvedAt && - subscription.deletedAt > involvedAt - ); - }) + .filter( + ({ userId, deletedAt }) => + !!deletedAt && + !comments.some( + (comment) => + comment.createdById === userId && comment.createdAt > deletedAt + ) + ) .map((subscription) => subscription.userId) ); };