UNPKG

storage-mule

Version:

The Realtime Framework Cloud Storage Mule utility

84 lines (76 loc) 2.95 kB
module.exports = function(opts){ var MOBILE_PUSH_URL = { host: 'mobilepush-web-useast1-s0001.realtime.co', path: '/mp/publish' }; var rest = require('./utils/Rest.js'); var logger = opts.logger || null; function checkOptions(data){ if(!data.applicationKey){ data.applicationKey = opts.applicationKey; } if(!data.privateKey){ if(opts.privateKey) { data.privateKey = opts.privateKey; } else { opts.onException && opts.onException('To execute this function, you need to provide the private key.'); return false; } } return true; } var sendPushNotification = function(data, callback){ if(checkOptions(data)){ if(!data.message){ opts.onException && opts.onException('To execute the function "sendPushNotification, you need to provide the message parameter.'); } else if(!data.channel){ opts.onException && opts.onException('To execute the function "sendPushNotification", you need to provide the channel parameter.'); } else { rest.post(MOBILE_PUSH_URL, data, function(err){ if(err) { logger && logger.error('[storage-mule] sendPushNotification error: ' + JSON.stringify(err)); } callback && callback(err); }); } } }; var sendMessage = function(data){ if(checkOptions(data)){ if(!data.message){ opts.onException && opts.onException('To execute the function "sendMessage", you need to provide the message parameter.'); } else if(!data.channel){ opts.onException && opts.onException('To execute the function "sendMessage", you need to provide the channel parameter.'); } else { opts.scope.ortc.send(data); } } }; var saveAuthentication = function(data, callback){ if(checkOptions(data)){ if(!data.token){ opts.onException && opts.onException('To execute the function "saveAuthentication", you need to provide the token parameter.'); } else if(!('isPrivate' in data)){ opts.onException && opts.onException('To execute the function "saveAuthentication", you need to provide the isPrivate parameter.'); } else if(!data.TTL){ opts.onException && opts.onException('To execute the function "saveAuthentication", you need to provide the TTL parameter.'); } else if(!data.permissions || typeof data.permissions !== 'object'){ opts.onException && opts.onException('To execute the function "saveAuthentication", you need to provide the permissions parameter.'); } else { opts.scope.ortc.saveAuthentication(opts.ortcCluster, true, data.token, data.isPrivate, data.applicationKey, data.TTL, data.privateKey, data.permissions, function(err, res){ if(err) { logger && logger.error('[storage-mule] saveAuthentication error: ' + JSON.stringify(err)); } callback && callback(err, res); } ); } } }; return { sendPushNotification: sendPushNotification, sendMessage: sendMessage, saveAuthentication: saveAuthentication }; };