Node.js based forum software built for the modern web
closes #1087
Baris Soner Usaklide34beaf6d89
diff --git a/src/categories.js b/src/categories.jsindex 2049b22c99..85315e103a 100644--- a/src/categories.js+++ b/src/categories.js@@ -48,18 +48,22 @@ var db = require('./database'),});};- Categories.getCategoryById = function(category_id, start, end, current_user, callback) {+ Categories.getCategoryById = function(cid, start, end, uid, callback) {++ if(parseInt(uid, 10)) {+ Categories.markAsRead(cid, uid);+ }function getCategoryData(next) {- Categories.getCategoryData(category_id, next);+ Categories.getCategoryData(cid, next);}function getTopics(next) {- Categories.getCategoryTopics(category_id, start, end, current_user, next);+ Categories.getCategoryTopics(cid, start, end, uid, next);}function getPageCount(next) {- Categories.getPageCount(category_id, current_user, next);+ Categories.getPageCount(cid, uid, next);}async.parallel({@@ -77,7 +81,7 @@ var db = require('./database'),'link': results.category.link,'disabled': results.category.disabled,'topic_row_size': 'col-md-9',- 'category_id': category_id,+ 'category_id': cid,'topics': results.topics.topics,'nextStart': results.topics.nextStart,'pageCount': results.pageCount,@@ -169,29 +173,6 @@ var db = require('./database'),});};- Categories.isTopicsRead = function(cid, uid, callback) {- db.getSortedSetRange('categories:' + cid + ':tid', 0, -1, function(err, tids) {- if(err) {- return callback(err);- }-- topics.hasReadTopics(tids, uid, function(err, hasRead) {- if(err) {- return callback(err);- }-- var allread = true;- for (var i = 0, ii = tids.length; i < ii; i++) {- if (hasRead[i] === 0) {- allread = false;- break;- }- }- callback(allread);- });- });- };-Categories.markAsRead = function(cid, uid) {db.setAdd('cid:' + cid + ':read_by_uid', uid);};diff --git a/src/database/mongo.js b/src/database/mongo.jsindex 63338856f6..7c8c50a46b 100644--- a/src/database/mongo.js+++ b/src/database/mongo.js@@ -614,11 +614,14 @@max = (args[1] === '+inf')?Number.MAX_VALUE:args[1],min = args[2],start = args[4],- stop = args[5];+ count = args[5];+ if(parseInt(count, 10) === -1) {+ count = 0;+ }db.collection('objects').find({_key:key, score: {$gte:min, $lte:max}}, {fields:{value:1}})- .limit(stop - start + 1)+ .limit(count).skip(start).sort({score: -1}).toArray(function(err, data) {diff --git a/src/routes/api.js b/src/routes/api.jsindex 12205e4353..1817cc15db 100644--- a/src/routes/api.js+++ b/src/routes/api.js@@ -232,24 +232,28 @@ var path = require('path'),end = start + settings.topicsPerPage - 1;categoryTools.privileges(req.params.id, uid, function(err, privileges) {- if (!err && privileges.read) {- categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {- if(err) {- return next(err);- }-- data.currentPage = page;- data.privileges = privileges;+ if (err) {+ return next(err);+ }- if (data && !data.disabled) {- res.json(data);- } else {- next();- }- }, req.params.id, uid);- } else {- res.send(403);+ if (!privileges.read) {+ return res.send(403);}++ categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {+ if(err) {+ return next(err);+ }++ data.currentPage = page;+ data.privileges = privileges;++ if (data && !data.disabled) {+ res.json(data);+ } else {+ next();+ }+ });});});});diff --git a/src/topics.js b/src/topics.jsindex 6ad23bee70..3080afa9cf 100644--- a/src/topics.js+++ b/src/topics.js@@ -460,10 +460,7 @@ var async = require('async'),});}- Topics.getLatestTopics = function(current_user, start, end, term, callback) {-- var timestamp = Date.now();-+ Topics.getLatestTids = function(start, end, term, callback) {var terms = {day: 86400000,week: 604800000,@@ -475,13 +472,17 @@ var async = require('async'),since = terms[term];}- var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1];- db.getSortedSetRevRangeByScore(args, function(err, tids) {- if (err) {+ var count = parseInt(end, 10) === -1 ? end : end - start + 1;++ db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);+ };++ Topics.getLatestTopics = function(uid, start, end, term, callback) {+ Topics.getLatestTids(start, end, term, function(err, tids) {+ if(err) {return callback(err);}-- getTopics('topics:recent', current_user, tids, callback);+ getTopics('topics:recent', uid, tids, callback);});};@@ -496,100 +497,54 @@ var async = require('async'),};Topics.getTotalUnread = function(uid, callback) {-- var unreadTids = [],- start = 0,- stop = 21,- done = false;-- async.whilst(- function() {- return unreadTids.length < 21 && !done;- },- function(callback) {- db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {-- if (err) {- return callback(err);- }-- if (tids && !tids.length) {- done = true;- return callback(null);- }-- Topics.hasReadTopics(tids, uid, function(err, read) {- if(err) {- return callback(err);- }- var newtids = tids.filter(function(tid, index, self) {- return read[index] === 0;- });-- unreadTids.push.apply(unreadTids, newtids);-- start = stop + 1;- stop = start + 21;- callback(null);- });- });- },- function(err) {- callback(null, {- count: unreadTids.length- });- }- );+ Topics.getUnreadTids(uid, 0, 21, function(err, tids) {+ callback(err, {count: tids ? tids.length : 0});+ });};Topics.getUnreadTids = function(uid, start, stop, callback) {var unreadTids = [],done = false;- function continueCondition() {- return unreadTids.length < 20 && !done;+ uid = parseInt(uid, 10);+ if(uid === 0) {+ return callback(null, unreadTids);}- async.whilst(continueCondition, function(callback) {- db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {+ async.whilst(function() {+ return unreadTids.length < 20 && !done;+ }, function(callback) {+ Topics.getLatestTids(start, stop, 'month', function(err, tids) {if (err) {return callback(err);}if (tids && !tids.length) {done = true;- return callback(null);+ return callback();}- if (uid === 0) {- unreadTids.push.apply(unreadTids, tids);- callback(null);- } else {- Topics.hasReadTopics(tids, uid, function(err, read) {- if(err) {- return callback(err);- }- var newtids = tids.filter(function(tid, index, self) {- return parseInt(read[index], 10) === 0;- });-+ Topics.hasReadTopics(tids, uid, function(err, read) {+ if(err) {+ return callback(err);+ }+ var newtids = tids.filter(function(tid, index, self) {+ return parseInt(read[index], 10) === 0;+ });- async.filter(newtids, function(tid, next) {- threadTools.privileges(tid, uid, function(err, privileges) {- next(!err && privileges.read);- });- }, function(newtids) {- unreadTids.push.apply(unreadTids, newtids);+ async.filter(newtids, function(tid, next) {+ threadTools.privileges(tid, uid, function(err, privileges) {+ next(!err && privileges.read);+ });+ }, function(newtids) {+ unreadTids.push.apply(unreadTids, newtids);- if(continueCondition()) {- start = stop + 1;- stop = start + 19;- }+ start = stop + 1;+ stop = start + 19;- callback(null);- });+ callback();});- }+ });});}, function(err) {callback(err, unreadTids);@@ -598,7 +553,6 @@ var async = require('async'),Topics.getUnreadTopics = function(uid, start, stop, callback) {var unreadTopics = {- 'show_sidebar': 'hidden','show_markallread_button': 'show','no_topics_message': 'hidden','topics': []@@ -915,13 +869,13 @@ var async = require('async'),};Topics.markAllRead = function(uid, callback) {- db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) {+ Topics.getLatestTids(0, -1, 'month', function(err, tids) {if (err) {return callback(err);}if(!tids || !tids.length) {- return callback(null);+ return callback();}function markRead(tid, next) {@@ -958,12 +912,7 @@ var async = require('async'),});Topics.getTopicField(tid, 'cid', function(err, cid) {-- categories.isTopicsRead(cid, uid, function(read) {- if (read) {- categories.markAsRead(cid, uid);- }- });+ categories.markAsRead(cid, uid);});user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) {diff --git a/src/webserver.js b/src/webserver.jsindex ea8e73521f..f1404651b0 100644--- a/src/webserver.js+++ b/src/webserver.js@@ -741,7 +741,6 @@ module.exports.server = server;end = start + settings.topicsPerPage - 1;categories.getCategoryById(cid, start, end, 0, function (err, categoryData) {-if (categoryData) {if (parseInt(categoryData.disabled, 10) === 1) {return next(new Error('Category disabled'), null);