fix: Move notifications to be revision driven (#2709)
This commit is contained in:
parent
b6a058147e
commit
5f00e1394d
|
@ -10,13 +10,18 @@ import {
|
|||
NotificationSetting,
|
||||
} from "../../models";
|
||||
import { Op } from "../../sequelize";
|
||||
import type { DocumentEvent, CollectionEvent, Event } from "../../types";
|
||||
import type {
|
||||
DocumentEvent,
|
||||
CollectionEvent,
|
||||
RevisionEvent,
|
||||
Event,
|
||||
} from "../../types";
|
||||
|
||||
export default class NotificationsProcessor {
|
||||
async on(event: Event) {
|
||||
switch (event.name) {
|
||||
case "documents.publish":
|
||||
case "documents.update.debounced":
|
||||
case "revisions.create":
|
||||
return this.documentUpdated(event);
|
||||
case "collections.create":
|
||||
return this.collectionCreated(event);
|
||||
|
@ -24,18 +29,17 @@ export default class NotificationsProcessor {
|
|||
}
|
||||
}
|
||||
|
||||
async documentUpdated(event: DocumentEvent) {
|
||||
async documentUpdated(event: DocumentEvent | RevisionEvent) {
|
||||
// never send notifications when batch importing documents
|
||||
if (event.data && event.data.source === "import") return;
|
||||
|
||||
const document = await Document.findByPk(event.documentId);
|
||||
if (!document) return;
|
||||
const [document, team] = await Promise.all([
|
||||
Document.findByPk(event.documentId),
|
||||
Team.findByPk(event.teamId),
|
||||
]);
|
||||
|
||||
if (!document || !team || !document.collection) return;
|
||||
const { collection } = document;
|
||||
if (!collection) return;
|
||||
|
||||
const team = await Team.findByPk(document.teamId);
|
||||
if (!team) return;
|
||||
|
||||
const notificationSettings = await NotificationSetting.findAll({
|
||||
where: {
|
||||
|
|
|
@ -93,8 +93,8 @@ describe("documents.publish", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("documents.update.debounced", () => {
|
||||
test("should send a notification to other collaborator", async () => {
|
||||
describe("revisions.create", () => {
|
||||
test("should send a notification to other collaborators", async () => {
|
||||
const document = await buildDocument();
|
||||
const collaborator = await buildUser({ teamId: document.teamId });
|
||||
document.collaboratorIds = [collaborator.id];
|
||||
|
@ -107,7 +107,7 @@ describe("documents.update.debounced", () => {
|
|||
});
|
||||
|
||||
await Notifications.on({
|
||||
name: "documents.update.debounced",
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
|
@ -132,7 +132,7 @@ describe("documents.update.debounced", () => {
|
|||
await View.touch(document.id, collaborator.id, true);
|
||||
|
||||
await Notifications.on({
|
||||
name: "documents.update.debounced",
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
|
@ -156,7 +156,7 @@ describe("documents.update.debounced", () => {
|
|||
});
|
||||
|
||||
await Notifications.on({
|
||||
name: "documents.update.debounced",
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
|
|
|
@ -2,13 +2,18 @@
|
|||
import fetch from "fetch-with-proxy";
|
||||
import { Document, Integration, Collection, Team } from "../../models";
|
||||
import { presentSlackAttachment } from "../../presenters";
|
||||
import type { DocumentEvent, IntegrationEvent, Event } from "../../types";
|
||||
import type {
|
||||
DocumentEvent,
|
||||
IntegrationEvent,
|
||||
RevisionEvent,
|
||||
Event,
|
||||
} from "../../types";
|
||||
|
||||
export default class SlackProcessor {
|
||||
async on(event: Event) {
|
||||
switch (event.name) {
|
||||
case "documents.publish":
|
||||
case "documents.update.debounced":
|
||||
case "revisions.create":
|
||||
return this.documentUpdated(event);
|
||||
case "integrations.create":
|
||||
return this.integrationCreated(event);
|
||||
|
@ -55,11 +60,15 @@ export default class SlackProcessor {
|
|||
});
|
||||
}
|
||||
|
||||
async documentUpdated(event: DocumentEvent) {
|
||||
async documentUpdated(event: DocumentEvent | RevisionEvent) {
|
||||
// never send notifications when batch importing documents
|
||||
if (event.data && event.data.source === "import") return;
|
||||
|
||||
const document = await Document.findByPk(event.documentId);
|
||||
const [document, team] = await Promise.all([
|
||||
Document.findByPk(event.documentId),
|
||||
Team.findByPk(event.teamId),
|
||||
]);
|
||||
|
||||
if (!document) return;
|
||||
|
||||
// never send notifications for draft documents
|
||||
|
@ -75,8 +84,6 @@ export default class SlackProcessor {
|
|||
});
|
||||
if (!integration) return;
|
||||
|
||||
const team = await Team.findByPk(document.teamId);
|
||||
|
||||
let text = `${document.updatedBy.name} updated a document`;
|
||||
|
||||
if (event.name === "documents.publish") {
|
||||
|
|
|
@ -1048,6 +1048,7 @@ router.post("documents.update", auth(), async (ctx) => {
|
|||
|
||||
document.lastModifiedById = user.id;
|
||||
const { collection } = document;
|
||||
const changed = document.changed();
|
||||
|
||||
let transaction;
|
||||
try {
|
||||
|
@ -1066,30 +1067,32 @@ router.post("documents.update", auth(), async (ctx) => {
|
|||
throw err;
|
||||
}
|
||||
|
||||
if (publish) {
|
||||
await Event.create({
|
||||
name: "documents.publish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
data: { title: document.title },
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
} else {
|
||||
await Event.create({
|
||||
name: "documents.update",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
data: {
|
||||
autosave,
|
||||
done,
|
||||
title: document.title,
|
||||
},
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
if (changed) {
|
||||
if (publish) {
|
||||
await Event.create({
|
||||
name: "documents.publish",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
data: { title: document.title },
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
} else {
|
||||
await Event.create({
|
||||
name: "documents.update",
|
||||
documentId: document.id,
|
||||
collectionId: document.collectionId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
data: {
|
||||
autosave,
|
||||
done,
|
||||
title: document.title,
|
||||
},
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (document.title !== previousTitle) {
|
||||
|
|
Reference in New Issue