Files
outline/server/services/slack/index.js
Tom Moor 44cb509ebf Post to Slack (#603)
* Migrations

* WIP: Integration model, slack perms / hooks

* So so rough it pains me. Building this new model is revealing just how much needs to be refactored

* Working connect and post

* Cleanup UI, upating documents

* Show when slack command is connected

* stash

* 💚

* Add documents.update trigger

* Authorization, tidying

* Fixed integration policy

* pick integration presenter keys
2018-04-03 20:36:25 -07:00

44 lines
1.1 KiB
JavaScript

// @flow
import type { Event } from '../../events';
import { Document, Integration } from '../../models';
import { presentSlackAttachment } from '../../presenters';
const Slack = {
on: async (event: Event) => {
if (event.name !== 'documents.publish' && event.name !== 'documents.update')
return;
const document = await Document.findById(event.model.id);
if (!document) return;
const integration = await Integration.findOne({
where: {
teamId: document.teamId,
serviceId: 'slack',
collectionId: document.atlasId,
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)],
}),
});
},
};
export default Slack;