update chat if necessary after new message posted or deleted

This commit is contained in:
Matteo Pagliazzi
2013-11-08 17:03:10 +01:00
parent a5999a7be3
commit a78297f129
2 changed files with 22 additions and 6 deletions
+9 -1
View File
@@ -206,6 +206,9 @@ api.postChat = function(req, res, next) {
timestamp: +(new Date)
};
var lastClientMsg = req.query.previousMsg;
var chatUpdated = (lastClientMsg && group.chat && group.chat[0] && group.chat[0].id !== lastClientMsg) ? true : false;
group.chat.unshift(message);
group.chat.splice(200);
@@ -217,6 +220,8 @@ api.postChat = function(req, res, next) {
group.save(function(err, saved){
if (err) return res.json(500, {err:err});
if(!chatUpdated) return res.json({message: saved.chat[0]});
res.json({chat: saved.chat});
});
}
@@ -231,9 +236,12 @@ api.deleteChatMessage = function(req, res){
if(user._id !== message.uuid && !(user.backer && user.backer.admin))
return res.json(401, {err: "Not authorized to delete this message!"})
var lastClientMsg = req.query.previousMsg;
var chatUpdated = (lastClientMsg && group.chat && group.chat[0] && group.chat[0].id !== lastClientMsg) ? true : false;
Group.update({_id:group._id}, {$pull:{chat:{id: req.params.messageId}}}, function(err){
if(err) return res.json(500, {err: err});
res.send(204);
return chatUpdated ? res.json({chat: group.chat}) : res.send(204)
});
}