2014-11-12 23:07:54 +08:00
|
|
|
_ = require("lodash")
|
2014-11-13 02:35:25 +08:00
|
|
|
redis = require("redis")
|
|
|
|
|
|
|
|
config = require("./config")
|
2014-11-12 23:07:54 +08:00
|
|
|
|
|
|
|
# The database of mappings. Format:
|
|
|
|
# { internalMeetingID: externalMeetingID }
|
|
|
|
db = {}
|
|
|
|
|
|
|
|
# A simple model to store mappings for meeting IDs.
|
|
|
|
module.exports = class MeetingIDMap
|
|
|
|
|
2014-11-13 04:07:29 +08:00
|
|
|
@addOrUpdateMapping = (internalMeetingID, externalMeetingID) ->
|
|
|
|
db[internalMeetingID] = externalMeetingID
|
|
|
|
console.log "MeetingIDMap: added or changed meeting mapping to the list { #{internalMeetingID}: #{db[internalMeetingID]} }"
|
|
|
|
MeetingIDMap.updateRedis()
|
2014-11-12 23:07:54 +08:00
|
|
|
|
|
|
|
@removeMapping = (internalMeetingID) ->
|
|
|
|
if internalMeetingID in _.keys(db)
|
|
|
|
console.log "MeetingIDMap: removing meeting mapping from the list { #{internalMeetingID}: #{db[internalMeetingID]} }"
|
|
|
|
delete db[internalMeetingID]
|
|
|
|
db[internalMeetingID] = null
|
2014-11-13 02:35:25 +08:00
|
|
|
MeetingIDMap.updateRedis()
|
2014-11-12 23:07:54 +08:00
|
|
|
|
|
|
|
@getInternalMeetingID = (externalMeetingID) ->
|
|
|
|
for internal, external of db
|
|
|
|
if external is externalMeetingID
|
|
|
|
return internal
|
|
|
|
null
|
|
|
|
|
|
|
|
@getExternalMeetingID = (internalMeetingID) ->
|
|
|
|
db[internalMeetingID]
|
2014-11-13 02:35:25 +08:00
|
|
|
|
|
|
|
@initialize = (callback) ->
|
|
|
|
MeetingIDMap.resync(callback)
|
|
|
|
|
|
|
|
# Gets all mappings from redis to populate the local database.
|
|
|
|
# Calls `callback()` when done.
|
|
|
|
@resync = (callback) ->
|
|
|
|
client = redis.createClient()
|
|
|
|
client.hgetall config.redis.keys.mappings, (error, mappings) =>
|
|
|
|
MeetingIDMap.fromRedis(mappings)
|
|
|
|
callback?(error, mappings)
|
|
|
|
|
|
|
|
@fromRedis = (mappings) ->
|
2014-11-13 04:07:29 +08:00
|
|
|
if mappings?
|
|
|
|
db = mappings
|
|
|
|
else
|
|
|
|
db = {}
|
2014-11-13 02:35:25 +08:00
|
|
|
|
|
|
|
@updateRedis = (callback) ->
|
|
|
|
client = redis.createClient()
|
|
|
|
client.hmset config.redis.keys.mappings, db, (error, reply) =>
|
|
|
|
callback?(error)
|