Base model refactor (#810)
* Big upgrades * WIP: Stash * Stash, 30 flow errors left * Downgrade mobx * WIP * When I understand the difference between class and instance methods * 💚 * Fixes: File import Model saving edge cases pinning and starring docs Collection editing Upgrade mobx devtools * Notification settings saving works * Disabled settings * Document mailer * Working notifications * Colletion created notification Ensure not notified for own actions * Tidy up * Document updated event only for document creation Add indexes Notification setting on user creation * Commentary * Fixed: Notification setting on signup * Fix document move / duplicate stale data Add BaseModel.refresh method * Fixes: Title in sidebar not updated after editing document * 💚 * Improve / restore error handling Better handle offline errors * 👕
This commit is contained in:
90
server/services/slack.js
Normal file
90
server/services/slack.js
Normal file
@@ -0,0 +1,90 @@
|
||||
// @flow
|
||||
import type { Event } from '../events';
|
||||
import { Document, Integration, Collection } from '../models';
|
||||
import { presentSlackAttachment } from '../presenters';
|
||||
|
||||
export default class Slack {
|
||||
async on(event: Event) {
|
||||
switch (event.name) {
|
||||
case 'documents.publish':
|
||||
case 'documents.update':
|
||||
return this.documentUpdated(event);
|
||||
case 'integrations.create':
|
||||
return this.integrationCreated(event);
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
async integrationCreated(event: Event) {
|
||||
const integration = await Integration.findOne({
|
||||
where: {
|
||||
id: event.model.id,
|
||||
service: 'slack',
|
||||
type: 'post',
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: Collection,
|
||||
required: true,
|
||||
as: 'collection',
|
||||
},
|
||||
],
|
||||
});
|
||||
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 Outline they will be posted to this channel!`,
|
||||
attachments: [
|
||||
{
|
||||
color: collection.color,
|
||||
title: collection.name,
|
||||
title_link: `${process.env.URL}${collection.url}`,
|
||||
text: collection.description,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
async documentUpdated(event: Event) {
|
||||
const document = await Document.findById(event.model.id);
|
||||
if (!document) return;
|
||||
|
||||
const integration = await Integration.findOne({
|
||||
where: {
|
||||
teamId: document.teamId,
|
||||
collectionId: document.collectionId,
|
||||
service: 'slack',
|
||||
type: 'post',
|
||||
},
|
||||
});
|
||||
if (!integration) return;
|
||||
|
||||
let text = `${document.createdBy.name} published a new document`;
|
||||
|
||||
if (event.name === 'documents.update') {
|
||||
text = `${document.createdBy.name} updated a document`;
|
||||
}
|
||||
|
||||
await fetch(integration.settings.url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text,
|
||||
attachments: [presentSlackAttachment(document)],
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user