Files
talk/plugins/talk-plugin-slack-notifications/server/hooks.js
T
Wyatt Johnson 735ecbcef3 linting updates
2018-02-23 14:00:07 -07:00

48 lines
1.5 KiB
JavaScript

const fetch = require('node-fetch');
const { SLACK_WEBHOOK_URL, SLACK_WEBHOOK_TIMEOUT } = require('./config');
const debug = require('debug')('talk:plugin:slack-notifications');
// We don't add the hooks during _test_ as the Slack API is not available.
if (process.env.NODE_ENV === 'test') {
return null;
}
module.exports = {
RootMutation: {
createComment: {
async post(_, _, context, info, result) {
debug(`Posting notification to Slack webhook: ${SLACK_WEBHOOK_URL}`);
const { comment: { body: text, created_at: createdAt } } = result;
const username = context.user.username;
process.nextTick(async () => {
const response = await fetch(SLACK_WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
timeout: SLACK_WEBHOOK_TIMEOUT,
body: JSON.stringify({
attachments: [
{
text: text,
footer: `Comment by ${username}`,
ts: Math.floor(Date.parse(createdAt) / 1000),
},
],
}),
});
if (!response.ok) {
const responseText = await response.text();
console.trace(
`Posting to Slack failed with HTTP code ${
response.status
} and body '${responseText}'`
);
}
});
return result;
},
},
},
};