diff --git a/api.js b/api.js index 47684933..835eb183 100644 --- a/api.js +++ b/api.js @@ -1398,7 +1398,10 @@ server.get('/users/:user/updates', (req, res, next) => { function formatJournalData(e) { let data = {}; Object.keys(e).forEach(key => { - if (!['_id', 'ignore', 'user'].includes(key)) { + if (!['_id', 'ignore', 'user', 'modseq', 'unseenChange', 'created'].includes(key)) { + if (e.command !== 'COUNTERS' && key === 'unseen') { + return; + } data[key] = e[key]; } }); @@ -1413,7 +1416,6 @@ function formatJournalData(e) { } function loadJournalStream(req, res, user, lastEventId, done) { - console.log('READ'); let query = { user }; if (lastEventId) { query._id = { $gt: lastEventId }; @@ -1446,7 +1448,7 @@ function loadJournalStream(req, res, user, lastEventId, done) { processed }); } - let mailbox = mailboxes[mailboxPos++]; + let mailbox = new ObjectID(mailboxes[mailboxPos++]); getMailboxCounter(mailbox, false, (err, total) => { if (err) { // ignore @@ -1502,12 +1504,14 @@ function loadJournalStream(req, res, user, lastEventId, done) { } function getMailboxCounter(mailbox, type, done) { - let prefix = type ? type : 'sum'; + let prefix = type ? type : 'total'; db.redis.get(prefix + ':' + mailbox.toString(), (err, sum) => { if (err) { return done(err); } + console.log(prefix + ':' + mailbox.toString(), err, sum); + if (sum !== null) { return done(null, Number(sum)); } @@ -1517,11 +1521,14 @@ function getMailboxCounter(mailbox, type, done) { if (type) { query[type] = true; } + db.database.collection('messages').count(query, (err, sum) => { if (err) { return done(err); } + console.log(query, err, sum); + // cache calculated sum in redis db.redis.multi().set(prefix + ':' + mailbox.toString(), sum).expire(prefix + ':' + mailbox.toString(), consts.MAILBOX_COUNTER_TTL).exec(() => { done(null, sum); diff --git a/lib/counters.js b/lib/counters.js index d8918e4c..ed3dccd7 100644 --- a/lib/counters.js +++ b/lib/counters.js @@ -30,9 +30,10 @@ local ttl = tonumber(ARGV[2]) or 0; if redis.call("EXISTS", key) == 1 then redis.call("INCRBY", key, increment); + local sum = tonumber(redis.call("GET", key)) or 0; -- extend the life of this counter by ttl seconds redis.call("EXPIRE", key, ttl); - return redis.call("GET", key); + return sum; else return nil; end @@ -65,7 +66,22 @@ module.exports = redis => { if (err) { return callback(err); } - script.run(1, key, count, ttl, callback); + + script.run( + 1, + key, + count, + ttl, + ( + err, + res => { + if (err) { + return callback(err); + } + callback(null, res); + } + ) + ); }); } };