2014-11-11 21:58:28 +08:00
|
|
|
_ = require("lodash")
|
|
|
|
async = require("async")
|
|
|
|
redis = require("redis")
|
|
|
|
request = require("request")
|
|
|
|
|
|
|
|
config = require("./config")
|
|
|
|
Hook = require("./hook")
|
2014-11-13 22:15:20 +08:00
|
|
|
IDMapping = require("./id_mapping")
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger = require("./logger")
|
2014-11-11 21:58:28 +08:00
|
|
|
|
|
|
|
# Web hooks will listen for events on redis coming from BigBlueButton and
|
|
|
|
# perform HTTP calls with them to all registered hooks.
|
|
|
|
module.exports = class WebHooks
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
@subscriberEvents = redis.createClient()
|
|
|
|
|
|
|
|
start: ->
|
2014-11-12 02:43:41 +08:00
|
|
|
@_subscribeToEvents()
|
|
|
|
|
|
|
|
# Subscribe to the events on pubsub that might need to be sent in callback calls.
|
|
|
|
_subscribeToEvents: ->
|
2014-11-11 21:58:28 +08:00
|
|
|
@subscriberEvents.on "psubscribe", (channel, count) ->
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "WebHooks: subscribed to " + channel
|
2014-11-11 21:58:28 +08:00
|
|
|
|
|
|
|
@subscriberEvents.on "pmessage", (pattern, channel, message) =>
|
2014-11-25 00:34:03 +08:00
|
|
|
|
|
|
|
processMessage = =>
|
|
|
|
if @_filterMessage(channel, message)
|
|
|
|
Logger.info "WebHooks: processing message on [#{channel}]:", JSON.stringify(message)
|
|
|
|
@_processEvent(message)
|
|
|
|
|
2014-11-11 21:58:28 +08:00
|
|
|
try
|
2014-11-13 22:03:42 +08:00
|
|
|
message = JSON.parse(message)
|
|
|
|
if message?
|
|
|
|
id = message.payload?.meeting_id
|
2014-11-13 22:15:20 +08:00
|
|
|
IDMapping.reportActivity(id)
|
2014-11-13 22:03:42 +08:00
|
|
|
|
2014-11-25 00:34:03 +08:00
|
|
|
# First treat meeting events to add/remove ID mappings
|
|
|
|
if message.header?.name is "meeting_created_message"
|
|
|
|
Logger.info "WebHooks: got create message on meetings channel [#{channel}]", message
|
|
|
|
IDMapping.addOrUpdateMapping message.payload?.meeting_id, message.payload?.external_meeting_id, (error, result) ->
|
|
|
|
# has to be here, after the meeting was created, otherwise create calls won't generate
|
|
|
|
# callback calls for meeting hooks
|
|
|
|
processMessage()
|
|
|
|
|
|
|
|
# TODO: Temporarily commented because we still need the mapping for recording events,
|
|
|
|
# after the meeting ended.
|
|
|
|
# else if message.header?.name is "meeting_destroyed_event"
|
|
|
|
# Logger.info "WebHooks: got destroy message on meetings channel [#{channel}]", message
|
|
|
|
# IDMapping.removeMapping message.payload?.meeting_id, (error, result) ->
|
|
|
|
# processMessage()
|
|
|
|
|
|
|
|
else
|
|
|
|
processMessage()
|
2014-11-11 21:58:28 +08:00
|
|
|
|
|
|
|
catch e
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "WebHooks: error processing the message", message, ":", e
|
2014-11-11 21:58:28 +08:00
|
|
|
|
|
|
|
@subscriberEvents.psubscribe config.hooks.pchannel
|
|
|
|
|
|
|
|
# Returns whether the message read from redis should generate a callback
|
|
|
|
# call or not.
|
|
|
|
_filterMessage: (channel, message) ->
|
|
|
|
for event in config.hooks.events
|
|
|
|
if channel? and message.header?.name? and
|
|
|
|
event.channel.match(channel) and event.name.match(message.header?.name)
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
|
|
|
# Processes an event received from redis. Will get all hook URLs that
|
|
|
|
# should receive this event and start the process to perform the callback.
|
|
|
|
_processEvent: (message) ->
|
2014-11-12 23:07:54 +08:00
|
|
|
hooks = Hook.allGlobalSync()
|
2014-11-12 20:53:49 +08:00
|
|
|
|
2014-11-15 02:53:46 +08:00
|
|
|
# TODO: events that happen after the meeting ended will never trigger the hooks
|
|
|
|
# below, since the mapping is removed when the meeting ends
|
2014-11-25 00:34:03 +08:00
|
|
|
|
2014-11-12 20:53:49 +08:00
|
|
|
# filter the hooks that need to receive this event
|
|
|
|
# only global hooks or hooks for this specific meeting
|
2014-11-12 23:07:54 +08:00
|
|
|
idFromMessage = message.payload?.meeting_id
|
2014-11-12 21:28:49 +08:00
|
|
|
if idFromMessage?
|
2014-11-13 22:15:20 +08:00
|
|
|
eMeetingID = IDMapping.getExternalMeetingID(idFromMessage)
|
2014-11-12 23:07:54 +08:00
|
|
|
hooks = hooks.concat(Hook.findByExternalMeetingIDSync(eMeetingID))
|
2014-11-12 20:53:49 +08:00
|
|
|
|
2014-11-12 02:43:41 +08:00
|
|
|
hooks.forEach (hook) ->
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "WebHooks: enqueueing a message in the hook:", hook.callbackURL
|
2014-11-12 02:43:41 +08:00
|
|
|
hook.enqueue message
|