Notifications refactor (#5151
* Ongoing * refactor * test * Add cleanup task * refactor
This commit is contained in:
51
server/queues/processors/EmailsProcessor.ts
Normal file
51
server/queues/processors/EmailsProcessor.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import CollectionCreatedEmail from "@server/emails/templates/CollectionCreatedEmail";
|
||||
import CommentCreatedEmail from "@server/emails/templates/CommentCreatedEmail";
|
||||
import CommentMentionedEmail from "@server/emails/templates/CommentMentionedEmail";
|
||||
import DocumentMentionedEmail from "@server/emails/templates/DocumentMentionedEmail";
|
||||
import DocumentPublishedOrUpdatedEmail from "@server/emails/templates/DocumentPublishedOrUpdatedEmail";
|
||||
import { Notification } from "@server/models";
|
||||
import { Event, NotificationEvent } from "@server/types";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class NotificationsProcessor extends BaseProcessor {
|
||||
static applicableEvents: Event["name"][] = ["notifications.create"];
|
||||
|
||||
async perform(event: NotificationEvent) {
|
||||
const notification = await Notification.scope([
|
||||
"withTeam",
|
||||
"withUser",
|
||||
"withActor",
|
||||
]).findByPk(event.modelId);
|
||||
if (!notification) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (notification.event) {
|
||||
case NotificationEventType.UpdateDocument:
|
||||
case NotificationEventType.PublishDocument: {
|
||||
await new DocumentPublishedOrUpdatedEmail(notification).schedule();
|
||||
return;
|
||||
}
|
||||
|
||||
case NotificationEventType.MentionedInDocument: {
|
||||
await new DocumentMentionedEmail(notification).schedule();
|
||||
return;
|
||||
}
|
||||
|
||||
case NotificationEventType.MentionedInComment: {
|
||||
await new CommentMentionedEmail(notification).schedule();
|
||||
return;
|
||||
}
|
||||
|
||||
case NotificationEventType.CreateCollection: {
|
||||
await new CollectionCreatedEmail(notification).schedule();
|
||||
return;
|
||||
}
|
||||
|
||||
case NotificationEventType.CreateComment: {
|
||||
await new CommentCreatedEmail(notification).schedule();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,635 +0,0 @@
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import DocumentPublishedOrUpdatedEmail from "@server/emails/templates/DocumentPublishedOrUpdatedEmail";
|
||||
import {
|
||||
View,
|
||||
Subscription,
|
||||
Event,
|
||||
Notification,
|
||||
Revision,
|
||||
} from "@server/models";
|
||||
import {
|
||||
buildDocument,
|
||||
buildCollection,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import NotificationsProcessor from "./NotificationsProcessor";
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe("documents.publish", () => {
|
||||
test("should not send a notification to author", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
lastModifiedById: user.id,
|
||||
});
|
||||
user.setNotificationEventType(NotificationEventType.PublishDocument);
|
||||
await user.save();
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "documents.publish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: document.createdById,
|
||||
data: {
|
||||
title: document.title,
|
||||
},
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should send a notification to other users in team", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
user.setNotificationEventType(NotificationEventType.PublishDocument);
|
||||
await user.save();
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "documents.publish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: document.createdById,
|
||||
data: {
|
||||
title: document.title,
|
||||
},
|
||||
ip,
|
||||
});
|
||||
expect(schedule).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should send only one notification in a 12-hour window", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
lastModifiedById: user.id,
|
||||
});
|
||||
|
||||
const recipient = await buildUser({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
user.setNotificationEventType(NotificationEventType.PublishDocument);
|
||||
await user.save();
|
||||
|
||||
await Notification.create({
|
||||
actorId: user.id,
|
||||
userId: recipient.id,
|
||||
documentId: document.id,
|
||||
teamId: recipient.teamId,
|
||||
event: "documents.publish",
|
||||
emailedAt: new Date(),
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "documents.publish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: document.createdById,
|
||||
data: {
|
||||
title: document.title,
|
||||
},
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should not send a notification to users without collection access", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
user.setNotificationEventType(NotificationEventType.PublishDocument);
|
||||
await user.save();
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "documents.publish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: document.createdById,
|
||||
data: {
|
||||
title: document.title,
|
||||
},
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("revisions.create", () => {
|
||||
test("should send a notification to other collaborators", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
await Revision.createFromDocument(document);
|
||||
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
document.collaboratorIds = [collaborator.id];
|
||||
await document.save();
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
expect(schedule).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should not send a notification if viewed since update", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
document.collaboratorIds = [collaborator.id];
|
||||
await document.save();
|
||||
|
||||
await View.create({
|
||||
userId: collaborator.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should not send a notification to last editor", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
lastModifiedById: user.id,
|
||||
});
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should send a notification for subscriptions, even to collaborator", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
const subscriber = await buildUser({ teamId: document.teamId });
|
||||
|
||||
document.collaboratorIds = [collaborator.id, subscriber.id];
|
||||
|
||||
await document.save();
|
||||
|
||||
await Subscription.create({
|
||||
userId: subscriber.id,
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
enabled: true,
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
expect(schedule).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should create subscriptions for collaborator", async () => {
|
||||
const collaborator0 = await buildUser();
|
||||
const collaborator1 = await buildUser({ teamId: collaborator0.teamId });
|
||||
const collaborator2 = await buildUser({ teamId: collaborator0.teamId });
|
||||
const document = await buildDocument({ userId: collaborator0.id });
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
|
||||
await document.update({
|
||||
collaboratorIds: [collaborator0.id, collaborator1.id, collaborator2.id],
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator0.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
const events = await Event.findAll();
|
||||
|
||||
// Should emit 3 `subscriptions.create` events.
|
||||
expect(events.length).toEqual(3);
|
||||
expect(events[0].name).toEqual("subscriptions.create");
|
||||
expect(events[1].name).toEqual("subscriptions.create");
|
||||
expect(events[2].name).toEqual("subscriptions.create");
|
||||
|
||||
// Each event should point to same document.
|
||||
expect(events[0].documentId).toEqual(document.id);
|
||||
expect(events[1].documentId).toEqual(document.id);
|
||||
expect(events[2].documentId).toEqual(document.id);
|
||||
|
||||
// Events should mention correct `userId`.
|
||||
expect(events[0].userId).toEqual(collaborator0.id);
|
||||
expect(events[1].userId).toEqual(collaborator1.id);
|
||||
expect(events[2].userId).toEqual(collaborator2.id);
|
||||
});
|
||||
|
||||
test("should not send multiple emails", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const collaborator0 = await buildUser();
|
||||
const collaborator1 = await buildUser({ teamId: collaborator0.teamId });
|
||||
const collaborator2 = await buildUser({ teamId: collaborator0.teamId });
|
||||
const document = await buildDocument({
|
||||
teamId: collaborator0.teamId,
|
||||
userId: collaborator0.id,
|
||||
});
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
|
||||
await document.update({
|
||||
collaboratorIds: [collaborator0.id, collaborator1.id, collaborator2.id],
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
// Changing document will emit a `documents.update` event.
|
||||
await processor.perform({
|
||||
name: "documents.update",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
createdAt: document.updatedAt.toString(),
|
||||
teamId: document.teamId,
|
||||
data: { title: document.title, autosave: false, done: true },
|
||||
actorId: collaborator2.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
// Those changes will also emit a `revisions.create` event.
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator0.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
// This should send out 2 emails, one for each collaborator that did not
|
||||
// participate in the edit
|
||||
expect(schedule).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test("should not create subscriptions if previously unsubscribed", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const collaborator0 = await buildUser();
|
||||
const collaborator1 = await buildUser({ teamId: collaborator0.teamId });
|
||||
const collaborator2 = await buildUser({ teamId: collaborator0.teamId });
|
||||
const document = await buildDocument({
|
||||
teamId: collaborator0.teamId,
|
||||
userId: collaborator0.id,
|
||||
});
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
|
||||
await document.update({
|
||||
collaboratorIds: [collaborator0.id, collaborator1.id, collaborator2.id],
|
||||
});
|
||||
|
||||
// `collaborator2` created a subscription.
|
||||
const subscription2 = await Subscription.create({
|
||||
userId: collaborator2.id,
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
});
|
||||
|
||||
// `collaborator2` would no longer like to be notified.
|
||||
await subscription2.destroy();
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator0.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
const events = await Event.findAll();
|
||||
|
||||
// Should emit 2 `subscriptions.create` events.
|
||||
expect(events.length).toEqual(2);
|
||||
expect(events[0].name).toEqual("subscriptions.create");
|
||||
expect(events[1].name).toEqual("subscriptions.create");
|
||||
|
||||
// Each event should point to same document.
|
||||
expect(events[0].documentId).toEqual(document.id);
|
||||
expect(events[1].documentId).toEqual(document.id);
|
||||
|
||||
// Events should mention correct `userId`.
|
||||
expect(events[0].userId).toEqual(collaborator0.id);
|
||||
expect(events[1].userId).toEqual(collaborator1.id);
|
||||
|
||||
// One notification as one collaborator performed edit and the other is
|
||||
// unsubscribed
|
||||
expect(schedule).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("should send a notification for subscriptions to non-collaborators", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
const subscriber = await buildUser({ teamId: document.teamId });
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
|
||||
// `subscriber` hasn't collaborated on `document`.
|
||||
document.collaboratorIds = [collaborator.id];
|
||||
|
||||
await document.save();
|
||||
|
||||
// `subscriber` subscribes to `document`'s changes.
|
||||
// Specifically "documents.update" event.
|
||||
await Subscription.create({
|
||||
userId: subscriber.id,
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
enabled: true,
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
expect(schedule).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should not send a notification for subscriptions to collaborators if unsubscribed", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
const subscriber = await buildUser({ teamId: document.teamId });
|
||||
|
||||
// `subscriber` has collaborated on `document`.
|
||||
document.collaboratorIds = [collaborator.id, subscriber.id];
|
||||
|
||||
await document.save();
|
||||
|
||||
// `subscriber` subscribes to `document`'s changes.
|
||||
// Specifically "documents.update" event.
|
||||
const subscription = await Subscription.create({
|
||||
userId: subscriber.id,
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
enabled: true,
|
||||
});
|
||||
|
||||
subscription.destroy();
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
// Should send notification to `collaborator` and not `subscriber`.
|
||||
expect(schedule).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("should not send a notification for subscriptions to members outside of the team", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
await Revision.createFromDocument(document);
|
||||
document.text = "Updated body content";
|
||||
document.updatedAt = new Date();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
|
||||
// `subscriber` *does not* belong
|
||||
// to `collaborator`'s team,
|
||||
const subscriber = await buildUser();
|
||||
|
||||
// `subscriber` hasn't collaborated on `document`.
|
||||
document.collaboratorIds = [collaborator.id];
|
||||
|
||||
await document.save();
|
||||
|
||||
// `subscriber` subscribes to `document`'s changes.
|
||||
// Specifically "documents.update" event.
|
||||
// Not sure how they got hold of this document,
|
||||
// but let's just pretend they did!
|
||||
await Subscription.create({
|
||||
userId: subscriber.id,
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
enabled: true,
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
|
||||
// Should send notification to `collaborator` and not `subscriber`.
|
||||
expect(schedule).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("should not send a notification if viewed since update", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const document = await buildDocument();
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
document.collaboratorIds = [collaborator.id];
|
||||
await document.save();
|
||||
|
||||
await View.create({
|
||||
userId: collaborator.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: collaborator.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should not send a notification to last editor", async () => {
|
||||
const schedule = jest.spyOn(
|
||||
DocumentPublishedOrUpdatedEmail.prototype,
|
||||
"schedule"
|
||||
);
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
lastModifiedById: user.id,
|
||||
});
|
||||
const revision = await Revision.createFromDocument(document);
|
||||
|
||||
const processor = new NotificationsProcessor();
|
||||
await processor.perform({
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
modelId: revision.id,
|
||||
ip,
|
||||
});
|
||||
expect(schedule).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,26 +1,4 @@
|
||||
import { subHours } from "date-fns";
|
||||
import { differenceBy } from "lodash";
|
||||
import { Op } from "sequelize";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import { Minute } from "@shared/utils/time";
|
||||
import subscriptionCreator from "@server/commands/subscriptionCreator";
|
||||
import { sequelize } from "@server/database/sequelize";
|
||||
import CollectionCreatedEmail from "@server/emails/templates/CollectionCreatedEmail";
|
||||
import DocumentMentionedEmail from "@server/emails/templates/DocumentMentionedEmail";
|
||||
import DocumentPublishedOrUpdatedEmail from "@server/emails/templates/DocumentPublishedOrUpdatedEmail";
|
||||
import env from "@server/env";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import {
|
||||
Document,
|
||||
Team,
|
||||
Collection,
|
||||
Notification,
|
||||
Revision,
|
||||
User,
|
||||
View,
|
||||
} from "@server/models";
|
||||
import DocumentHelper from "@server/models/helpers/DocumentHelper";
|
||||
import NotificationHelper from "@server/models/helpers/NotificationHelper";
|
||||
import {
|
||||
CollectionEvent,
|
||||
RevisionEvent,
|
||||
@@ -28,8 +6,11 @@ import {
|
||||
DocumentEvent,
|
||||
CommentEvent,
|
||||
} from "@server/types";
|
||||
import CommentCreatedNotificationTask from "../tasks/CommentCreatedNotificationTask";
|
||||
import CommentUpdatedNotificationTask from "../tasks/CommentUpdatedNotificationTask";
|
||||
import CollectionCreatedNotificationsTask from "../tasks/CollectionCreatedNotificationsTask";
|
||||
import CommentCreatedNotificationsTask from "../tasks/CommentCreatedNotificationsTask";
|
||||
import CommentUpdatedNotificationsTask from "../tasks/CommentUpdatedNotificationsTask";
|
||||
import DocumentPublishedNotificationsTask from "../tasks/DocumentPublishedNotificationsTask";
|
||||
import RevisionCreatedNotificationsTask from "../tasks/RevisionCreatedNotificationsTask";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class NotificationsProcessor extends BaseProcessor {
|
||||
@@ -57,20 +38,8 @@ export default class NotificationsProcessor extends BaseProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
async commentCreated(event: CommentEvent) {
|
||||
await CommentCreatedNotificationTask.schedule(event, {
|
||||
delay: Minute,
|
||||
});
|
||||
}
|
||||
|
||||
async commentUpdated(event: CommentEvent) {
|
||||
await CommentUpdatedNotificationTask.schedule(event, {
|
||||
delay: Minute,
|
||||
});
|
||||
}
|
||||
|
||||
async documentPublished(event: DocumentEvent) {
|
||||
// never send notifications when batch importing documents
|
||||
// never send notifications when batch importing
|
||||
if (
|
||||
"data" in event &&
|
||||
"source" in event.data &&
|
||||
@@ -79,304 +48,35 @@ export default class NotificationsProcessor extends BaseProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
const [collection, document, team] = await Promise.all([
|
||||
Collection.findByPk(event.collectionId),
|
||||
Document.findByPk(event.documentId, { includeState: true }),
|
||||
Team.findByPk(event.teamId),
|
||||
]);
|
||||
|
||||
if (!document || !team || !collection) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.createDocumentSubscriptions(document, event);
|
||||
|
||||
// Send notifications to mentioned users first
|
||||
const mentions = DocumentHelper.parseMentions(document);
|
||||
const userIdsSentNotifications: string[] = [];
|
||||
|
||||
for (const mention of mentions) {
|
||||
const [recipient, actor] = await Promise.all([
|
||||
User.findByPk(mention.modelId),
|
||||
User.findByPk(mention.actorId),
|
||||
]);
|
||||
if (
|
||||
recipient &&
|
||||
actor &&
|
||||
recipient.id !== actor.id &&
|
||||
recipient.subscribedToEventType(NotificationEventType.Mentioned)
|
||||
) {
|
||||
const notification = await Notification.create({
|
||||
event: event.name,
|
||||
userId: recipient.id,
|
||||
actorId: document.updatedBy.id,
|
||||
teamId: team.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
userIdsSentNotifications.push(recipient.id);
|
||||
await new DocumentMentionedEmail(
|
||||
{
|
||||
to: recipient.email,
|
||||
documentId: event.documentId,
|
||||
actorName: actor.name,
|
||||
teamUrl: team.url,
|
||||
mentionId: mention.id,
|
||||
},
|
||||
{ notificationId: notification.id }
|
||||
).schedule();
|
||||
}
|
||||
}
|
||||
|
||||
const recipients = (
|
||||
await NotificationHelper.getDocumentNotificationRecipients(
|
||||
document,
|
||||
NotificationEventType.PublishDocument,
|
||||
document.lastModifiedById,
|
||||
false
|
||||
)
|
||||
).filter((recipient) => !userIdsSentNotifications.includes(recipient.id));
|
||||
|
||||
for (const recipient of recipients) {
|
||||
const notify = await this.shouldNotify(document, recipient);
|
||||
|
||||
if (notify) {
|
||||
const notification = await Notification.create({
|
||||
event: event.name,
|
||||
userId: recipient.id,
|
||||
actorId: document.updatedBy.id,
|
||||
teamId: team.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
await new DocumentPublishedOrUpdatedEmail(
|
||||
{
|
||||
to: recipient.email,
|
||||
userId: recipient.id,
|
||||
eventType: NotificationEventType.PublishDocument,
|
||||
documentId: document.id,
|
||||
teamUrl: team.url,
|
||||
actorName: document.updatedBy.name,
|
||||
collectionName: collection.name,
|
||||
},
|
||||
{ notificationId: notification.id }
|
||||
).schedule();
|
||||
}
|
||||
}
|
||||
await DocumentPublishedNotificationsTask.schedule(event);
|
||||
}
|
||||
|
||||
async revisionCreated(event: RevisionEvent) {
|
||||
const [collection, document, revision, team] = await Promise.all([
|
||||
Collection.findByPk(event.collectionId),
|
||||
Document.findByPk(event.documentId, { includeState: true }),
|
||||
Revision.findByPk(event.modelId),
|
||||
Team.findByPk(event.teamId),
|
||||
]);
|
||||
|
||||
if (!document || !team || !revision || !collection) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.createDocumentSubscriptions(document, event);
|
||||
|
||||
// Send notifications to mentioned users first
|
||||
const prev = await revision.previous();
|
||||
const oldMentions = prev ? DocumentHelper.parseMentions(prev) : [];
|
||||
const newMentions = DocumentHelper.parseMentions(document);
|
||||
const mentions = differenceBy(newMentions, oldMentions, "id");
|
||||
const userIdsSentNotifications: string[] = [];
|
||||
|
||||
for (const mention of mentions) {
|
||||
const [recipient, actor] = await Promise.all([
|
||||
User.findByPk(mention.modelId),
|
||||
User.findByPk(mention.actorId),
|
||||
]);
|
||||
if (
|
||||
recipient &&
|
||||
actor &&
|
||||
recipient.id !== actor.id &&
|
||||
recipient.subscribedToEventType(NotificationEventType.Mentioned)
|
||||
) {
|
||||
const notification = await Notification.create({
|
||||
event: event.name,
|
||||
userId: recipient.id,
|
||||
actorId: document.updatedBy.id,
|
||||
teamId: team.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
userIdsSentNotifications.push(recipient.id);
|
||||
await new DocumentMentionedEmail(
|
||||
{
|
||||
to: recipient.email,
|
||||
documentId: event.documentId,
|
||||
actorName: actor.name,
|
||||
teamUrl: team.url,
|
||||
mentionId: mention.id,
|
||||
},
|
||||
{ notificationId: notification.id }
|
||||
).schedule();
|
||||
}
|
||||
}
|
||||
|
||||
const recipients = (
|
||||
await NotificationHelper.getDocumentNotificationRecipients(
|
||||
document,
|
||||
NotificationEventType.UpdateDocument,
|
||||
document.lastModifiedById,
|
||||
true
|
||||
)
|
||||
).filter((recipient) => !userIdsSentNotifications.includes(recipient.id));
|
||||
if (!recipients.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
// generate the diff html for the email
|
||||
const before = await revision.previous();
|
||||
const content = await DocumentHelper.toEmailDiff(before, revision, {
|
||||
includeTitle: false,
|
||||
centered: false,
|
||||
signedUrls: 86400 * 4,
|
||||
});
|
||||
if (!content) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const recipient of recipients) {
|
||||
const notify = await this.shouldNotify(document, recipient);
|
||||
|
||||
if (notify) {
|
||||
const notification = await Notification.create({
|
||||
event: event.name,
|
||||
userId: recipient.id,
|
||||
actorId: document.updatedBy.id,
|
||||
teamId: team.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
await new DocumentPublishedOrUpdatedEmail(
|
||||
{
|
||||
to: recipient.email,
|
||||
userId: recipient.id,
|
||||
eventType: NotificationEventType.UpdateDocument,
|
||||
documentId: document.id,
|
||||
teamUrl: team.url,
|
||||
actorName: document.updatedBy.name,
|
||||
collectionName: collection.name,
|
||||
content,
|
||||
},
|
||||
{ notificationId: notification.id }
|
||||
).schedule();
|
||||
}
|
||||
}
|
||||
await RevisionCreatedNotificationsTask.schedule(event);
|
||||
}
|
||||
|
||||
async collectionCreated(event: CollectionEvent) {
|
||||
const collection = await Collection.scope("withUser").findByPk(
|
||||
event.collectionId
|
||||
);
|
||||
|
||||
if (!collection || !collection.permission) {
|
||||
// never send notifications when batch importing
|
||||
if (
|
||||
"data" in event &&
|
||||
"source" in event.data &&
|
||||
event.data.source === "import"
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const recipients = await NotificationHelper.getCollectionNotificationRecipients(
|
||||
collection,
|
||||
NotificationEventType.CreateCollection
|
||||
);
|
||||
|
||||
for (const recipient of recipients) {
|
||||
// Suppress notifications for suspended users
|
||||
if (recipient.isSuspended || !recipient.email) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await new CollectionCreatedEmail({
|
||||
to: recipient.email,
|
||||
userId: recipient.id,
|
||||
collectionId: collection.id,
|
||||
}).schedule();
|
||||
}
|
||||
await CollectionCreatedNotificationsTask.schedule(event);
|
||||
}
|
||||
|
||||
private shouldNotify = async (
|
||||
document: Document,
|
||||
user: User
|
||||
): Promise<boolean> => {
|
||||
// Deliver only a single notification in a 12 hour window
|
||||
const notification = await Notification.findOne({
|
||||
order: [["createdAt", "DESC"]],
|
||||
where: {
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
emailedAt: {
|
||||
[Op.not]: null,
|
||||
[Op.gte]: subHours(new Date(), 12),
|
||||
},
|
||||
},
|
||||
async commentCreated(event: CommentEvent) {
|
||||
await CommentCreatedNotificationsTask.schedule(event, {
|
||||
delay: Minute,
|
||||
});
|
||||
}
|
||||
|
||||
if (notification) {
|
||||
if (env.ENVIRONMENT === "development") {
|
||||
Logger.info(
|
||||
"processor",
|
||||
`would have suppressed notification to ${user.id}, but not in development`
|
||||
);
|
||||
} else {
|
||||
Logger.info(
|
||||
"processor",
|
||||
`suppressing notification to ${user.id} as recently notified`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// If this recipient has viewed the document since the last update was made
|
||||
// then we can avoid sending them a useless notification, yay.
|
||||
const view = await View.findOne({
|
||||
where: {
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
updatedAt: {
|
||||
[Op.gt]: document.updatedAt,
|
||||
},
|
||||
},
|
||||
async commentUpdated(event: CommentEvent) {
|
||||
await CommentUpdatedNotificationsTask.schedule(event, {
|
||||
delay: Minute,
|
||||
});
|
||||
|
||||
if (view) {
|
||||
Logger.info(
|
||||
"processor",
|
||||
`suppressing notification to ${user.id} because update viewed`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create any new subscriptions that might be missing for collaborators in the
|
||||
* document on publish and revision creation. This does mean that there is a
|
||||
* short period of time where the user is not subscribed after editing until a
|
||||
* revision is created.
|
||||
*
|
||||
* @param document The document to create subscriptions for
|
||||
* @param event The event that triggered the subscription creation
|
||||
*/
|
||||
private createDocumentSubscriptions = async (
|
||||
document: Document,
|
||||
event: DocumentEvent | RevisionEvent
|
||||
): Promise<void> => {
|
||||
await sequelize.transaction(async (transaction) => {
|
||||
const users = await document.collaborators({ transaction });
|
||||
|
||||
for (const user of users) {
|
||||
await subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
resubscribe: false,
|
||||
transaction,
|
||||
ip: event.ip,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user