Slack hooks fixes and improvements

closes #533
This commit is contained in:
Tom Moor
2018-01-28 19:37:14 -08:00
parent 97268314fe
commit f870cd88bc
3 changed files with 11 additions and 4 deletions

View File

@ -71,13 +71,14 @@ router.post('hooks.slack', async ctx => {
color: document.collection.color, color: document.collection.color,
title: document.title, title: document.title,
title_link: `${process.env.URL}${document.getUrl()}`, title_link: `${process.env.URL}${document.getUrl()}`,
footer: document.collection.name,
text: document.getSummary(), text: document.getSummary(),
ts: new Date(document.updatedAt).getTime(), ts: document.getTimestamp(),
}); });
} }
ctx.body = { ctx.body = {
text: `This is what we found…`, text: `This is what we found for "${text}"`,
attachments, attachments,
}; };
} else { } else {

View File

@ -65,18 +65,20 @@ describe('#hooks.slack', async () => {
}); });
it('should return search results', async () => { it('should return search results', async () => {
const { user } = await seed(); const { user, document, collection } = await seed();
const res = await server.post('/api/hooks.slack', { const res = await server.post('/api/hooks.slack', {
body: { body: {
token: process.env.SLACK_VERIFICATION_TOKEN, token: process.env.SLACK_VERIFICATION_TOKEN,
user_id: user.slackId, user_id: user.slackId,
text: 'Welcome', text: document.title,
}, },
}); });
const body = await res.json(); const body = await res.json();
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].footer).toEqual(collection.name);
}); });
it('should error if unknown user', async () => { it('should error if unknown user', async () => {

View File

@ -221,6 +221,10 @@ Document.addHook('afterUpdate', model =>
// Instance methods // Instance methods
Document.prototype.getTimestamp = function() {
return Math.round(new Date(this.updatedAt).getTime() / 1000);
};
Document.prototype.getSummary = function() { Document.prototype.getSummary = function() {
const value = Markdown.deserialize(this.text); const value = Markdown.deserialize(this.text);
const plain = Plain.serialize(value); const plain = Plain.serialize(value);