Files
outline/server/queues/processors/ChatNotificationsProcessor.ts
Tom Moor 6659590438 Merge remote-tracking branch 'origin/main' into tommoor/chat-provider-interface
# Conflicts:
#	plugins/slack/server/api/hooks.ts
#	plugins/slack/server/auth/slack.ts
#	plugins/slack/server/processors/SlackProcessor.ts
#	server/context.ts
#	server/routes/api/index.ts
#	shared/i18n/locales/en_US/translation.json
2026-11-25 21:27:38 -05:00

185 lines
5.0 KiB
TypeScript

import { differenceInMilliseconds } from "date-fns";
import { Op } from "sequelize";
import { IntegrationType } from "@shared/types";
import { Minute, Second } from "@shared/utils/time";
import { ChatMessageHelper } from "@server/chat/ChatMessageHelper";
import { ChatService } from "@server/chat/ChatService";
import env from "@server/env";
import { Collection, Document, Integration } from "@server/models";
import ChatPostDocumentTask from "../tasks/ChatPostDocumentTask";
import type {
DocumentEvent,
Event,
IntegrationEvent,
RevisionEvent,
} from "@server/types";
import BaseProcessor from "./BaseProcessor";
/**
* Schedules posts of document changes to the chat channels that are connected
* to a collection, on every registered chat platform.
*/
export default class ChatNotificationsProcessor extends BaseProcessor {
static applicableEvents: Event["name"][] = [
"documents.publish",
"revisions.create",
"integrations.create",
];
/**
* Only queue document events when the team has a chat post integration
* subscribed to the event.
*
* @param event the event about to be queued.
* @returns true if the event may need to be posted to a chat channel.
*/
static async shouldQueue(event: Event): Promise<boolean> {
if (event.name === "integrations.create") {
return true;
}
const eventName =
event.name === "revisions.create" ? "documents.update" : event.name;
const integrations = (
await Promise.all(
ChatService.getProviders().map((provider) =>
Integration.findEnabledByTeamIdAndService(
event.teamId,
provider.service
)
)
)
).flat();
return integrations.some(
(integration) =>
integration.type === IntegrationType.Post &&
!!integration.events?.includes(eventName)
);
}
async perform(event: Event) {
const services = ChatService.getProviders().map(
(provider) => provider.service
);
if (!services.length) {
return;
}
switch (event.name) {
case "documents.publish":
case "revisions.create":
return this.documentUpdated(event, services);
case "integrations.create":
return this.integrationCreated(event, services);
default:
}
}
private async integrationCreated(
event: IntegrationEvent,
services: string[]
) {
const integration = (await Integration.findOne({
where: {
id: event.modelId,
service: services,
type: IntegrationType.Post,
},
include: [
{
model: Collection,
required: true,
as: "collection",
},
],
})) as Integration<IntegrationType.Post> | null;
const collection = integration?.collection;
if (!integration || !collection) {
return;
}
const provider = ChatService.getProvider(integration.service);
if (!provider) {
return;
}
await provider.postMessage(integration, {
text: `👋 Hey there! When documents are published or updated in the **${ChatMessageHelper.escape(
collection.name
)}** collection on ${ChatMessageHelper.escape(
env.APP_NAME
)} they will be posted to this channel!`,
visibility: "conversation",
cards: [
{
title: collection.name,
url: `${env.URL}${collection.path}`,
text: collection.description ?? undefined,
color: collection.color ?? undefined,
},
],
});
}
private async documentUpdated(
event: DocumentEvent | RevisionEvent,
services: string[]
) {
// never send notifications when batch importing documents
if (event.name === "documents.publish" && event.data?.source === "import") {
return;
}
const document = await Document.findByPk(event.documentId);
if (!document) {
return;
}
// never send notifications for draft documents
if (!document.publishedAt) {
return;
}
// if the document was published less than a minute ago, don't send a
// separate notification.
if (
event.name === "revisions.create" &&
differenceInMilliseconds(document.updatedAt, document.publishedAt) <
Minute.ms
) {
return;
}
const eventName =
event.name === "documents.publish"
? "documents.publish"
: "documents.update";
const integrations = await Integration.findAll({
attributes: ["id"],
where: {
teamId: document.teamId,
collectionId: document.collectionId,
service: services,
type: IntegrationType.Post,
events: { [Op.contains]: [eventName] },
},
});
await Promise.all(
integrations.map((integration) =>
new ChatPostDocumentTask().schedule(
{
integrationId: integration.id,
documentId: document.id,
eventName,
},
// Gives the document summary a chance to be generated.
{ delay: 5 * Second.ms }
)
)
);
}
}