2017-11-16 06:56:40 +00:00
|
|
|
|
// @flow
|
2016-08-23 06:37:01 +00:00
|
|
|
|
import Router from 'koa-router';
|
2019-01-12 03:41:19 +00:00
|
|
|
|
import { escapeRegExp } from 'lodash';
|
2018-02-25 04:44:13 +00:00
|
|
|
|
import { AuthenticationError, InvalidRequestError } from '../errors';
|
2019-01-06 08:05:50 +00:00
|
|
|
|
import { Authentication, Document, User, Team } from '../models';
|
2018-04-04 03:36:25 +00:00
|
|
|
|
import { presentSlackAttachment } from '../presenters';
|
2017-12-19 03:59:29 +00:00
|
|
|
|
import * as Slack from '../slack';
|
2016-08-23 06:37:01 +00:00
|
|
|
|
const router = new Router();
|
|
|
|
|
|
2019-02-20 06:42:13 +00:00
|
|
|
|
// triggered by a user posting a getoutline.com link in Slack
|
2017-12-19 03:59:29 +00:00
|
|
|
|
router.post('hooks.unfurl', async ctx => {
|
|
|
|
|
const { challenge, token, event } = ctx.body;
|
|
|
|
|
if (challenge) return (ctx.body = ctx.body.challenge);
|
|
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
|
if (token !== process.env.SLACK_VERIFICATION_TOKEN) {
|
2018-02-25 04:44:13 +00:00
|
|
|
|
throw new AuthenticationError('Invalid token');
|
2019-06-23 22:49:45 +00:00
|
|
|
|
}
|
2017-12-19 03:59:29 +00:00
|
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
|
const user = await User.findOne({
|
2018-05-29 03:31:53 +00:00
|
|
|
|
where: { service: 'slack', serviceId: event.user },
|
|
|
|
|
});
|
2017-12-19 03:59:29 +00:00
|
|
|
|
if (!user) return;
|
|
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
|
const auth = await Authentication.findOne({
|
2018-06-01 19:13:05 +00:00
|
|
|
|
where: { service: 'slack', teamId: user.teamId },
|
2017-12-19 03:59:29 +00:00
|
|
|
|
});
|
|
|
|
|
if (!auth) return;
|
|
|
|
|
|
|
|
|
|
// get content for unfurled links
|
|
|
|
|
let unfurls = {};
|
|
|
|
|
for (let link of event.links) {
|
|
|
|
|
const id = link.url.substr(link.url.lastIndexOf('/') + 1);
|
2019-06-23 22:49:45 +00:00
|
|
|
|
const doc = await Document.findByPk(id);
|
2017-12-19 03:59:29 +00:00
|
|
|
|
if (!doc || doc.teamId !== user.teamId) continue;
|
|
|
|
|
|
|
|
|
|
unfurls[link.url] = {
|
|
|
|
|
title: doc.title,
|
|
|
|
|
text: doc.getSummary(),
|
|
|
|
|
color: doc.collection.color,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Slack.post('chat.unfurl', {
|
|
|
|
|
token: auth.token,
|
|
|
|
|
channel: event.channel,
|
|
|
|
|
ts: event.message_ts,
|
|
|
|
|
unfurls,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-20 06:42:13 +00:00
|
|
|
|
// triggered by interactions with actions, dialogs, message buttons in Slack
|
|
|
|
|
router.post('hooks.interactive', async ctx => {
|
|
|
|
|
const { payload } = ctx.body;
|
|
|
|
|
ctx.assertPresent(payload, 'payload is required');
|
|
|
|
|
|
|
|
|
|
const data = JSON.parse(payload);
|
|
|
|
|
const { callback_id, token } = data;
|
|
|
|
|
ctx.assertPresent(token, 'token is required');
|
|
|
|
|
ctx.assertPresent(callback_id, 'callback_id is required');
|
|
|
|
|
|
2019-09-22 18:52:15 +00:00
|
|
|
|
if (token !== process.env.SLACK_VERIFICATION_TOKEN) {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
throw new AuthenticationError('Invalid verification token');
|
2019-09-22 18:52:15 +00:00
|
|
|
|
}
|
2019-02-20 06:42:13 +00:00
|
|
|
|
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const team = await Team.findOne({
|
|
|
|
|
where: { slackId: data.team.id },
|
2019-02-20 06:42:13 +00:00
|
|
|
|
});
|
2019-09-22 18:52:15 +00:00
|
|
|
|
|
|
|
|
|
if (!team) {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
ctx.body = {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
text:
|
|
|
|
|
'Sorry, we couldn’t find an integration for your team. Head to your Outline settings to set one up.',
|
2019-02-20 06:42:13 +00:00
|
|
|
|
response_type: 'ephemeral',
|
|
|
|
|
replace_original: false,
|
|
|
|
|
};
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we find the document based on the users teamId to ensure access
|
2019-06-23 22:49:45 +00:00
|
|
|
|
const document = await Document.findOne({
|
2019-09-22 18:52:15 +00:00
|
|
|
|
where: {
|
|
|
|
|
id: data.callback_id,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
},
|
2019-02-20 06:42:13 +00:00
|
|
|
|
});
|
|
|
|
|
if (!document) throw new InvalidRequestError('Invalid document');
|
|
|
|
|
|
|
|
|
|
// respond with a public message that will be posted in the original channel
|
|
|
|
|
ctx.body = {
|
|
|
|
|
response_type: 'in_channel',
|
|
|
|
|
replace_original: false,
|
|
|
|
|
attachments: [
|
|
|
|
|
presentSlackAttachment(document, team, document.getSummary()),
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// triggered by the /outline command in Slack
|
2017-04-27 04:47:03 +00:00
|
|
|
|
router.post('hooks.slack', async ctx => {
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const { token, team_id, user_id, text } = ctx.body;
|
2016-08-23 06:37:01 +00:00
|
|
|
|
ctx.assertPresent(token, 'token is required');
|
2019-09-22 18:52:15 +00:00
|
|
|
|
ctx.assertPresent(team_id, 'team_id is required');
|
2016-08-23 06:37:01 +00:00
|
|
|
|
ctx.assertPresent(user_id, 'user_id is required');
|
|
|
|
|
|
2019-06-26 03:43:59 +00:00
|
|
|
|
if (token !== process.env.SLACK_VERIFICATION_TOKEN) {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
throw new AuthenticationError('Invalid verification token');
|
2019-06-26 03:43:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle "help" command or no input
|
|
|
|
|
if (text.trim() === 'help' || !text.trim()) {
|
|
|
|
|
ctx.body = {
|
|
|
|
|
response_type: 'ephemeral',
|
|
|
|
|
text: 'How to use /outline',
|
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
text:
|
|
|
|
|
'To search your knowledgebase use `/outline keyword`. \nYou’ve already learned how to get help with `/outline help`.',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-23 06:37:01 +00:00
|
|
|
|
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const team = await Team.findOne({
|
|
|
|
|
where: { slackId: team_id },
|
2016-08-23 06:37:01 +00:00
|
|
|
|
});
|
2019-09-22 18:52:15 +00:00
|
|
|
|
if (!team) {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
ctx.body = {
|
2019-06-26 03:43:59 +00:00
|
|
|
|
response_type: 'ephemeral',
|
2019-09-22 18:52:15 +00:00
|
|
|
|
text:
|
|
|
|
|
'Sorry, we couldn’t find an integration for your team. Head to your Outline settings to set one up.',
|
2019-02-20 06:42:13 +00:00
|
|
|
|
};
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-23 06:37:01 +00:00
|
|
|
|
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const user = await User.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
service: 'slack',
|
|
|
|
|
serviceId: user_id,
|
|
|
|
|
},
|
2016-08-23 06:37:01 +00:00
|
|
|
|
});
|
|
|
|
|
|
2019-09-22 18:52:15 +00:00
|
|
|
|
const options = {
|
|
|
|
|
limit: 5,
|
|
|
|
|
};
|
|
|
|
|
const results = user
|
|
|
|
|
? await Document.searchForUser(user, text, options)
|
|
|
|
|
: await Document.searchForTeam(team, text, options);
|
|
|
|
|
|
2018-08-05 01:32:56 +00:00
|
|
|
|
if (results.length) {
|
2018-01-29 01:00:20 +00:00
|
|
|
|
const attachments = [];
|
2018-08-05 01:32:56 +00:00
|
|
|
|
for (const result of results) {
|
2018-08-05 07:51:47 +00:00
|
|
|
|
const queryIsInTitle = !!result.document.title
|
|
|
|
|
.toLowerCase()
|
2019-01-12 03:41:19 +00:00
|
|
|
|
.match(escapeRegExp(text.toLowerCase()));
|
2018-08-05 07:51:47 +00:00
|
|
|
|
|
|
|
|
|
attachments.push(
|
|
|
|
|
presentSlackAttachment(
|
|
|
|
|
result.document,
|
2019-01-06 08:05:50 +00:00
|
|
|
|
team,
|
2019-02-20 06:42:13 +00:00
|
|
|
|
queryIsInTitle ? undefined : result.context,
|
|
|
|
|
process.env.SLACK_MESSAGE_ACTIONS
|
|
|
|
|
? [
|
|
|
|
|
{
|
|
|
|
|
name: 'post',
|
|
|
|
|
text: 'Post to Channel',
|
|
|
|
|
type: 'button',
|
|
|
|
|
value: result.document.id,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
: undefined
|
2018-08-05 07:51:47 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
2017-11-20 07:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
2018-01-29 03:37:14 +00:00
|
|
|
|
text: `This is what we found for "${text}"…`,
|
2018-01-29 01:00:20 +00:00
|
|
|
|
attachments,
|
2017-11-20 07:49:27 +00:00
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
ctx.body = {
|
2018-01-29 01:00:20 +00:00
|
|
|
|
text: `No results for "${text}"`,
|
2017-11-20 07:49:27 +00:00
|
|
|
|
};
|
2016-08-23 06:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default router;
|