diff --git a/src/messaging/create.js b/src/messaging/create.js index d01eaf6292..3b2b5dcf26 100644 --- a/src/messaging/create.js +++ b/src/messaging/create.js @@ -74,13 +74,14 @@ module.exports = function(Messaging) { async.parallel([ async.apply(Messaging.addRoomToUsers, roomId, uids, timestamp), async.apply(Messaging.addMessageToUsers, roomId, uids, mid, timestamp), - async.apply(Messaging.markUnread, uids, roomId) + async.apply(Messaging.markUnread, uids, roomId), + async.apply(Messaging.addUsersToRoom, fromuid, [fromuid], roomId) ], next); }, function (results, next) { async.parallel({ markRead: async.apply(Messaging.markRead, fromuid, roomId), - messages: async.apply(Messaging.getMessagesData, [mid], fromuid, roomId, true) + messages: async.apply(Messaging.getMessagesData, [mid], fromuid, roomId, true) }, next); }, function (results, next) { diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 5cc3459291..54f67796ee 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -16,10 +16,14 @@ module.exports = function(Messaging) { }, function (_roomId, next) { roomId = _roomId; - db.sortedSetAdd('chat:room:' + roomId + ':uids', now, uid, next); + var room = { + owner: uid, + roomId: roomId + }; + db.setObject('chat:room:' + roomId, room, next); }, function (next) { - Messaging.addUsersToRoom(uid, toUids, roomId, next); + Messaging.addUsersToRoom(uid, [uid].concat(toUids), roomId, next); }, function (next) { Messaging.addRoomToUsers(roomId, [uid].concat(toUids), now, next); @@ -39,14 +43,12 @@ module.exports = function(Messaging) { }; Messaging.isRoomOwner = function(uid, roomId, callback) { - db.getSortedSetRange('chat:room:' + roomId + ':uids', 0, 0, function(err, uids) { + db.getObjectField('chat:room:' + roomId, 'owner', function(err, owner) { if (err) { return callback(err); } - if (!Array.isArray(uids) || !uids.length) { - return callback(null, false); - } - callback(null, parseInt(uids[0], 10) === parseInt(uid, 10)); + + callback(null, parseInt(uid, 10) === parseInt(owner, 10)); }); }; diff --git a/src/upgrade.js b/src/upgrade.js index 4b2f78e56c..e23a260cbe 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -10,7 +10,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2015, 11, 15); + latestSchema = Date.UTC(2015, 11, 23); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -232,6 +232,52 @@ Upgrade.upgrade = function(callback) { winston.info('[2015/12/15] Chats upgrade skipped!'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2015, 11, 23); + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + winston.info('[2015/12/23] Upgrading chat room hashes'); + + db.getObjectField('global', 'nextChatRoomId', function(err, nextChatRoomId) { + if (err) { + return next(err); + } + var currentChatRoomId = 1; + async.whilst(function() { + return currentChatRoomId < nextChatRoomId; + }, function(next) { + db.getSortedSetRange('chat:room:' + currentChatRoomId + ':uids', 0, 0, function(err, uids) { + if (err) { + return next(err); + } + if (!Array.isArray(uids) || !uids.length || !uids[0]) { + ++ currentChatRoomId; + return next(); + } + + db.setObject('chat:room:' + currentChatRoomId, {owner: uids[0], roomId: currentChatRoomId}, function(err) { + if (err) { + return next(err); + } + ++ currentChatRoomId; + next(); + }); + }); + }, function(err) { + if (err) { + return next(err); + } + + winston.info('[2015/12/23] Chats room hashes upgrade done!'); + Upgrade.update(thisSchemaDate, next); + }); + }); + } else { + winston.info('[2015/12/23] Chats room hashes upgrade skipped!'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!!