💚
This commit is contained in:
@ -71,7 +71,16 @@ 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, result.context));
|
const queryIsInTitle = !!result.document.title
|
||||||
|
.toLowerCase()
|
||||||
|
.match(text.toLowerCase());
|
||||||
|
|
||||||
|
attachments.push(
|
||||||
|
presentSlackAttachment(
|
||||||
|
result.document,
|
||||||
|
queryIsInTitle ? undefined : result.context
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
|
@ -3,7 +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 { buildDocument, buildUser } 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,8 +65,8 @@ describe('#hooks.slack', async () => {
|
|||||||
expect(body.attachments).toEqual(undefined);
|
expect(body.attachments).toEqual(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return search results', async () => {
|
it('should return search results with summary if query is in title', async () => {
|
||||||
const { user, collection } = await seed();
|
const user = await buildUser();
|
||||||
const document = await buildDocument({
|
const document = await buildDocument({
|
||||||
title: 'This title contains a search term',
|
title: 'This title contains a search term',
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
@ -83,10 +83,30 @@ describe('#hooks.slack', async () => {
|
|||||||
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(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(
|
expect(body.attachments[0].text).toEqual(
|
||||||
'This title *contains* a search term'
|
'This title *contains* a search term'
|
||||||
);
|
);
|
||||||
expect(body.attachments[0].footer).toEqual(collection.name);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if unknown user', async () => {
|
it('should error if unknown user', async () => {
|
||||||
|
@ -4,7 +4,9 @@ import { Document } from '../models';
|
|||||||
function present(document: Document, context?: string) {
|
function present(document: Document, context?: string) {
|
||||||
// the context contains <b> tags around search terms, we convert them here
|
// the context contains <b> tags around search terms, we convert them here
|
||||||
// to the markdown format that slack expects to receive.
|
// 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 {
|
return {
|
||||||
color: document.collection.color,
|
color: document.collection.color,
|
||||||
|
Reference in New Issue
Block a user