diff --git a/server/emails/templates/MentionNotificationEmail.tsx b/server/emails/templates/MentionNotificationEmail.tsx index 39418b0c2..4c90c57a1 100644 --- a/server/emails/templates/MentionNotificationEmail.tsx +++ b/server/emails/templates/MentionNotificationEmail.tsx @@ -37,14 +37,18 @@ export default class MentionNotificationEmail extends BaseEmail< return { document }; } - protected subject({ actorName, document }: Props) { - return `${actorName} mentioned you in "${document.title}"`; + protected subject({ document }: Props) { + return `Mentioned you in “${document.title}”`; } protected preview({ actorName }: Props): string { return `${actorName} mentioned you`; } + protected fromName({ actorName }: Props) { + return actorName; + } + protected renderAsText({ actorName, teamUrl, @@ -54,7 +58,7 @@ export default class MentionNotificationEmail extends BaseEmail< return ` You were mentioned -${actorName} mentioned you in the document "${document.title}". +${actorName} mentioned you in the document “${document.title}”. Open Document: ${teamUrl}${document.url}?mentionId=${mentionId} `; diff --git a/server/queues/processors/NotificationsProcessor.ts b/server/queues/processors/NotificationsProcessor.ts index b98651fbc..ca277eec5 100644 --- a/server/queues/processors/NotificationsProcessor.ts +++ b/server/queues/processors/NotificationsProcessor.ts @@ -157,11 +157,49 @@ export default class NotificationsProcessor extends BaseProcessor { await this.createDocumentSubscriptions(document, event); - const recipients = await NotificationHelper.getDocumentNotificationRecipients( - document, - "documents.update", - document.lastModifiedById, - true + // Send notifications to mentioned users first + const prev = await revision.previous(); + const oldMentions = prev ? parseMentions(prev) : []; + const newMentions = 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) { + 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 MentionNotificationEmail.schedule( + { + to: recipient.email, + documentId: event.documentId, + actorName: actor.name, + teamUrl: team.url, + mentionId: mention.id, + }, + { notificationId: notification.id } + ); + } + } + + const recipients = ( + await NotificationHelper.getDocumentNotificationRecipients( + document, + "documents.update", + document.lastModifiedById, + true + ) + ).filter( + (recipient) => !userIdsSentNotifications.includes(recipient.userId) ); if (!recipients.length) { return; @@ -178,37 +216,6 @@ export default class NotificationsProcessor extends BaseProcessor { return; } - // send notifs to newly mentioned users - const prev = await revision.previous(); - const oldMentions = prev ? parseMentions(prev) : []; - const newMentions = parseMentions(document); - const mentions = differenceBy(newMentions, oldMentions, "id"); - 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) { - const notification = await Notification.create({ - event: event.name, - userId: recipient.id, - actorId: document.updatedBy.id, - teamId: team.id, - documentId: document.id, - }); - await MentionNotificationEmail.schedule( - { - to: recipient.email, - documentId: event.documentId, - actorName: actor.name, - teamUrl: team.url, - mentionId: mention.id, - }, - { notificationId: notification.id } - ); - } - } - for (const recipient of recipients) { const notify = await this.shouldNotify(document, recipient.user);