From da626a4035d6a5130954bde7c021642c715c5e0a Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Sun, 26 Jan 2014 16:23:37 -0500 Subject: [PATCH] more fixes --- src/database/mongo.js | 17 +++++++++++++++++ src/database/redis.js | 4 ++++ src/routes/api.js | 2 +- src/topics.js | 26 +++++++++++++++++++------- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index 59fbc41b5a..9ebc45bd33 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -656,6 +656,23 @@ }); } + module.sortedSetRevRank = function(key, value, callback) { + if(value !== null && value !== undefined) { + value = value.toString(); + } + module.getSortedSetRange(key, 0, -1, function(err, result) { + if(err) { + return callback(err); + } + var rank = result.indexOf(value); + if(rank === -1) { + return callback(null, null); + } + + callback(null, result.length - rank - 1); + }); + } + module.sortedSetScore = function(key, value, callback) { if(value !== null && value !== undefined) { value = value.toString(); diff --git a/src/database/redis.js b/src/database/redis.js index de4802456c..b4e67b836b 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -382,6 +382,10 @@ redisClient.zrank(key, value, callback); } + module.sortedSetRevRank = function(key, value, callback) { + redisClient.zrevrank(key, value, callback); + } + module.sortedSetScore = function(key, value, callback) { redisClient.zscore(key, value, callback); } diff --git a/src/routes/api.js b/src/routes/api.js index 6dbb0080f7..58b818668d 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -245,7 +245,7 @@ var path = require('path'), app.get('/recent/:term?', function (req, res, next) { var uid = (req.user) ? req.user.uid : 0; - topics.getLatestTopics(uid, 0, 19, req.params.term, function (err, data) { + topics.getLatestTopics(uid, 0, 9, req.params.term, function (err, data) { if(err) { return next(err); } diff --git a/src/topics.js b/src/topics.js index d7848ce466..76a98767d9 100644 --- a/src/topics.js +++ b/src/topics.js @@ -426,8 +426,9 @@ var async = require('async'), }; var since = terms['day']; - if(terms[term]) + if(terms[term]) { since = terms[term]; + } var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1]; db.getSortedSetRevRangeByScore(args, function(err, tids) { @@ -442,11 +443,9 @@ var async = require('async'), if (!tids || !tids.length) { latestTopics.no_topics_message = 'show'; - callback(err, latestTopics); - return; + return callback(null, latestTopics); } - // Filter out topics that belong to categories that this user cannot access async.filter(tids, function(tid, next) { threadTools.privileges(tid, current_user, function(err, privileges) { if (!err && privileges.read) { @@ -457,8 +456,20 @@ var async = require('async'), }); }, function(tids) { Topics.getTopicsByTids(tids, 0, current_user, function(err, topicData) { - latestTopics.topics = topicData; - callback(err, latestTopics); + if(err) { + return callback(err); + } + + if(!topicData || !topicData.length) { + latestTopics.no_topics_message = 'show'; + return callback(null, latestTopics); + } + + db.sortedSetRevRank('topics:recent', topicData[topicData.length - 1].tid, function(err, rank) { + latestTopics.lastIndex = rank; + latestTopics.topics = topicData; + callback(null, latestTopics); + }); }); }); }); @@ -700,6 +711,7 @@ var async = require('async'), function isTopicVisible(topicData, topicInfo) { var deleted = parseInt(topicData.deleted, 10) !== 0; + return !deleted || (deleted && topicInfo.privileges.view_deleted) || topicData.uid === current_user; } @@ -738,7 +750,7 @@ var async = require('async'), if (isTopicVisible(topicData, topicInfo)) { retrieved_topics.push(topicData); - } + } else console.log('not visible'); next(null); });