Files
outline/plugins/slack/server/processors/SlackProcessor.ts
T
Claude b399ffaca9 perf: Remove eager user joins from Document default scope
Every Document query previously eager-loaded the createdBy and updatedBy
User associations via the default scope, paying for two LEFT JOINs to the
wide users table on every query — including the many policy checks,
background tasks, and command paths that never read them.

The associations are now resolved only where needed:

- The document presenter loads users through a new request-scoped
  DataLoader (server/utils/loaders.ts), so a page of documents resolves
  all creator/updater rows in a single batched query, memoized per
  request. When no request context is available a shared batch-only
  (non-caching) loader is used instead.
- Consumers that only needed the actor ID now read the FK column
  directly (lastModifiedById) instead of the joined row.
- Single-document consumers (revisions.info, JSON export, Slack
  processor, unfurls) load the association explicitly or lazily.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012rKcnTfSLGCzcMq4yvmopj
2026-07-05 10:10:23 +00:00

150 lines
4.2 KiB
TypeScript

import { differenceInMilliseconds } from "date-fns";
import { Op } from "sequelize";
import { IntegrationService, IntegrationType } from "@shared/types";
import { Minute } from "@shared/utils/time";
import { Document, Integration, Collection, Team } from "@server/models";
import BaseProcessor from "@server/queues/processors/BaseProcessor";
import type {
DocumentEvent,
IntegrationEvent,
RevisionEvent,
Event,
} from "@server/types";
import fetch from "@server/utils/fetch";
import { sleep } from "@shared/utils/timers";
import env from "../env";
import { presentMessageAttachment } from "../presenters/messageAttachment";
export default class SlackProcessor extends BaseProcessor {
static applicableEvents: Event["name"][] = [
"documents.publish",
"revisions.create",
"integrations.create",
];
async perform(event: Event) {
switch (event.name) {
case "documents.publish":
case "revisions.create":
// wait a few seconds to give the document summary chance to be generated
await sleep(5000);
return this.documentUpdated(event);
case "integrations.create":
return this.integrationCreated(event);
default:
}
}
async integrationCreated(event: IntegrationEvent) {
const integration = (await Integration.findOne({
where: {
id: event.modelId,
service: IntegrationService.Slack,
type: IntegrationType.Post,
},
include: [
{
model: Collection,
required: true,
as: "collection",
},
],
})) as Integration<IntegrationType.Post>;
if (!integration) {
return;
}
const collection = integration.collection;
if (!collection) {
return;
}
await fetch(integration.settings.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text: `👋 Hey there! When documents are published or updated in the *${collection.name}* collection on ${env.APP_NAME} they will be posted to this channel!`,
attachments: [
{
color: collection.color,
title: collection.name,
title_link: `${env.URL}${collection.path}`,
text: collection.description,
},
],
}),
});
}
async documentUpdated(event: DocumentEvent | RevisionEvent) {
// never send notifications when batch importing documents
if (event.name === "documents.publish" && event.data?.source === "import") {
return;
}
const [document, team] = await Promise.all([
Document.findByPk(event.documentId),
Team.findByPk(event.teamId),
]);
if (!document || !team) {
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 integration = (await Integration.findOne({
where: {
teamId: document.teamId,
collectionId: document.collectionId,
service: IntegrationService.Slack,
type: IntegrationType.Post,
events: {
[Op.contains]: [
event.name === "revisions.create" ? "documents.update" : event.name,
],
},
},
})) as Integration<IntegrationType.Post>;
if (!integration) {
return;
}
let text: string;
if (event.name === "documents.publish") {
const createdBy = await document.$get("createdBy", { paranoid: false });
text = `${createdBy?.name} published "${document.titleWithDefault}"`;
} else {
const updatedBy = await document.$get("updatedBy", { paranoid: false });
text = `${updatedBy?.name} updated "${document.titleWithDefault}"`;
}
await fetch(integration.settings.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text,
attachments: [
presentMessageAttachment(document, team, document.collection),
],
}),
});
}
}