Return relevant context in search results returned to slack slash command

This commit is contained in:
Tom Moor
2018-08-05 00:34:08 -07:00
parent 71830d7c77
commit fc7373a6f5
4 changed files with 19 additions and 7 deletions

View File

@ -71,7 +71,7 @@ router.post('hooks.slack', async ctx => {
if (results.length) { if (results.length) {
const attachments = []; const attachments = [];
for (const result of results) { for (const result of results) {
attachments.push(presentSlackAttachment(result.document)); attachments.push(presentSlackAttachment(result.document, result.context));
} }
ctx.body = { ctx.body = {

View File

@ -3,6 +3,7 @@ import TestServer from 'fetch-test-server';
import app from '..'; import app from '..';
import { Authentication } from '../models'; import { Authentication } from '../models';
import { flushdb, seed } from '../test/support'; import { flushdb, seed } from '../test/support';
import { buildDocument } from '../test/factories';
import * as Slack from '../slack'; import * as Slack from '../slack';
const server = new TestServer(app.callback()); const server = new TestServer(app.callback());
@ -65,19 +66,26 @@ describe('#hooks.slack', async () => {
}); });
it('should return search results', async () => { it('should return search results', async () => {
const { user, document, collection } = await seed(); const { user, collection } = await seed();
const document = await buildDocument({
title: 'This title contains a search term',
userId: user.id,
teamId: user.teamId,
});
const res = await server.post('/api/hooks.slack', { const res = await server.post('/api/hooks.slack', {
body: { body: {
token: process.env.SLACK_VERIFICATION_TOKEN, token: process.env.SLACK_VERIFICATION_TOKEN,
user_id: user.serviceId, user_id: user.serviceId,
text: document.title, text: 'contains',
}, },
}); });
const body = await res.json(); const body = await res.json();
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
expect(body.attachments.length).toEqual(1); expect(body.attachments.length).toEqual(1);
expect(body.attachments[0].title).toEqual(document.title); expect(body.attachments[0].title).toEqual(document.title);
expect(body.attachments[0].text).toEqual(
'This title *contains* a search term'
);
expect(body.attachments[0].footer).toEqual(collection.name); expect(body.attachments[0].footer).toEqual(collection.name);
}); });

View File

@ -208,7 +208,7 @@ Document.searchForUser = async (
SELECT SELECT
id, id,
ts_rank(documents."searchVector", plainto_tsquery('english', :query)) as "searchRanking", ts_rank(documents."searchVector", plainto_tsquery('english', :query)) as "searchRanking",
ts_headline('english', "text", plainto_tsquery('english', :query), 'MaxFragments=1, MinWords=20, MaxWords=35') as "searchContext" ts_headline('english', "text", plainto_tsquery('english', :query), 'MaxFragments=1, MinWords=20, MaxWords=30') as "searchContext"
FROM documents FROM documents
WHERE "searchVector" @@ plainto_tsquery('english', :query) AND WHERE "searchVector" @@ plainto_tsquery('english', :query) AND
"teamId" = '${user.teamId}'::uuid AND "teamId" = '${user.teamId}'::uuid AND

View File

@ -1,13 +1,17 @@
// @flow // @flow
import { Document } from '../models'; import { Document } from '../models';
function present(document: Document) { function present(document: Document, context?: string) {
// the context contains <b> tags around search terms, we convert them here
// to the markdown format that slack expects to receive.
const text = context ? context.replace(/<\/?b>/, '*') : document.getSummary();
return { return {
color: document.collection.color, color: document.collection.color,
title: document.title, title: document.title,
title_link: `${process.env.URL}${document.getUrl()}`, title_link: `${process.env.URL}${document.getUrl()}`,
footer: document.collection.name, footer: document.collection.name,
text: document.getSummary(), text,
ts: document.getTimestamp(), ts: document.getTimestamp(),
}; };
} }