Webhooks: rename MeetingIDMap to IDMapping

This commit is contained in:
Leonardo Crauss Daronco 2014-11-13 12:15:20 -02:00
parent 14641d1a3b
commit 6dbcf1bab2
4 changed files with 21 additions and 21 deletions

View File

@ -1,6 +1,6 @@
config = require("./config") config = require("./config")
Hook = require("./hook") Hook = require("./hook")
MeetingIDMap = require("./meeting_id_map") IDMapping = require("./id_mapping")
WebHooks = require("./web_hooks") WebHooks = require("./web_hooks")
WebServer = require("./web_server") WebServer = require("./web_server")
@ -14,6 +14,6 @@ module.exports = class Application
start: -> start: ->
Hook.initialize => Hook.initialize =>
MeetingIDMap.initialize => IDMapping.initialize =>
@webServer.start(config.server.port) @webServer.start(config.server.port)
@webHooks.start() @webHooks.start()

View File

@ -4,7 +4,7 @@ redis = require("redis")
config = require("./config") config = require("./config")
CallbackEmitter = require("./callback_emitter") CallbackEmitter = require("./callback_emitter")
MeetingIDMap = require("./meeting_id_map") IDMapping = require("./id_mapping")
# The database of hooks. # The database of hooks.
# Used always from memory, but saved to redis for persistence. # Used always from memory, but saved to redis for persistence.

View File

@ -24,7 +24,7 @@ db = {}
nextID = 1 nextID = 1
# A simple model to store mappings for meeting IDs. # A simple model to store mappings for meeting IDs.
module.exports = class MeetingIDMap module.exports = class IDMapping
constructor: -> constructor: ->
@id = null @id = null
@ -72,27 +72,27 @@ module.exports = class MeetingIDMap
JSON.stringify(@toRedis()) JSON.stringify(@toRedis())
@addOrUpdateMapping = (internalMeetingID, externalMeetingID, callback) -> @addOrUpdateMapping = (internalMeetingID, externalMeetingID, callback) ->
mapping = new MeetingIDMap() mapping = new IDMapping()
mapping.id = nextID++ mapping.id = nextID++
mapping.internalMeetingID = internalMeetingID mapping.internalMeetingID = internalMeetingID
mapping.externalMeetingID = externalMeetingID mapping.externalMeetingID = externalMeetingID
mapping.lastActivity = new Date().getTime() mapping.lastActivity = new Date().getTime()
mapping.save (error, result) -> mapping.save (error, result) ->
console.log "MeetingIDMap: added or changed meeting mapping to the list #{externalMeetingID}:", mapping.print() console.log "IDMapping: added or changed meeting mapping to the list #{externalMeetingID}:", mapping.print()
callback?(error, result) callback?(error, result)
@removeMapping = (internalMeetingID, callback) -> @removeMapping = (internalMeetingID, callback) ->
for external, mapping of db for external, mapping of db
if mapping.internalMeetingID is internalMeetingID if mapping.internalMeetingID is internalMeetingID
mapping.destroy (error, result) -> mapping.destroy (error, result) ->
console.log "MeetingIDMap: removing meeting mapping from the list #{external}:", mapping.print() console.log "IDMapping: removing meeting mapping from the list #{external}:", mapping.print()
callback?(error, result) callback?(error, result)
@getInternalMeetingID = (externalMeetingID) -> @getInternalMeetingID = (externalMeetingID) ->
db[externalMeetingID].internalMeetingID db[externalMeetingID].internalMeetingID
@getExternalMeetingID = (internalMeetingID) -> @getExternalMeetingID = (internalMeetingID) ->
mapping = MeetingIDMap.findByInternalMeetingID(internalMeetingID) mapping = IDMapping.findByInternalMeetingID(internalMeetingID)
mapping?.externalMeetingID mapping?.externalMeetingID
@findByInternalMeetingID = (internalMeetingID) -> @findByInternalMeetingID = (internalMeetingID) ->
@ -111,7 +111,7 @@ module.exports = class MeetingIDMap
# Sets the last activity of the mapping for `internalMeetingID` to now. # Sets the last activity of the mapping for `internalMeetingID` to now.
@reportActivity = (internalMeetingID) -> @reportActivity = (internalMeetingID) ->
mapping = MeetingIDMap.findByInternalMeetingID(internalMeetingID) mapping = IDMapping.findByInternalMeetingID(internalMeetingID)
if mapping? if mapping?
mapping.lastActivity = new Date().getTime() mapping.lastActivity = new Date().getTime()
mapping.save() mapping.save()
@ -120,18 +120,18 @@ module.exports = class MeetingIDMap
# are "expired", that had their last activity too long ago. # are "expired", that had their last activity too long ago.
@cleanup = -> @cleanup = ->
now = new Date().getTime() now = new Date().getTime()
all = MeetingIDMap.allSync() all = IDMapping.allSync()
toRemove = _.filter(all, (mapping) -> toRemove = _.filter(all, (mapping) ->
mapping.lastActivity < now - config.mappings.timeout mapping.lastActivity < now - config.mappings.timeout
) )
unless _.isEmpty(toRemove) unless _.isEmpty(toRemove)
console.log "MeetingIDMap: expiring the mappings:", _.map(toRemove, (map) -> map.print()) console.log "IDMapping: expiring the mappings:", _.map(toRemove, (map) -> map.print())
toRemove.forEach (mapping) -> mapping.destroy() toRemove.forEach (mapping) -> mapping.destroy()
# Initializes global methods for this model. # Initializes global methods for this model.
@initialize = (callback) -> @initialize = (callback) ->
MeetingIDMap.resync(callback) IDMapping.resync(callback)
MeetingIDMap.cleanupInterval = setInterval(MeetingIDMap.cleanup, config.mappings.cleanupInterval) IDMapping.cleanupInterval = setInterval(IDMapping.cleanup, config.mappings.cleanupInterval)
# Gets all mappings from redis to populate the local database. # Gets all mappings from redis to populate the local database.
# Calls `callback()` when done. # Calls `callback()` when done.
@ -148,7 +148,7 @@ module.exports = class MeetingIDMap
console.log "Hook: error getting information for a mapping from redis", error if error? console.log "Hook: error getting information for a mapping from redis", error if error?
if mappingData? if mappingData?
mapping = new MeetingIDMap() mapping = new IDMapping()
mapping.fromRedis(mappingData) mapping.fromRedis(mappingData)
mapping.save (error, hook) -> mapping.save (error, hook) ->
nextID = mapping.id + 1 if mapping.id >= nextID nextID = mapping.id + 1 if mapping.id >= nextID
@ -157,6 +157,6 @@ module.exports = class MeetingIDMap
done(null, null) done(null, null)
async.series tasks, (errors, result) -> async.series tasks, (errors, result) ->
mappings = _.map(MeetingIDMap.allSync(), (m) -> m.print()) mappings = _.map(IDMapping.allSync(), (m) -> m.print())
console.log "MeetingIDMap: finished resync, mappings registered:", mappings console.log "IDMapping: finished resync, mappings registered:", mappings
callback?() callback?()

View File

@ -5,7 +5,7 @@ request = require("request")
config = require("./config") config = require("./config")
Hook = require("./hook") Hook = require("./hook")
MeetingIDMap = require("./meeting_id_map") IDMapping = require("./id_mapping")
# Web hooks will listen for events on redis coming from BigBlueButton and # Web hooks will listen for events on redis coming from BigBlueButton and
# perform HTTP calls with them to all registered hooks. # perform HTTP calls with them to all registered hooks.
@ -31,7 +31,7 @@ module.exports = class WebHooks
message = JSON.parse(message) message = JSON.parse(message)
if message? if message?
id = message.payload?.meeting_id id = message.payload?.meeting_id
MeetingIDMap.reportActivity(id) IDMapping.reportActivity(id)
if @_filterMessage(channel, message) if @_filterMessage(channel, message)
console.log "WebHooks: processing message on [#{channel}]:", JSON.stringify(message) console.log "WebHooks: processing message on [#{channel}]:", JSON.stringify(message)
@ -60,7 +60,7 @@ module.exports = class WebHooks
# only global hooks or hooks for this specific meeting # only global hooks or hooks for this specific meeting
idFromMessage = message.payload?.meeting_id idFromMessage = message.payload?.meeting_id
if idFromMessage? if idFromMessage?
eMeetingID = MeetingIDMap.getExternalMeetingID(idFromMessage) eMeetingID = IDMapping.getExternalMeetingID(idFromMessage)
hooks = hooks.concat(Hook.findByExternalMeetingIDSync(eMeetingID)) hooks = hooks.concat(Hook.findByExternalMeetingIDSync(eMeetingID))
hooks.forEach (hook) -> hooks.forEach (hook) ->
@ -78,9 +78,9 @@ module.exports = class WebHooks
try try
message = JSON.parse(message) message = JSON.parse(message)
if message.header?.name is "meeting_created_message" if message.header?.name is "meeting_created_message"
MeetingIDMap.addOrUpdateMapping(message.payload?.meeting_id, message.payload?.external_meeting_id) IDMapping.addOrUpdateMapping(message.payload?.meeting_id, message.payload?.external_meeting_id)
else if message.header?.name is "meeting_destroyed_event" else if message.header?.name is "meeting_destroyed_event"
MeetingIDMap.removeMapping(message.payload?.meeting_id) IDMapping.removeMapping(message.payload?.meeting_id)
catch e catch e
console.log "WebHooks: error processing the message", JSON.stringify(message), ":", e console.log "WebHooks: error processing the message", JSON.stringify(message), ":", e