mirror of
https://github.com/outline/outline.git
synced 2026-12-01 09:22:14 +03:00
perf: Batch comment view lookups and memoize email diff clipping (#14251)
Load the views of all comment notification recipients in one query instead of one query for each recipient, and check each element for diff classes with one combined selector, once.
This commit is contained in:
1 parent
86e274fe84
commit
d2ad848cfb
3 files changed
+102
-32
No files matched your search
@@ -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<Element, boolean>();
|
||||
|
||||
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
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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`
|
||||
|
||||
Reference in new issue
Block a user