This commit is contained in:
Tom Moor 2018-08-05 00:51:47 -07:00
parent fc7373a6f5
commit 61b6b18148
3 changed files with 37 additions and 6 deletions

View File

@ -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 = {

View File

@ -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 () => {

View File

@ -4,7 +4,9 @@ import { Document } from '../models';
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();
const text = context
? context.replace(/<\/?b>/g, '*')
: document.getSummary();
return {
color: document.collection.color,