fix: Email notifications not sent when mention added to edited comment (#5145

* WIP: Need new email template

* New emails
This commit is contained in:
Tom Moor
2023-04-02 14:46:47 -04:00
committed by GitHub
parent 40103c9d8f
commit cf3689014b
9 changed files with 298 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
import { NotificationEventType } from "@shared/types";
import DocumentNotificationEmail from "@server/emails/templates/DocumentNotificationEmail";
import DocumentPublishedOrUpdatedEmail from "@server/emails/templates/DocumentPublishedOrUpdatedEmail";
import {
View,
Subscription,
@@ -26,7 +26,7 @@ beforeEach(async () => {
describe("documents.publish", () => {
test("should not send a notification to author", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
@@ -55,7 +55,7 @@ describe("documents.publish", () => {
test("should send a notification to other users in team", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const user = await buildUser();
@@ -82,7 +82,7 @@ describe("documents.publish", () => {
test("should send only one notification in a 12-hour window", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const user = await buildUser();
@@ -125,7 +125,7 @@ describe("documents.publish", () => {
test("should not send a notification to users without collection access", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const user = await buildUser();
@@ -159,7 +159,7 @@ describe("documents.publish", () => {
describe("revisions.create", () => {
test("should send a notification to other collaborators", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -187,7 +187,7 @@ describe("revisions.create", () => {
test("should not send a notification if viewed since update", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -219,7 +219,7 @@ describe("revisions.create", () => {
test("should not send a notification to last editor", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const user = await buildUser();
@@ -247,7 +247,7 @@ describe("revisions.create", () => {
test("should send a notification for subscriptions, even to collaborator", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -331,7 +331,7 @@ describe("revisions.create", () => {
test("should not send multiple emails", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const collaborator0 = await buildUser();
@@ -382,7 +382,7 @@ describe("revisions.create", () => {
test("should not create subscriptions if previously unsubscribed", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const collaborator0 = await buildUser();
@@ -445,7 +445,7 @@ describe("revisions.create", () => {
test("should send a notification for subscriptions to non-collaborators", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -487,7 +487,7 @@ describe("revisions.create", () => {
test("should not send a notification for subscriptions to collaborators if unsubscribed", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -532,7 +532,7 @@ describe("revisions.create", () => {
test("should not send a notification for subscriptions to members outside of the team", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -580,7 +580,7 @@ describe("revisions.create", () => {
test("should not send a notification if viewed since update", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const document = await buildDocument();
@@ -610,7 +610,7 @@ describe("revisions.create", () => {
test("should not send a notification to last editor", async () => {
const schedule = jest.spyOn(
DocumentNotificationEmail.prototype,
DocumentPublishedOrUpdatedEmail.prototype,
"schedule"
);
const user = await buildUser();

View File

@@ -6,8 +6,8 @@ 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 DocumentNotificationEmail from "@server/emails/templates/DocumentNotificationEmail";
import MentionNotificationEmail from "@server/emails/templates/MentionNotificationEmail";
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 {
@@ -29,6 +29,7 @@ import {
CommentEvent,
} from "@server/types";
import CommentCreatedNotificationTask from "../tasks/CommentCreatedNotificationTask";
import CommentUpdatedNotificationTask from "../tasks/CommentUpdatedNotificationTask";
import BaseProcessor from "./BaseProcessor";
export default class NotificationsProcessor extends BaseProcessor {
@@ -37,6 +38,7 @@ export default class NotificationsProcessor extends BaseProcessor {
"revisions.create",
"collections.create",
"comments.create",
"comments.update",
];
async perform(event: Event) {
@@ -49,6 +51,8 @@ export default class NotificationsProcessor extends BaseProcessor {
return this.collectionCreated(event);
case "comments.create":
return this.commentCreated(event);
case "comments.update":
return this.commentUpdated(event);
default:
}
}
@@ -59,6 +63,12 @@ export default class NotificationsProcessor extends BaseProcessor {
});
}
async commentUpdated(event: CommentEvent) {
await CommentUpdatedNotificationTask.schedule(event, {
delay: Minute,
});
}
async documentPublished(event: DocumentEvent) {
// never send notifications when batch importing documents
if (
@@ -104,7 +114,7 @@ export default class NotificationsProcessor extends BaseProcessor {
documentId: document.id,
});
userIdsSentNotifications.push(recipient.id);
await new MentionNotificationEmail(
await new DocumentMentionedEmail(
{
to: recipient.email,
documentId: event.documentId,
@@ -137,7 +147,7 @@ export default class NotificationsProcessor extends BaseProcessor {
teamId: team.id,
documentId: document.id,
});
await new DocumentNotificationEmail(
await new DocumentPublishedOrUpdatedEmail(
{
to: recipient.email,
userId: recipient.id,
@@ -193,7 +203,7 @@ export default class NotificationsProcessor extends BaseProcessor {
documentId: document.id,
});
userIdsSentNotifications.push(recipient.id);
await new MentionNotificationEmail(
await new DocumentMentionedEmail(
{
to: recipient.email,
documentId: event.documentId,
@@ -241,7 +251,7 @@ export default class NotificationsProcessor extends BaseProcessor {
documentId: document.id,
});
await new DocumentNotificationEmail(
await new DocumentPublishedOrUpdatedEmail(
{
to: recipient.email,
userId: recipient.id,

View File

@@ -4,6 +4,7 @@ import subscriptionCreator from "@server/commands/subscriptionCreator";
import { sequelize } from "@server/database/sequelize";
import { schema } from "@server/editor";
import CommentCreatedEmail from "@server/emails/templates/CommentCreatedEmail";
import CommentMentionedEmail from "@server/emails/templates/CommentMentionedEmail";
import { Comment, Document, Notification, Team, User } from "@server/models";
import DocumentHelper from "@server/models/helpers/DocumentHelper";
import NotificationHelper from "@server/models/helpers/NotificationHelper";
@@ -82,13 +83,12 @@ export default class CommentCreatedNotificationTask extends BaseTask<
});
userIdsSentNotifications.push(recipient.id);
await new CommentCreatedEmail(
await new CommentMentionedEmail(
{
to: recipient.email,
userId: recipient.id,
documentId: document.id,
teamUrl: team.url,
isReply: !!comment.parentCommentId,
actorName: comment.createdBy.name,
commentId: comment.id,
content,

View File

@@ -0,0 +1,91 @@
import { NotificationEventType } from "@shared/types";
import CommentMentionedEmail from "@server/emails/templates/CommentMentionedEmail";
import { Comment, Document, Notification, Team, User } from "@server/models";
import DocumentHelper from "@server/models/helpers/DocumentHelper";
import ProsemirrorHelper from "@server/models/helpers/ProsemirrorHelper";
import { CommentEvent, CommentUpdateEvent } from "@server/types";
import BaseTask, { TaskPriority } from "./BaseTask";
export default class CommentUpdatedNotificationTask extends BaseTask<
CommentEvent
> {
public async perform(event: CommentUpdateEvent) {
const [document, comment, team] = await Promise.all([
Document.scope("withCollection").findOne({
where: {
id: event.documentId,
},
}),
Comment.findByPk(event.modelId),
Team.findByPk(event.teamId),
]);
if (!document || !comment || !team) {
return;
}
const mentions = ProsemirrorHelper.parseMentions(
ProsemirrorHelper.toProsemirror(comment.data)
).filter((mention) => event.data.newMentionIds.includes(mention.id));
if (mentions.length === 0) {
return;
}
let content = ProsemirrorHelper.toHTML(
ProsemirrorHelper.toProsemirror(comment.data),
{
centered: false,
}
);
if (!content) {
return;
}
content = await DocumentHelper.attachmentsToSignedUrls(
content,
event.teamId,
86400 * 4
);
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: actor.id,
teamId: team.id,
documentId: document.id,
});
await new CommentMentionedEmail(
{
to: recipient.email,
userId: recipient.id,
documentId: document.id,
teamUrl: team.url,
actorName: comment.createdBy.name,
commentId: comment.id,
content,
collectionName: document.collection?.name,
},
{ notificationId: notification.id }
).schedule();
}
}
}
public get options() {
return {
attempts: 1,
priority: TaskPriority.Background,
};
}
}