diff --git a/server/api/hooks.js b/server/api/hooks.js index 6afe7133..3c548f93 100644 --- a/server/api/hooks.js +++ b/server/api/hooks.js @@ -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 = { diff --git a/server/api/hooks.test.js b/server/api/hooks.test.js index 3fc9b36f..14ab1bca 100644 --- a/server/api/hooks.test.js +++ b/server/api/hooks.test.js @@ -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); }); diff --git a/server/models/Document.js b/server/models/Document.js index ff605c2d..de17335d 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -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 diff --git a/server/presenters/slackAttachment.js b/server/presenters/slackAttachment.js index d1f7c9f0..2917ffd2 100644 --- a/server/presenters/slackAttachment.js +++ b/server/presenters/slackAttachment.js @@ -1,13 +1,17 @@ // @flow import { Document } from '../models'; -function present(document: Document) { +function present(document: Document, context?: string) { + // the context contains 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(), }; }