From c7de38f28e6f5823d29e3cecb926a0a6c2b20c8f Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 24 Jul 2014 22:38:35 -0400 Subject: [PATCH] closes #1902 messages:: list changed to messages:uid::to: sortedset --- src/messaging.js | 8 ++--- src/upgrade.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/src/messaging.js b/src/messaging.js index 31303fe362..89621d6ea0 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -20,10 +20,10 @@ var db = require('./database'), if (err) { return callback(err); } - + var timestamp = Date.now(); var message = { content: content, - timestamp: Date.now(), + timestamp: timestamp, fromuid: fromuid, touid: touid }; @@ -44,7 +44,7 @@ var db = require('./database'), return callback(err); } - db.listAppend('messages:' + uids[0] + ':' + uids[1], mid); + db.sortedSetAdd('messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid); db.listPrepend('messages:recent:' + fromuid, message.content, function(err) { if (err) { return callback(err); @@ -85,7 +85,7 @@ var db = require('./database'), Messaging.getMessages = function(fromuid, touid, isNew, callback) { var uids = sortUids(fromuid, touid); - db.getListRange('messages:' + uids[0] + ':' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) { + db.getSortedSetRange('messages:uid:' + uids[0] + ':to:' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) { if (err) { return callback(err); } diff --git a/src/upgrade.js b/src/upgrade.js index 4ded293ab5..db91530ada 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -19,7 +19,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2014, 6, 23); + latestSchema = Date.UTC(2014, 6, 24); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -882,6 +882,87 @@ Upgrade.upgrade = function(callback) { winston.info('[2014/7/23] Upgrading db dependencies - skipped'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2014, 6, 24); + + if (schemaDate < thisSchemaDate) { + winston.info('[2014/7/24] Upgrading chats to sorted set...'); + + db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) { + if (err) { + return next(err); + } + + async.eachLimit(uids, 10, function(uid, next) { + db.getSortedSetRange('uid:' + uid + ':chats', 0, -1, function(err, toUids) { + if (err) { + return next(err); + } + + if (!Array.isArray(toUids) || !toUids.length) { + return next(); + } + + async.eachLimit(toUids, 10, function(toUid, next) { + var uids = [uid, toUid].sort(); + db.getListRange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) { + if (err) { + return next(err); + } + + if (!Array.isArray(mids) || !mids.length) { + return next(); + } + + async.eachLimit(mids, 10, function(mid, next) { + db.getObjectField('message:' + mid, 'timestamp', function(err, timestamp) { + if (err || !timestamp) { + return next(err); + } + + db.sortedSetAdd('messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid, next); + }); + }, next); + }); + }, next); + }); + }, function(err) { + if (err) { + winston.error('[2014/7/24] Error encountered while updating chats to sorted set'); + return next(err); + } + + async.eachLimit(uids, 10, function(uid, next) { + db.getSortedSetRange('uid:' + uid + ':chats', 0, -1, function(err, toUids) { + if (err) { + return next(err); + } + + if (!Array.isArray(toUids) || !toUids.length) { + return next(); + } + + async.eachLimit(toUids, 10, function(toUid, next) { + var uids = [uid, toUid].sort(); + db.delete('messages:' + uids[0] + ':' + uids[1], next); + }, next); + }); + }, function(err) { + if (err) { + winston.error('[2014/7/24] Error encountered while updating chats to sorted set'); + return next(err); + } + + winston.info('[2014/7/24] Upgraded chats to sorted set'); + Upgrade.update(thisSchemaDate, next); + }); + }); + }); + } else { + winston.info('[2014/7/24] Upgrading chats to sorted set - skipped'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!!