Files
outline/server/services/slack.js
Tom Moor 07a941a65d Websocket Support (#937)
* Atom / RSS meta link

* Spike

* Feeling good about this spike now

* Remove document.collection

* Remove koa.ctx from all presenters to make them portable outside requests

* Remove full serialized model from events
Move events.add to controllers for now, will eventually be in commands

* collections.create event
parentDocument -> parentDocumentId

* Fix up deprecated tests

* Fixed: Doc creation

* documents.move

* Handle collection deleted

* 💚

* Authorize room join requests

* Move starred data structure
Account for documents with no context on sockets

* Add socket.io-redis

* Add WEBSOCKETS_ENABLED env variable to disable websockets entirely for self hosted
New installations will default to true, existing installations to false

* 💚 No need for promise response here

* Reload notice
2019-04-17 19:11:23 -07:00

96 lines
2.4 KiB
JavaScript

// @flow
import type { Event } from '../events';
import { Document, Integration, Collection, Team } 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.modelId,
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.modelId);
if (!document) return;
// never send information on draft documents
if (!document.publishedAt) return;
const integration = await Integration.findOne({
where: {
teamId: document.teamId,
collectionId: document.collectionId,
service: 'slack',
type: 'post',
},
});
if (!integration) return;
const team = await Team.findById(document.teamId);
let text = `${document.createdBy.name} published a new document`;
if (event.name === 'documents.update') {
text = `${document.updatedBy.name} updated a document`;
}
await fetch(integration.settings.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text,
attachments: [presentSlackAttachment(document, team)],
}),
});
}
}