Plugin architecture (#4861)

* wip

* Refactor, tasks, processors, routes loading

* Move Slack settings config to plugin

* Fix translations in plugins

* Move Slack auth to plugin

* test

* Move other slack-related files into plugin

* Forgot to save

* refactor
This commit is contained in:
Tom Moor
2023-02-12 13:11:30 -05:00
committed by GitHub
parent 492beedf00
commit 33afa2f029
30 changed files with 273 additions and 117 deletions

View File

@@ -1,139 +0,0 @@
import fetch from "fetch-with-proxy";
import { Op } from "sequelize";
import { IntegrationService, IntegrationType } from "@shared/types";
import env from "@server/env";
import { Document, Integration, Collection, Team } from "@server/models";
import { presentSlackAttachment } from "@server/presenters";
import {
DocumentEvent,
IntegrationEvent,
RevisionEvent,
Event,
} from "@server/types";
import BaseProcessor from "./BaseProcessor";
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":
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.url}`,
text: collection.description,
},
],
}),
});
}
async documentUpdated(event: DocumentEvent | RevisionEvent) {
// 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 && 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 (
event.name === "revisions.create" &&
document.updatedAt === document.publishedAt
) {
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 = `${document.updatedBy.name} updated a document`;
if (event.name === "documents.publish") {
text = `${document.createdBy.name} published a new document`;
}
await fetch(integration.settings.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text,
attachments: [
presentSlackAttachment(document, team, document.collection),
],
}),
});
}
}

View File

@@ -1,16 +1,28 @@
import path from "path";
import { glob } from "glob";
import Logger from "@server/logging/Logger";
import { requireDirectory } from "@server/utils/fs";
import BaseProcessor from "./BaseProcessor";
const processors = {};
requireDirectory(__dirname).forEach(([module, id]) => {
// @ts-expect-error ts-migrate(2339) FIXME: Property 'default' does not exist on type 'unknown'
const { default: Processor } = module;
if (id === "index") {
return;
requireDirectory<{ default: BaseProcessor }>(__dirname).forEach(
([module, id]) => {
if (id === "index") {
return;
}
processors[id] = module.default;
}
);
processors[id] = Processor;
});
glob
.sync("build/plugins/*/server/processors/!(*.test).js")
.forEach((filePath: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const processor = require(path.join(process.cwd(), filePath)).default;
const name = path.basename(filePath, ".js");
processors[name] = processor;
Logger.debug("processor", `Registered processor ${name}`);
});
export default processors;

View File

@@ -1,16 +1,28 @@
import path from "path";
import { glob } from "glob";
import Logger from "@server/logging/Logger";
import { requireDirectory } from "@server/utils/fs";
import BaseTask from "./BaseTask";
const tasks = {};
requireDirectory(__dirname).forEach(([module, id]) => {
// @ts-expect-error ts-migrate(2339) FIXME: Property 'default' does not exist on type 'unknown'
const { default: Task } = module;
if (id === "index") {
return;
requireDirectory<{ default: BaseTask<any> }>(__dirname).forEach(
([module, id]) => {
if (id === "index") {
return;
}
tasks[id] = module.default;
}
);
tasks[id] = Task;
});
glob
.sync("build/plugins/*/server/tasks/!(*.test).js")
.forEach((filePath: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const task = require(path.join(process.cwd(), filePath)).default;
const name = path.basename(filePath, ".js");
tasks[name] = task;
Logger.debug("task", `Registered task ${name}`);
});
export default tasks;