diff --git a/src/messaging.js b/src/messaging.js index 70e535a0d9..784c66dda0 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -211,32 +211,20 @@ var async = require('async'), }); }; - Messaging.isNewSet = function(uid, roomId, mid, callback) { + Messaging.isNewSet = function(uid, roomId, timestamp, callback) { var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids'; async.waterfall([ - async.apply(db.sortedSetRank, setKey, mid), - function(index, next) { - if (index > 0) { - db.getSortedSetRange(setKey, index - 1, index, next); - } else { - next(null, true); - } - }, - function(mids, next) { - if (typeof mids !== 'boolean' && mids && mids.length) { - db.getObjects(['message:' + mids[0], 'message:' + mids[1]], next); - } else { - next(null, mids); - } + function(next) { + db.getSortedSetRevRangeWithScores(setKey, 0, 0, next); }, function(messages, next) { - if (typeof messages !== 'boolean' && messages && messages.length) { - next(null, parseInt(messages[1].timestamp, 10) > parseInt(messages[0].timestamp, 10) + (1000*60*5)); + if (messages && messages.length) { + next(null, parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + (1000 * 60 * 5)); } else { - next(null, messages); + next(null, true); } - } + } ], callback); }; diff --git a/src/messaging/create.js b/src/messaging/create.js index 0309cab4b9..d01eaf6292 100644 --- a/src/messaging/create.js +++ b/src/messaging/create.js @@ -41,6 +41,7 @@ module.exports = function(Messaging) { Messaging.addMessage = function(fromuid, roomId, content, timestamp, callback) { var mid; var message; + var isNewSet; async.waterfall([ function (next) { @@ -63,6 +64,10 @@ module.exports = function(Messaging) { db.setObject('message:' + mid, message, next); }, function (next) { + Messaging.isNewSet(fromuid, roomId, timestamp, next); + }, + function (_isNewSet, next) { + isNewSet = _isNewSet; db.getSortedSetRange('chat:room:' + roomId + ':uids', 0, -1, next); }, function (uids, next) { @@ -75,8 +80,7 @@ module.exports = function(Messaging) { function (results, next) { async.parallel({ markRead: async.apply(Messaging.markRead, fromuid, roomId), - messages: async.apply(Messaging.getMessagesData, [mid], fromuid, roomId, true), - isNewSet: async.apply(Messaging.isNewSet, fromuid, roomId, mid) + messages: async.apply(Messaging.getMessagesData, [mid], fromuid, roomId, true) }, next); }, function (results, next) { @@ -84,7 +88,7 @@ module.exports = function(Messaging) { return next(null, null); } - results.messages[0].newSet = results.isNewSet; + results.messages[0].newSet = isNewSet; results.messages[0].mid = mid; results.messages[0].roomId = roomId; next(null, results.messages[0]);