fix: Match search requests from Slack using Integration for non-Slack teams (#1599)
* Match slack hook requests to integration
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
Team,
|
||||
Collection,
|
||||
SearchQuery,
|
||||
Integration,
|
||||
} from "../models";
|
||||
import { presentSlackAttachment } from "../presenters";
|
||||
import * as Slack from "../slack";
|
||||
@@ -130,9 +131,45 @@ router.post("hooks.slack", async (ctx) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const team = await Team.findOne({
|
||||
let user;
|
||||
|
||||
// attempt to find the corresponding team for this request based on the team_id
|
||||
let team = await Team.findOne({
|
||||
where: { slackId: team_id },
|
||||
});
|
||||
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.
|
||||
if (!team) {
|
||||
ctx.body = {
|
||||
response_type: "ephemeral",
|
||||
@@ -142,17 +179,13 @@ router.post("hooks.slack", async (ctx) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
teamId: team.id,
|
||||
service: "slack",
|
||||
serviceId: user_id,
|
||||
},
|
||||
});
|
||||
|
||||
const options = {
|
||||
limit: 5,
|
||||
};
|
||||
|
||||
// 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.
|
||||
const { results, totalCount } = user
|
||||
? await Document.searchForUser(user, text, options)
|
||||
: await Document.searchForTeam(team, text, options);
|
||||
@@ -165,6 +198,9 @@ router.post("hooks.slack", async (ctx) => {
|
||||
results: totalCount,
|
||||
});
|
||||
|
||||
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
|
||||
if (results.length) {
|
||||
const attachments = [];
|
||||
for (const result of results) {
|
||||
@@ -193,12 +229,16 @@ router.post("hooks.slack", async (ctx) => {
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
text: `This is what we found for "${text}"…`,
|
||||
text: user
|
||||
? `This is what we found for "${text}"…`
|
||||
: `This is what we found for "${text}" ${haventSignedIn}…`,
|
||||
attachments,
|
||||
};
|
||||
} else {
|
||||
ctx.body = {
|
||||
text: `No results for "${text}"`,
|
||||
text: user
|
||||
? `No results for "${text}"`
|
||||
: `No results for "${text}" ${haventSignedIn}`,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import TestServer from "fetch-test-server";
|
||||
import app from "../app";
|
||||
import { Authentication, SearchQuery } from "../models";
|
||||
import * as Slack from "../slack";
|
||||
import { buildDocument } from "../test/factories";
|
||||
import { buildDocument, buildIntegration } from "../test/factories";
|
||||
import { flushdb, seed } from "../test/support";
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
@@ -212,6 +212,41 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.text).toContain("you haven’t signed in to Outline yet");
|
||||
expect(body.attachments.length).toEqual(1);
|
||||
expect(body.attachments[0].title).toEqual(document.title);
|
||||
expect(body.attachments[0].text).toEqual(
|
||||
"This title *contains* a search term"
|
||||
);
|
||||
});
|
||||
|
||||
it("should return search results with snippet for user through integration mapping", async () => {
|
||||
const { user } = await seed();
|
||||
const serviceTeamId = "slack_team_id";
|
||||
|
||||
await buildIntegration({
|
||||
teamId: user.teamId,
|
||||
settings: {
|
||||
serviceTeamId,
|
||||
},
|
||||
});
|
||||
|
||||
const document = await buildDocument({
|
||||
text: "This title contains a search term",
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const res = await server.post("/api/hooks.slack", {
|
||||
body: {
|
||||
token: process.env.SLACK_VERIFICATION_TOKEN,
|
||||
user_id: "unknown-slack-user-id",
|
||||
team_id: serviceTeamId,
|
||||
text: "contains",
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.text).toContain("you haven’t signed in to Outline yet");
|
||||
expect(body.attachments.length).toEqual(1);
|
||||
expect(body.attachments[0].title).toEqual(document.title);
|
||||
expect(body.attachments[0].text).toEqual(
|
||||
|
||||
@@ -151,7 +151,7 @@ router.get("slack.commands", auth({ required: false }), async (ctx) => {
|
||||
}
|
||||
|
||||
// this code block accounts for the root domain being unable to
|
||||
// access authentcation for subdomains. We must forward to the appropriate
|
||||
// access authentication for subdomains. We must forward to the appropriate
|
||||
// subdomain to complete the oauth flow
|
||||
if (!user) {
|
||||
if (state) {
|
||||
@@ -187,6 +187,9 @@ router.get("slack.commands", auth({ required: false }), async (ctx) => {
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
authenticationId: authentication.id,
|
||||
settings: {
|
||||
serviceTeamId: data.team_id,
|
||||
},
|
||||
});
|
||||
|
||||
ctx.redirect("/settings/integrations/slack");
|
||||
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
Group,
|
||||
GroupUser,
|
||||
Attachment,
|
||||
Authentication,
|
||||
Integration,
|
||||
} from "../models";
|
||||
|
||||
let count = 0;
|
||||
@@ -68,6 +70,33 @@ export async function buildUser(overrides: Object = {}) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function buildIntegration(overrides: Object = {}) {
|
||||
if (!overrides.teamId) {
|
||||
const team = await buildTeam();
|
||||
overrides.teamId = team.id;
|
||||
}
|
||||
|
||||
const user = await buildUser({ teamId: overrides.teamId });
|
||||
|
||||
const authentication = await Authentication.create({
|
||||
service: "slack",
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
token: "fake-access-token",
|
||||
scopes: ["example", "scopes", "here"],
|
||||
});
|
||||
|
||||
return Integration.create({
|
||||
type: "post",
|
||||
service: "slack",
|
||||
settings: {
|
||||
serviceTeamId: "slack_team_id",
|
||||
},
|
||||
authenticationId: authentication.id,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function buildCollection(overrides: Object = {}) {
|
||||
count++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user