Skip to content

Instantly share code, notes, and snippets.

@jmcguirk
Created January 1, 2013 00:29
Show Gist options
  • Save jmcguirk/4424156 to your computer and use it in GitHub Desktop.
Save jmcguirk/4424156 to your computer and use it in GitHub Desktop.
/*!
* Inbox service. Responsible for sending messages and receiving new ones
*/
function InboxService(){
this.fetchNewItems = function(args, context, callback){
var callResponse = {};
ServiceLocator.PlayerManager.getOrCreatePlayer(context.UserId, context, function(player){
ServiceLocator.DataStore.getNewInboxItems(context.UserId, ServiceLocator.Constants.PLAYER_INBOX_MAIN_INBOX_NAME, player.data.inbox, function(err, newItems){
callResponse.newItems = newItems;
player.save(context);
callback(callResponse);
});
});
}
this.ackItems = function(args, context, callback){
var callResponse = {};
ServiceLocator.PlayerManager.getOrCreatePlayer(context.UserId, context, function(player){
ServiceLocator.DataStore.ackInboxItems(context.UserId, ServiceLocator.Constants.PLAYER_INBOX_MAIN_INBOX_NAME, player.data.inbox, function(err){
if(!err){
callResponse.result = "OK";
player.save(context);
} else{
callResponse.error = 1;
callResponse.errorMessage = "Failed to ack messages " + err;
}
callback(callResponse);
});
});
}
this.sendItem = function(args, context, callback){
var callResponse = {};
var recipient = args.recipientId;
var msgBody = {};
msgBody.text = "Be sure to drink your Ovaltine";
msgBody.senderId = context.UserId;
ServiceLocator.DataStore.appendInboxItem(recipient, ServiceLocator.Constants.PLAYER_INBOX_MAIN_INBOX_NAME, msgBody, function(err){
if(!err){
callResponse.result = "OK";
} else{
callResponse.error = 1;
callResponse.errorMessage = "Failed to ack messages " + err;
}
callback(callResponse);
});
}
}
exports.instance = new InboxService();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment