diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 0009e3e8e9..bcb2b82962 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -557,25 +557,33 @@ export class DocumentHelper { * @returns The clipped HTML, or undefined when there is nothing to show. */ private static clipEmailDiff(doc: JSDOM["window"]["document"]) { + const diffSelector = [ + EditorStyleHelper.diffInsertion, + EditorStyleHelper.diffDeletion, + EditorStyleHelper.diffModification, + EditorStyleHelper.diffNodeInsertion, + EditorStyleHelper.diffNodeDeletion, + EditorStyleHelper.diffNodeModification, + ] + .map((className) => `.${className}`) + .join(","); + + // Each node is checked again as the neighbor of its siblings, and removing + // nodes without changes does not change the result for the others. + const results = new Map(); + const containsDiffElement = (node: Element | null) => { if (!node) { return false; } - const diffClasses = [ - EditorStyleHelper.diffInsertion, - EditorStyleHelper.diffDeletion, - EditorStyleHelper.diffModification, - EditorStyleHelper.diffNodeInsertion, - EditorStyleHelper.diffNodeDeletion, - EditorStyleHelper.diffNodeModification, - ]; - - return diffClasses.some( - (className) => - node.classList.contains(className) || - node.querySelector(`.${className}`) - ); + let result = results.get(node); + if (result === undefined) { + result = + node.matches(diffSelector) || !!node.querySelector(diffSelector); + results.set(node, result); + } + return result; }; // The diffing lib isn't able to catch all changes currently, e.g. changing diff --git a/server/models/helpers/NotificationHelper.test.ts b/server/models/helpers/NotificationHelper.test.ts index 46cf3377d5..e9522773ca 100644 --- a/server/models/helpers/NotificationHelper.test.ts +++ b/server/models/helpers/NotificationHelper.test.ts @@ -1,5 +1,7 @@ +import { addMinutes, subMinutes } from "date-fns"; import { DocumentPermission, NotificationEventType } from "@shared/types"; -import { UserMembership } from "@server/models"; +import { UserMembership, View } from "@server/models"; +import { sequelize } from "@server/storage/database"; import { buildComment, buildDocument, @@ -281,6 +283,64 @@ describe("NotificationHelper", () => { expect(recipients.length).toEqual(1); expect(recipients[0].id).toEqual(notificationEnabledUserInThread.id); }); + + it("should not return users who viewed the document after the comment", async () => { + const documentAuthor = await buildUser(); + const document = await buildDocument({ + userId: documentAuthor.id, + teamId: documentAuthor.teamId, + }); + const [viewedBefore, viewedAfter] = await Promise.all([ + buildUser({ + teamId: document.teamId, + notificationSettings: { + [NotificationEventType.CreateComment]: true, + }, + }), + buildUser({ + teamId: document.teamId, + notificationSettings: { + [NotificationEventType.CreateComment]: true, + }, + }), + ]); + await Promise.all( + [viewedBefore, viewedAfter].map((user) => + buildSubscription({ userId: user.id, documentId: document.id }) + ) + ); + + const comment = await buildComment({ + documentId: document.id, + userId: documentAuthor.id, + }); + await Promise.all( + [ + { user: viewedBefore, updatedAt: subMinutes(comment.createdAt, 1) }, + { user: viewedAfter, updatedAt: addMinutes(comment.createdAt, 1) }, + ].map(async ({ user, updatedAt }) => { + const view = await View.create({ + userId: user.id, + documentId: document.id, + }); + await sequelize.query( + 'UPDATE views SET "updatedAt" = :updatedAt WHERE id = :id', + { replacements: { updatedAt, id: view.id } } + ); + }) + ); + + const recipients = + await NotificationHelper.getCommentNotificationRecipients( + document, + comment, + comment.createdById + ); + + expect(recipients.map((recipient) => recipient.id)).toEqual([ + viewedBefore.id, + ]); + }); }); describe("getDocumentNotificationRecipients", () => { diff --git a/server/models/helpers/NotificationHelper.ts b/server/models/helpers/NotificationHelper.ts index 951732ad0d..f9bfc23a5c 100644 --- a/server/models/helpers/NotificationHelper.ts +++ b/server/models/helpers/NotificationHelper.ts @@ -107,26 +107,28 @@ export default class NotificationHelper { }); } + recipients = recipients.filter((recipient) => !recipient.isSuspended); + + // Recipients that viewed the document since the comment was made do not + // need a notification. + const views = recipients.length + ? await View.findAll({ + attributes: ["userId"], + where: { + userId: recipients.map((recipient) => recipient.id), + documentId: document.id, + updatedAt: { + [Op.gt]: comment.createdAt, + }, + }, + }) + : []; + const viewedUserIds = new Set(views.map((view) => view.userId)); + const filtered: User[] = []; for (const recipient of recipients) { - if (recipient.isSuspended) { - continue; - } - - // If this recipient has viewed the document since the comment was made - // then we can avoid sending them a useless notification, yay. - const view = await View.findOne({ - where: { - userId: recipient.id, - documentId: document.id, - updatedAt: { - [Op.gt]: comment.createdAt, - }, - }, - }); - - if (view) { + if (viewedUserIds.has(recipient.id)) { Logger.info( "processor", `suppressing notification to ${recipient.id} because doc viewed`