notificator
Version:
Library for handling custom notifications, emails, APS, GCM...
150 lines (122 loc) • 4.27 kB
text/coffeescript
async = require('async')
util = require('util')
Q = require('q')
class Notificator
@::defaultLanguage = 'en'
@::channels = []
@::events = null
constructor:( = {})->
.logging = .logging or no
= .defaultLanguage if .defaultLanguage
registerEvent:(event)->
= or []
.push(event)
registerEvents:(events)->
for event in events
addChannel:(name,channel)->
.push({name:name,channel:channel})
getChannel:(name)->
for channel in
if channel.name is name
return channel
return null
getTemplates:(channel,info,callback)->
channel.getTemplates(info,(err,messages)->
return callback(err) if err
if messages and not util.isArray(messages)
messages = [messages]
callback(null,messages)
)
getMessageFromTemplate:(template,receiver,destination,data)->
data = data or {}
if receiver
data.receiver = receiver
data.destination = destination
return template.getMessage(data)
notify:(event,receivers,data,options,callback)->
deferred = Q.defer()
if not Array.isArray(receivers)
receivers = [receivers]
if typeof data is 'function'
callback = data
data = options = undefined
else if typeof options is 'function'
callback = options
options = undefined
async.nextTick(()=>
if and event not in
return deferred.reject(new Error('unknown event ' + event))
# console.log(event,receiver,data,options,callback)
data = data or {}
data._event = event
async.forEach(receivers,(receiver,cb)=>
async.forEach(,(channelWrap,cb)=>
if util.isArray(options?.channels) and channelWrap.name not in options.channels
return cb()
channel = channelWrap.channel
channel.getDestinations(receiver,(err,destinations)=>
return cb(err) if err
async.forEach(destinations,(destination,cb2)=>
,cb)
)
,cb)
,(err)->
if err
deferred.reject(err)
else
deferred.resolve()
)
)
return deferred.promise.nodeify(callback)
notifyDestination:(event,channelName,destinations,data,options,callback)->
deferred = Q.defer()
if typeof data is 'function'
callback = data
data = options = undefined
else if typeof options is 'function'
callback = options
options = undefined
channelWrap =
if not channelWrap
throw new Error('could not find channel \'' + channelName + '\'')
if not Array.isArray(destinations)
destinations = [destinations]
async.map(destinations,(destination,cb)=>
channel = channelWrap.channel
wrappedDestination = channel.wrappedDestination(destination)
,(err,info)->
if err
deferred.reject(err)
else
if destinations.length is 0
deferred.resolve(info[0])
else
deferred.resolve(info)
)
return deferred.promise.nodeify(callback)
sendMessage:(event,channel,receiver,destination,data,callback)->
info = {
event: event,
language: destination.language or ,
data: data
}
log:()->
if .logging
console.log.apply(console,arguments)
module.exports = Notificator