diff --git a/server/api/hooks.js b/server/api/hooks.js index 3c548f93..4280606e 100644 --- a/server/api/hooks.js +++ b/server/api/hooks.js @@ -71,7 +71,16 @@ router.post('hooks.slack', async ctx => { if (results.length) { const attachments = []; for (const result of results) { - attachments.push(presentSlackAttachment(result.document, result.context)); + const queryIsInTitle = !!result.document.title + .toLowerCase() + .match(text.toLowerCase()); + + attachments.push( + presentSlackAttachment( + result.document, + queryIsInTitle ? undefined : result.context + ) + ); } ctx.body = { diff --git a/server/api/hooks.test.js b/server/api/hooks.test.js index 14ab1bca..27b7a415 100644 --- a/server/api/hooks.test.js +++ b/server/api/hooks.test.js @@ -3,7 +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 { buildDocument, buildUser } from '../test/factories'; import * as Slack from '../slack'; const server = new TestServer(app.callback()); @@ -65,8 +65,8 @@ describe('#hooks.slack', async () => { expect(body.attachments).toEqual(undefined); }); - it('should return search results', async () => { - const { user, collection } = await seed(); + it('should return search results with summary if query is in title', async () => { + const user = await buildUser(); const document = await buildDocument({ title: 'This title contains a search term', userId: user.id, @@ -83,10 +83,30 @@ describe('#hooks.slack', async () => { 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(document.getSummary()); + }); + + it('should return search results with snippet if query is in text', async () => { + const user = await buildUser(); + 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: user.serviceId, + 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); }); it('should error if unknown user', async () => { diff --git a/server/presenters/slackAttachment.js b/server/presenters/slackAttachment.js index 2917ffd2..c782f092 100644 --- a/server/presenters/slackAttachment.js +++ b/server/presenters/slackAttachment.js @@ -4,7 +4,9 @@ import { Document } from '../models'; 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(); + const text = context + ? context.replace(/<\/?b>/g, '*') + : document.getSummary(); return { color: document.collection.color,