Return relevant context in search results returned to slack slash command
This commit is contained in:
@ -71,7 +71,7 @@ router.post('hooks.slack', async ctx => {
|
||||
if (results.length) {
|
||||
const attachments = [];
|
||||
for (const result of results) {
|
||||
attachments.push(presentSlackAttachment(result.document));
|
||||
attachments.push(presentSlackAttachment(result.document, result.context));
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
|
@ -3,6 +3,7 @@ import TestServer from 'fetch-test-server';
|
||||
import app from '..';
|
||||
import { Authentication } from '../models';
|
||||
import { flushdb, seed } from '../test/support';
|
||||
import { buildDocument } from '../test/factories';
|
||||
import * as Slack from '../slack';
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
@ -65,19 +66,26 @@ describe('#hooks.slack', 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', {
|
||||
body: {
|
||||
token: process.env.SLACK_VERIFICATION_TOKEN,
|
||||
user_id: user.serviceId,
|
||||
text: document.title,
|
||||
text: 'contains',
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
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'
|
||||
);
|
||||
expect(body.attachments[0].footer).toEqual(collection.name);
|
||||
});
|
||||
|
||||
|
@ -208,7 +208,7 @@ Document.searchForUser = async (
|
||||
SELECT
|
||||
id,
|
||||
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
|
||||
WHERE "searchVector" @@ plainto_tsquery('english', :query) AND
|
||||
"teamId" = '${user.teamId}'::uuid AND
|
||||
|
@ -1,13 +1,17 @@
|
||||
// @flow
|
||||
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 {
|
||||
color: document.collection.color,
|
||||
title: document.title,
|
||||
title_link: `${process.env.URL}${document.getUrl()}`,
|
||||
footer: document.collection.name,
|
||||
text: document.getSummary(),
|
||||
text,
|
||||
ts: document.getTimestamp(),
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user