Webhooks: use more "hook" and less "subscription" for consistency

It now creates and destroys hooks instead of creating and removing
subscriptions.
This commit is contained in:
Leonardo Crauss Daronco 2014-11-12 11:28:49 -02:00
parent c25082bafa
commit 79609fadb2
4 changed files with 51 additions and 44 deletions

View File

@ -40,34 +40,34 @@ config.api.responses.failure = (key, msg) ->
config.api.responses.checksumError =
config.api.responses.failure("checksumError", "You did not pass the checksum security check.")
config.api.responses.subscribeSuccess = (id) ->
config.api.responses.hookSuccess = (id) ->
"<response> \
<returncode>SUCCESS</returncode> \
<subscriptionID>#{id}</subscriptionID> \
<hookID>#{id}</hookID> \
</response>"
config.api.responses.subscribeFailure =
config.api.responses.failure("subscribeEventError", "An error happened while storing your subscription. Check the logs.")
config.api.responses.subscribeDuplicated = (id) ->
config.api.responses.hookFailure =
config.api.responses.failure("createHookError", "An error happened while creating your hook. Check the logs.")
config.api.responses.hookDuplicated = (id) ->
"<response> \
<returncode>SUCCESS</returncode> \
<subscriptionID>#{id}</subscriptionID> \
<hookID>#{id}</hookID> \
<messageKey>duplicateWarning</messageKey> \
<message>There is already a subscription for this callback URL.</message> \
<message>There is already a hook for this callback URL.</message> \
</response>"
config.api.responses.unsubscribeSuccess =
config.api.responses.destroySuccess =
"<response> \
<returncode>SUCCESS</returncode> \
<unsubscribed>true</unsubscribed> \
<removed>true</removed> \
</response>"
config.api.responses.unsubscribeFailure =
config.api.responses.failure("unsubscribeEventError", "An error happened while unsubscribing. Check the logs.")
config.api.responses.unsubscribeNoSubscription =
config.api.responses.failure("unsubscribeMissingSubscription", "The subscription informed was not found.")
config.api.responses.destroyFailure =
config.api.responses.failure("destroyHookError", "An error happened while removing your hook. Check the logs.")
config.api.responses.destroyNoHook =
config.api.responses.failure("destroyMissingHook", "The hook informed was not found.")
config.api.responses.missingParamCallbackURL =
config.api.responses.failure("missingParamCallbackURL", "You must specify a callbackURL in the parameters.")
config.api.responses.missingParamSubscriptionID =
config.api.responses.failure("missingParamSubscriptionID", "You must specify a subscriptionID in the parameters.")
config.api.responses.missingParamHookID =
config.api.responses.failure("missingParamHookID", "You must specify a hookID in the parameters.")
module.exports = config

View File

@ -1,3 +1,5 @@
_ = require("lodash")
CallbackEmitter = require("./callback_emitter")
# The database of hooks.
@ -85,8 +87,8 @@ module.exports = class Hook
hook.saveSync()
callback?(null, hook)
@removeSubscription = (subscriptionID, callback) ->
hook = Hook.getSync(subscriptionID)
@removeSubscription = (hookID, callback) ->
hook = Hook.getSync(hookID)
if hook?
msg = "Hook: removing the hook with callback URL [#{hook.callbackURL}]"
msg += " for the meeting [#{hook.externalMeetingID}]" if hook.externalMeetingID?
@ -110,6 +112,17 @@ module.exports = class Hook
else
null
@allForMeetingSync = (externalMeetingID) ->
hooks = Hook.allSync()
_.filter(hooks, (hook) ->
hook.isGlobal() or
(externalMeetingID? and externalMeetingID is hook.targetMeetingID())
)
@allGlobalSync = ->
hooks = Hook.allSync()
_.filter(hooks, (hook) -> hook.isGlobal())
@allSync = ->
arr = Object.keys(db).reduce((arr, id) ->
arr.push db[id]

View File

@ -53,20 +53,14 @@ module.exports = class WebHooks
_processEvent: (message) ->
hooks = Hook.allSync(message)
# get the meeting ID from the message and try to find the external meeting ID
# from our mappings
idFromMessage = message.payload?.meeting_id
if idFromMessage?
extMeetingID = @meetingMappings[idFromMessage]
else
extMeetingID = null
# filter the hooks that need to receive this event
# only global hooks or hooks for this specific meeting
hooks = _.filter(hooks, (hook) ->
hook.isGlobal() or
(extMeetingID? and extMeetingID is hook.targetMeetingID())
)
idFromMessage = message.payload?.meeting_id # always the internal meetingID
if idFromMessage?
externalMeetingID = @meetingMappings[idFromMessage]
hooks = Hook.allForMeetingSync(externalMeetingID)
else
hooks = Hook.allGlobalSync(externalMeetingID)
hooks.forEach (hook) ->
console.log "WebHooks: enqueueing a message in the hook:", hook.callbackURL
@ -88,7 +82,7 @@ module.exports = class WebHooks
@_removeMeetingMapping(message.payload?.meeting_id)
catch e
console.log "WebHooks: error processing the message", message, ":", e
console.log "WebHooks: error processing the message", JSON.stringify(message), ":", e
@subscriberMeetings.subscribe config.hooks.meetingsChannel

View File

@ -26,11 +26,11 @@ module.exports = class WebServer
console.log "<==", req.method, "request to", req.url, "from:", clientDataSimple(req)
next()
@app.get "/bigbluebutton/api/hooks/subscribe", @_validateChecksum, @_subscribe
@app.get "/bigbluebutton/api/hooks/unsubscribe", @_validateChecksum, @_unsubscribe
@app.get "/bigbluebutton/api/hooks/create", @_validateChecksum, @_create
@app.get "/bigbluebutton/api/hooks/destroy", @_validateChecksum, @_destroy
@app.get "/bigbluebutton/api/hooks/list", @_validateChecksum, @_list
_subscribe: (req, res, next) ->
_create: (req, res, next) ->
urlObj = url.parse(req.url, true)
callbackURL = urlObj.query["callbackURL"]
meetingID = urlObj.query["meetingID"]
@ -43,27 +43,27 @@ module.exports = class WebServer
else
Hook.addSubscription callbackURL, meetingID, (error, hook) ->
if error? # the only error for now is for duplicated callbackURL
msg = config.api.responses.subscribeDuplicated(hook.id)
msg = config.api.responses.hookDuplicated(hook.id)
else if hook?
msg = config.api.responses.subscribeSuccess(hook.id)
msg = config.api.responses.hookSuccess(hook.id)
else
msg = config.api.responses.subscribeFailure
msg = config.api.responses.hookFailure
respondWithXML(res, msg)
_unsubscribe: (req, res, next) ->
_destroy: (req, res, next) ->
urlObj = url.parse(req.url, true)
subscriptionID = urlObj.query["subscriptionID"]
hookID = urlObj.query["hookID"]
unless subscriptionID?
respondWithXML(res, config.api.responses.missingParamSubscriptionID)
unless hookID?
respondWithXML(res, config.api.responses.missingParamHookID)
else
Hook.removeSubscription subscriptionID, (error, result) ->
Hook.removeSubscription hookID, (error, result) ->
if error?
msg = config.api.responses.unsubscribeFailure
msg = config.api.responses.destroyFailure
else if !result
msg = config.api.responses.unsubscribeNoSubscription
msg = config.api.responses.destroyNoHook
else
msg = config.api.responses.unsubscribeSuccess
msg = config.api.responses.destroySuccess
respondWithXML(res, msg)
_list: (req, res, next) ->