feat: Render diffs in email notifications (#4164)

* deps

* diffCompact

* Diffs in email

* test

* fix: Fade deleted images
fix: Don't include empty paragraphs as context
fix: Allow for same image multiple times and refactor

* Remove target _blank

* fix: Table heading incorrect color
This commit is contained in:
Tom Moor
2022-09-24 23:29:11 +02:00
committed by GitHub
parent 0c5859222f
commit 91d8d27f2d
15 changed files with 396 additions and 85 deletions

View File

@@ -18,7 +18,7 @@ export default class DebounceProcessor extends BaseProcessor {
{
// speed up revision creation in development, we don't have all the
// time in the world.
delay: (env.ENVIRONMENT === "development" ? 1 : 5) * 60 * 1000,
delay: (env.ENVIRONMENT === "development" ? 0.5 : 5) * 60 * 1000,
}
);
break;

View File

@@ -5,6 +5,7 @@ import {
Subscription,
Event,
Notification,
Revision,
} from "@server/models";
import {
buildDocument,
@@ -156,6 +157,7 @@ describe("documents.publish", () => {
describe("revisions.create", () => {
test("should send a notification to other collaborators", async () => {
const document = await buildDocument();
const revision = await Revision.createFromDocument(document);
const collaborator = await buildUser({ teamId: document.teamId });
document.collaboratorIds = [collaborator.id];
await document.save();
@@ -171,7 +173,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
expect(DocumentNotificationEmail.schedule).toHaveBeenCalled();
@@ -179,6 +181,7 @@ describe("revisions.create", () => {
test("should not send a notification if viewed since update", async () => {
const document = await buildDocument();
const revision = await Revision.createFromDocument(document);
const collaborator = await buildUser({ teamId: document.teamId });
document.collaboratorIds = [collaborator.id];
await document.save();
@@ -196,7 +199,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
@@ -208,6 +211,7 @@ describe("revisions.create", () => {
teamId: user.teamId,
lastModifiedById: user.id,
});
const revision = await Revision.createFromDocument(document);
await NotificationSetting.create({
userId: user.id,
teamId: user.teamId,
@@ -220,7 +224,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
modelId: document.id,
modelId: revision.id,
ip,
});
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
@@ -228,6 +232,7 @@ describe("revisions.create", () => {
test("should send a notification for subscriptions, even to collaborator", async () => {
const document = await buildDocument();
const revision = await Revision.createFromDocument(document);
const collaborator = await buildUser({ teamId: document.teamId });
const subscriber = await buildUser({ teamId: document.teamId });
@@ -256,7 +261,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -268,6 +273,7 @@ describe("revisions.create", () => {
const collaborator1 = await buildUser({ teamId: collaborator0.teamId });
const collaborator2 = await buildUser({ teamId: collaborator0.teamId });
const document = await buildDocument({ userId: collaborator0.id });
const revision = await Revision.createFromDocument(document);
await document.update({
collaboratorIds: [collaborator0.id, collaborator1.id, collaborator2.id],
@@ -281,7 +287,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator0.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -312,6 +318,7 @@ describe("revisions.create", () => {
teamId: collaborator0.teamId,
userId: collaborator0.id,
});
const revision = await Revision.createFromDocument(document);
await document.update({
collaboratorIds: [collaborator0.id, collaborator1.id, collaborator2.id],
@@ -338,7 +345,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator0.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -355,6 +362,7 @@ describe("revisions.create", () => {
teamId: collaborator0.teamId,
userId: collaborator0.id,
});
const revision = await Revision.createFromDocument(document);
await document.update({
collaboratorIds: [collaborator0.id, collaborator1.id, collaborator2.id],
@@ -378,7 +386,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator0.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -406,6 +414,7 @@ describe("revisions.create", () => {
const document = await buildDocument();
const collaborator = await buildUser({ teamId: document.teamId });
const subscriber = await buildUser({ teamId: document.teamId });
const revision = await Revision.createFromDocument(document);
// `subscriber` hasn't collaborated on `document`.
document.collaboratorIds = [collaborator.id];
@@ -435,7 +444,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -444,6 +453,7 @@ describe("revisions.create", () => {
test("should not send a notification for subscriptions to collaborators if unsubscribed", async () => {
const document = await buildDocument();
const revision = await Revision.createFromDocument(document);
const collaborator = await buildUser({ teamId: document.teamId });
const subscriber = await buildUser({ teamId: document.teamId });
@@ -477,7 +487,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -487,6 +497,7 @@ describe("revisions.create", () => {
test("should not send a notification for subscriptions to members outside of the team", async () => {
const document = await buildDocument();
const revision = await Revision.createFromDocument(document);
const collaborator = await buildUser({ teamId: document.teamId });
// `subscriber` *does not* belong
@@ -523,7 +534,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
@@ -533,6 +544,7 @@ describe("revisions.create", () => {
test("should not send a notification if viewed since update", async () => {
const document = await buildDocument();
const revision = await Revision.createFromDocument(document);
const collaborator = await buildUser({ teamId: document.teamId });
document.collaboratorIds = [collaborator.id];
await document.save();
@@ -551,7 +563,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: collaborator.id,
modelId: document.id,
modelId: revision.id,
ip,
});
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
@@ -563,6 +575,8 @@ describe("revisions.create", () => {
teamId: user.teamId,
lastModifiedById: user.id,
});
const revision = await Revision.createFromDocument(document);
await NotificationSetting.create({
userId: user.id,
teamId: user.teamId,
@@ -575,7 +589,7 @@ describe("revisions.create", () => {
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
modelId: document.id,
modelId: revision.id,
ip,
});
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();

View File

@@ -5,6 +5,7 @@ import subscriptionCreator from "@server/commands/subscriptionCreator";
import { sequelize } from "@server/database/sequelize";
import CollectionNotificationEmail from "@server/emails/templates/CollectionNotificationEmail";
import DocumentNotificationEmail from "@server/emails/templates/DocumentNotificationEmail";
import env from "@server/env";
import Logger from "@server/logging/Logger";
import {
View,
@@ -15,7 +16,9 @@ import {
NotificationSetting,
Subscription,
Notification,
Revision,
} from "@server/models";
import DocumentHelper from "@server/models/helpers/DocumentHelper";
import {
CollectionEvent,
RevisionEvent,
@@ -34,9 +37,9 @@ export default class NotificationsProcessor extends BaseProcessor {
async perform(event: Event) {
switch (event.name) {
case "documents.publish":
return this.documentPublished(event);
case "revisions.create":
return this.documentUpdated(event);
return this.revisionCreated(event);
case "collections.create":
return this.collectionCreated(event);
@@ -44,10 +47,13 @@ export default class NotificationsProcessor extends BaseProcessor {
}
}
async documentUpdated(event: DocumentEvent | RevisionEvent) {
async documentPublished(event: DocumentEvent) {
// never send notifications when batch importing documents
// @ts-expect-error ts-migrate(2339) FIXME: Property 'data' does not exist on type 'DocumentEv... Remove this comment to see the full error message
if (event.data?.source === "import") {
if (
"data" in event &&
"source" in event.data &&
event.data.source === "import"
) {
return;
}
@@ -65,9 +71,7 @@ export default class NotificationsProcessor extends BaseProcessor {
const recipients = await this.getDocumentNotificationRecipients(
document,
event.name === "documents.publish"
? "documents.publish"
: "documents.update"
"documents.publish"
);
for (const recipient of recipients) {
@@ -84,8 +88,7 @@ export default class NotificationsProcessor extends BaseProcessor {
await DocumentNotificationEmail.schedule(
{
to: recipient.user.email,
eventName:
event.name === "documents.publish" ? "published" : "updated",
eventName: "published",
documentId: document.id,
teamUrl: team.url,
actorName: document.updatedBy.name,
@@ -98,6 +101,66 @@ export default class NotificationsProcessor extends BaseProcessor {
}
}
async revisionCreated(event: RevisionEvent) {
const [collection, document, revision, team] = await Promise.all([
Collection.findByPk(event.collectionId),
Document.findByPk(event.documentId),
Revision.findByPk(event.modelId),
Team.findByPk(event.teamId),
]);
if (!document || !team || !revision || !collection) {
return;
}
await this.createDocumentSubscriptions(document, event);
const recipients = await this.getDocumentNotificationRecipients(
document,
"documents.update"
);
// generate the diff html for the email
const before = await revision.previous();
let content = DocumentHelper.toEmailDiff(before, revision, {
includeTitle: false,
centered: false,
});
content = await DocumentHelper.attachmentsToSignedUrls(
content,
event.teamId,
86400 * 4
);
for (const recipient of recipients) {
const notify = await this.shouldNotify(document, recipient.user);
if (notify) {
const notification = await Notification.create({
event: event.name,
userId: recipient.user.id,
actorId: document.updatedBy.id,
teamId: team.id,
documentId: document.id,
});
await DocumentNotificationEmail.schedule(
{
to: recipient.user.email,
eventName: "updated",
documentId: document.id,
teamUrl: team.url,
actorName: document.updatedBy.name,
collectionName: collection.name,
unsubscribeUrl: recipient.unsubscribeUrl,
content,
},
{ notificationId: notification.id }
);
}
}
}
async collectionCreated(event: CollectionEvent) {
const collection = await Collection.scope("withUser").findByPk(
event.collectionId
@@ -263,7 +326,18 @@ export default class NotificationsProcessor extends BaseProcessor {
});
if (notification) {
return false;
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