2017-11-16 06:56:40 +00:00
|
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
|
import Router from "koa-router";
|
|
|
|
|
import { escapeRegExp } from "lodash";
|
|
|
|
|
import { AuthenticationError, InvalidRequestError } from "../errors";
|
2020-09-22 06:05:42 +00:00
|
|
|
|
import {
|
|
|
|
|
Authentication,
|
|
|
|
|
Document,
|
|
|
|
|
User,
|
|
|
|
|
Team,
|
|
|
|
|
Collection,
|
|
|
|
|
SearchQuery,
|
2020-10-21 15:53:38 +00:00
|
|
|
|
Integration,
|
2020-09-22 06:05:42 +00:00
|
|
|
|
} from "../models";
|
2020-06-20 20:59:15 +00:00
|
|
|
|
import { presentSlackAttachment } from "../presenters";
|
|
|
|
|
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
|
2020-08-09 01:53:11 +00:00
|
|
|
|
router.post("hooks.unfurl", async (ctx) => {
|
2017-12-19 03:59:29 +00:00
|
|
|
|
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) {
|
2020-06-20 20:59:15 +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({
|
2020-06-20 20:59:15 +00:00
|
|
|
|
where: { service: "slack", serviceId: event.user },
|
2018-05-29 03:31:53 +00:00
|
|
|
|
});
|
2017-12-19 03:59:29 +00:00
|
|
|
|
if (!user) return;
|
|
|
|
|
|
2019-06-23 22:49:45 +00:00
|
|
|
|
const auth = await Authentication.findOne({
|
2020-06-20 20:59:15 +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) {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
await Slack.post("chat.unfurl", {
|
2017-12-19 03:59:29 +00:00
|
|
|
|
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
|
2020-08-09 01:53:11 +00:00
|
|
|
|
router.post("hooks.interactive", async (ctx) => {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
const { payload } = ctx.body;
|
2020-06-20 20:59:15 +00:00
|
|
|
|
ctx.assertPresent(payload, "payload is required");
|
2019-02-20 06:42:13 +00:00
|
|
|
|
|
|
|
|
|
const data = JSON.parse(payload);
|
|
|
|
|
const { callback_id, token } = data;
|
2020-06-20 20:59:15 +00:00
|
|
|
|
ctx.assertPresent(token, "token is required");
|
|
|
|
|
ctx.assertPresent(callback_id, "callback_id is required");
|
2019-02-20 06:42:13 +00:00
|
|
|
|
|
2019-09-22 18:52:15 +00:00
|
|
|
|
if (token !== process.env.SLACK_VERIFICATION_TOKEN) {
|
2020-06-20 20:59:15 +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:
|
2020-06-20 20:59:15 +00:00
|
|
|
|
"Sorry, we couldn’t find an integration for your team. Head to your Outline settings to set one up.",
|
|
|
|
|
response_type: "ephemeral",
|
2019-02-20 06:42:13 +00:00
|
|
|
|
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
|
|
|
|
});
|
2020-06-20 20:59:15 +00:00
|
|
|
|
if (!document) throw new InvalidRequestError("Invalid document");
|
2019-02-20 06:42:13 +00:00
|
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
|
const collection = await Collection.findByPk(document.collectionId);
|
|
|
|
|
|
2019-02-20 06:42:13 +00:00
|
|
|
|
// respond with a public message that will be posted in the original channel
|
|
|
|
|
ctx.body = {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
response_type: "in_channel",
|
2019-02-20 06:42:13 +00:00
|
|
|
|
replace_original: false,
|
|
|
|
|
attachments: [
|
2019-10-06 01:42:03 +00:00
|
|
|
|
presentSlackAttachment(document, collection, team, document.getSummary()),
|
2019-02-20 06:42:13 +00:00
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// triggered by the /outline command in Slack
|
2020-08-09 01:53:11 +00:00
|
|
|
|
router.post("hooks.slack", async (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
const { token, team_id, user_id, text = "" } = ctx.body;
|
|
|
|
|
ctx.assertPresent(token, "token is required");
|
|
|
|
|
ctx.assertPresent(team_id, "team_id is required");
|
|
|
|
|
ctx.assertPresent(user_id, "user_id is required");
|
2016-08-23 06:37:01 +00:00
|
|
|
|
|
2019-06-26 03:43:59 +00:00
|
|
|
|
if (token !== process.env.SLACK_VERIFICATION_TOKEN) {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
throw new AuthenticationError("Invalid verification token");
|
2019-06-26 03:43:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle "help" command or no input
|
2020-06-20 20:59:15 +00:00
|
|
|
|
if (text.trim() === "help" || !text.trim()) {
|
2019-06-26 03:43:59 +00:00
|
|
|
|
ctx.body = {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
response_type: "ephemeral",
|
|
|
|
|
text: "How to use /outline",
|
2019-06-26 03:43:59 +00:00
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
text:
|
2020-07-18 20:17:10 +00:00
|
|
|
|
"To search your knowledge base use `/outline keyword`. \nYou’ve already learned how to get help with `/outline help`.",
|
2019-06-26 03:43:59 +00:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-23 06:37:01 +00:00
|
|
|
|
|
2020-10-21 15:53:38 +00:00
|
|
|
|
let user;
|
|
|
|
|
|
|
|
|
|
// attempt to find the corresponding team for this request based on the team_id
|
|
|
|
|
let team = await Team.findOne({
|
2019-09-22 18:52:15 +00:00
|
|
|
|
where: { slackId: team_id },
|
2016-08-23 06:37:01 +00:00
|
|
|
|
});
|
2020-10-21 15:53:38 +00:00
|
|
|
|
if (team) {
|
|
|
|
|
user = await User.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
service: "slack",
|
|
|
|
|
serviceId: user_id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// If we couldn't find a team it's still possible that the request is from
|
|
|
|
|
// a team that authenticated with a different service, but connected Slack
|
|
|
|
|
// via integration
|
|
|
|
|
const integration = await Integration.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
settings: {
|
|
|
|
|
serviceTeamId: team_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Team,
|
|
|
|
|
as: "team",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (integration) {
|
|
|
|
|
team = integration.team;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This should be super rare, how does someone end up being able to make a valid
|
|
|
|
|
// request from Slack that connects to no teams in Outline.
|
2019-09-22 18:52:15 +00:00
|
|
|
|
if (!team) {
|
2019-02-20 06:42:13 +00:00
|
|
|
|
ctx.body = {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
response_type: "ephemeral",
|
2019-09-22 18:52:15 +00:00
|
|
|
|
text:
|
2020-06-20 20:59:15 +00:00
|
|
|
|
"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 options = {
|
|
|
|
|
limit: 5,
|
|
|
|
|
};
|
2020-10-21 15:53:38 +00:00
|
|
|
|
|
|
|
|
|
// If we were able to map the request to a user then we can use their permissions
|
|
|
|
|
// to load more documents based on the collections they have access to. Otherwise
|
|
|
|
|
// just a generic search against team-visible documents is allowed.
|
2020-09-22 06:05:42 +00:00
|
|
|
|
const { results, totalCount } = user
|
2019-09-22 18:52:15 +00:00
|
|
|
|
? await Document.searchForUser(user, text, options)
|
|
|
|
|
: await Document.searchForTeam(team, text, options);
|
|
|
|
|
|
2020-09-22 06:05:42 +00:00
|
|
|
|
SearchQuery.create({
|
|
|
|
|
userId: user ? user.id : null,
|
|
|
|
|
teamId: team.id,
|
|
|
|
|
source: "slack",
|
|
|
|
|
query: text,
|
|
|
|
|
results: totalCount,
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-21 15:53:38 +00:00
|
|
|
|
const haventSignedIn = `(It looks like you haven’t signed in to Outline yet, so results may be limited)`;
|
|
|
|
|
|
|
|
|
|
// Map search results to the format expected by the Slack API
|
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-10-06 01:42:03 +00:00
|
|
|
|
result.document.collection,
|
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
|
|
|
|
|
? [
|
|
|
|
|
{
|
2020-06-20 20:59:15 +00:00
|
|
|
|
name: "post",
|
|
|
|
|
text: "Post to Channel",
|
|
|
|
|
type: "button",
|
2019-02-20 06:42:13 +00:00
|
|
|
|
value: result.document.id,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
: undefined
|
2018-08-05 07:51:47 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
2017-11-20 07:49:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.body = {
|
2020-10-21 15:53:38 +00:00
|
|
|
|
text: user
|
|
|
|
|
? `This is what we found for "${text}"…`
|
|
|
|
|
: `This is what we found for "${text}" ${haventSignedIn}…`,
|
2018-01-29 01:00:20 +00:00
|
|
|
|
attachments,
|
2017-11-20 07:49:27 +00:00
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
ctx.body = {
|
2020-10-21 15:53:38 +00:00
|
|
|
|
text: user
|
|
|
|
|
? `No results for "${text}"`
|
|
|
|
|
: `No results for "${text}" ${haventSignedIn}`,
|
2017-11-20 07:49:27 +00:00
|
|
|
|
};
|
2016-08-23 06:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default router;
|