From 366ab103a460fcf53990ee44f20fe05bfe7b5c8a Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 27 Jan 2017 20:37:54 +0300 Subject: [PATCH] closes #5405 --- src/controllers/unread.js | 10 +++++++- src/socket.io/topics/infinitescroll.js | 2 +- src/socket.io/topics/unread.js | 2 +- src/topics/unread.js | 33 ++++++++++++-------------- test/topics.js | 8 +++---- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/controllers/unread.js b/src/controllers/unread.js index 83de85a67e..8be52f955c 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -40,7 +40,15 @@ unreadController.get = function (req, res, next) { settings = results.settings; var start = Math.max(0, (page - 1) * settings.topicsPerPage); var stop = start + settings.topicsPerPage - 1; - topics.getUnreadTopics(cid, req.uid, start, stop, filter, next); + var cutoff = req.session.unreadCutoff ? req.session.unreadCutoff : topics.unreadCutoff(); + topics.getUnreadTopics({ + cid: cid, + uid: req.uid, + start: start, + stop: stop, + filter: filter, + cutoff: cutoff + }, next); } ], function (err, data) { if (err) { diff --git a/src/socket.io/topics/infinitescroll.js b/src/socket.io/topics/infinitescroll.js index a68d220609..3efa621656 100644 --- a/src/socket.io/topics/infinitescroll.js +++ b/src/socket.io/topics/infinitescroll.js @@ -98,7 +98,7 @@ module.exports = function (SocketTopics) { var start = parseInt(data.after, 10); var stop = start + 9; - topics.getUnreadTopics(data.cid, socket.uid, start, stop, data.filter, callback); + topics.getUnreadTopics({cid: data.cid, uid: socket.uid, start: start, stop: stop, filter: data.filter}, callback); }; SocketTopics.loadMoreRecentTopics = function (socket, data, callback) { diff --git a/src/socket.io/topics/unread.js b/src/socket.io/topics/unread.js index 39c6485a26..d5a010af14 100644 --- a/src/socket.io/topics/unread.js +++ b/src/socket.io/topics/unread.js @@ -51,7 +51,7 @@ module.exports = function (SocketTopics) { SocketTopics.markCategoryTopicsRead = function (socket, cid, callback) { async.waterfall([ function (next) { - topics.getUnreadTids(cid, socket.uid, '', next); + topics.getUnreadTids({cid: cid, uid: socket.uid, filter: ''}, next); }, function (tids, next) { SocketTopics.markAsRead(socket, tids, next); diff --git a/src/topics/unread.js b/src/topics/unread.js index e61ca46d59..a754fa0f99 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -18,14 +18,13 @@ module.exports = function (Topics) { callback = filter; filter = ''; } - Topics.getUnreadTids(0, uid, filter, function (err, tids) { + Topics.getUnreadTids({cid: 0, uid: uid, filter: filter}, function (err, tids) { callback(err, Array.isArray(tids) ? tids.length : 0); }); }; - Topics.getUnreadTopics = function (cid, uid, start, stop, filter, callback) { - + Topics.getUnreadTopics = function (params, callback) { var unreadTopics = { showSelect: true, nextStart : 0, @@ -34,7 +33,7 @@ module.exports = function (Topics) { async.waterfall([ function (next) { - Topics.getUnreadTids(cid, uid, filter, next); + Topics.getUnreadTids(params, next); }, function (tids, next) { unreadTopics.topicCount = tids.length; @@ -43,13 +42,13 @@ module.exports = function (Topics) { return next(null, []); } - if (stop === -1) { - tids = tids.slice(start); + if (params.stop === -1) { + tids = tids.slice(params.start); } else { - tids = tids.slice(start, stop + 1); + tids = tids.slice(params.start, params.stop + 1); } - Topics.getTopicsByTids(tids, uid, next); + Topics.getTopicsByTids(tids, params.uid, next); }, function (topicData, next) { if (!Array.isArray(topicData) || !topicData.length) { @@ -57,7 +56,7 @@ module.exports = function (Topics) { } unreadTopics.topics = topicData; - unreadTopics.nextStart = stop + 1; + unreadTopics.nextStart = params.stop + 1; next(null, unreadTopics); } ], callback); @@ -67,21 +66,19 @@ module.exports = function (Topics) { return Date.now() - (parseInt(meta.config.unreadCutoff, 10) || 2) * 86400000; }; - Topics.getUnreadTids = function (cid, uid, filter, callback) { - uid = parseInt(uid, 10); + Topics.getUnreadTids = function (params, callback) { + var uid = parseInt(params.uid, 10); if (uid === 0) { return callback(null, []); } - - var cutoff = Topics.unreadCutoff(); - + var cutoff = params.cutoff || Topics.unreadCutoff(); var ignoredCids; async.waterfall([ function (next) { async.parallel({ ignoredCids: function (next) { - if (filter === 'watched') { + if (params.filter === 'watched') { return next(null, []); } user.getIgnoredCategories(uid, next); @@ -121,7 +118,7 @@ module.exports = function (Topics) { if (results.ignoredTids.indexOf(recentTopic.value.toString()) !== -1) { return false; } - switch (filter) { + switch (params.filter) { case 'new': return !userRead[recentTopic.value]; default: @@ -133,7 +130,7 @@ module.exports = function (Topics) { return array.indexOf(tid) === index; }); - if (filter === 'watched') { + if (params.filter === 'watched') { Topics.filterWatchedTids(tids, uid, next); } else { next(null, tids); @@ -143,7 +140,7 @@ module.exports = function (Topics) { tids = tids.slice(0, 200); - filterTopics(uid, tids, cid, ignoredCids, filter, next); + filterTopics(uid, tids, params.cid, ignoredCids, params.filter, next); } ], callback); }; diff --git a/test/topics.js b/test/topics.js index 3a192b9e94..38881c4d58 100644 --- a/test/topics.js +++ b/test/topics.js @@ -445,7 +445,7 @@ describe('Topic\'s', function () { assert.equal(pinnedTids[1], tid2); done(); }); - }); + }); }); }); @@ -482,7 +482,7 @@ describe('Topic\'s', function () { topics.ignore( newTid, uid, done ); }, function (done) { - topics.getUnreadTopics(0, uid, 0, -1, '', done ); + topics.getUnreadTopics({cid: 0, uid: uid, start: 0, stop: -1, filter: ''}, done ); }, function (results, done) { var topics = results.topics; @@ -526,7 +526,7 @@ describe('Topic\'s', function () { topics.follow( newTid, uid, done ); }, function (done) { - topics.getUnreadTopics(0, uid, 0, -1, '', done ); + topics.getUnreadTopics({cid: 0, uid: uid, start: 0, stop: -1, filter: ''}, done ); }, function (results, done) { var topics = results.topics; @@ -546,7 +546,7 @@ describe('Topic\'s', function () { topics.follow( newTid, uid, done ); }, function (done) { - topics.getUnreadTopics(0, uid, 0, -1, '', done ); + topics.getUnreadTopics({cid: 0, uid: uid, start: 0, stop: -1, filter: ''}, done ); }, function (results, done) { var topics = results.topics;