This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhooks.js
49 lines (38 loc) · 1.62 KB
/
webhooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
const EventEmitter = require('events').EventEmitter
const logger = require('winston')
module.exports = new EventEmitter()
module.exports.emitWebhook = (message) => {
const type = message.type
const annotationType = message.annotationType
let annotationPayload = {}
// the annotationPayload is a string that must be parsed to an object
if (message.annotationPayload) {
annotationPayload = JSON.parse(message.annotationPayload)
// since we now have the payload, remove it from the message
// and send it as a param in the emit event
delete message.annotationPayload
}
logger.verbose(`Emiting '${type}' with message`)
logger.debug(message)
logger.verbose(`Emiting '${type}' with payload`)
logger.debug(annotationPayload)
// call the node event emitters
// message-created or message-annotation-removed
module.exports.emit(type, message, annotationPayload)
// more granular annotation related events
// 'message-focus' or 'actionSelected'
module.exports.emit(annotationType, message, annotationPayload)
// 'message-focus:ActionRequest' or 'message-focus:Question'
if (annotationPayload.lens) {
module.exports.emit(`${annotationType}:${annotationPayload.lens}`, message, annotationPayload)
}
// 'message-focus:ActionRequest:Schedule'
if (annotationPayload.category) {
module.exports.emit(`${annotationType}:${annotationPayload.lens}:${annotationPayload.category}`, message, annotationPayload)
}
// 'actionSelected:sample_button'
if (annotationPayload.actionId) {
module.exports.emit(`${annotationType}:${annotationPayload.actionId}`, message, annotationPayload)
}
}