diff --git a/src/controllers/unread.js b/src/controllers/unread.js index d07c2d16a1..e7aacb2465 100644 --- a/src/controllers/unread.js +++ b/src/controllers/unread.js @@ -12,11 +12,17 @@ var plugins = require('../plugins'); var unreadController = {}; +var validFilter = {'': true, 'new': true}; + unreadController.get = function(req, res, next) { var stop = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; var results; var cid = req.query.cid; - var seen = !req.query.unseen; + var filter = req.params.filter || ''; + + if (!validFilter[filter]) { + return next(); + } async.waterfall([ function(next) { @@ -25,7 +31,7 @@ unreadController.get = function(req, res, next) { user.getWatchedCategories(req.uid, next); }, unreadTopics: function(next) { - topics.getUnreadTopics(cid, req.uid, 0, stop, seen, next); + topics.getUnreadTopics(cid, req.uid, 0, stop, filter, next); } }, next); }, @@ -65,7 +71,13 @@ unreadController.get = function(req, res, next) { unreadController.unreadTotal = function(req, res, next) { - topics.getTotalUnread(req.uid, !req.query.unseen, function (err, data) { + var filter = req.params.filter || ''; + + if (!validFilter[filter]) { + return next(); + } + + topics.getTotalUnread(req.uid, filter, function (err, data) { if (err) { return next(err); } diff --git a/src/routes/api.js b/src/routes/api.js index 34cf142b37..4806663460 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -22,7 +22,7 @@ module.exports = function(app, middleware, controllers) { router.get('/categories/:cid/moderators', controllers.api.getModerators); router.get('/recent/posts/:term?', controllers.api.getRecentPosts); - router.get('/unread/total', middleware.authenticate, controllers.unread.unreadTotal); + router.get('/unread/:filter?/total', middleware.authenticate, controllers.unread.unreadTotal); router.get('/topic/teaser/:topic_id', controllers.topics.teaser); var multipart = require('connect-multiparty'); diff --git a/src/routes/index.js b/src/routes/index.js index a80eb2e46a..539f26b3db 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -55,7 +55,7 @@ function categoryRoutes(app, middleware, controllers) { setupPageRoute(app, '/categories', middleware, [], controllers.categories.list); setupPageRoute(app, '/popular/:term?', middleware, [], controllers.popular.get); setupPageRoute(app, '/recent', middleware, [], controllers.recent.get); - setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.unread.get); + setupPageRoute(app, '/unread/:filter?', middleware, [middleware.authenticate], controllers.unread.get); setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [], controllers.category.get); setupPageRoute(app, '/category/:category_id/:slug?', middleware, [], controllers.category.get); diff --git a/src/topics/unread.js b/src/topics/unread.js index 713f4732fa..63941cd01a 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -14,21 +14,21 @@ var utils = require('../../public/src/utils'); module.exports = function(Topics) { - Topics.getTotalUnread = function(uid, allowSeen, callback) { + Topics.getTotalUnread = function(uid, filter, callback) { if (!callback) { - callback = allowSeen; - allowSeen = true; + callback = filter; + filter = ''; } - Topics.getUnreadTids(0, uid, 0, 99, allowSeen, function(err, tids) { + Topics.getUnreadTids(0, uid, 0, 99, filter, function(err, tids) { callback(err, tids ? tids.length : 0); }); }; - Topics.getUnreadTopics = function(cid, uid, start, stop, allowSeen, callback) { + Topics.getUnreadTopics = function(cid, uid, start, stop, filter, callback) { if (!callback) { - callback = allowSeen; - allowSeen = true; + callback = filter; + filter = ''; } @@ -40,7 +40,7 @@ module.exports = function(Topics) { async.waterfall([ function(next) { - Topics.getUnreadTids(cid, uid, start, stop, allowSeen, next); + Topics.getUnreadTids(cid, uid, start, stop, filter, next); }, function(tids, next) { if (!tids.length) { @@ -64,10 +64,10 @@ module.exports = function(Topics) { return Date.now() - (parseInt(meta.config.unreadCutoff, 10) || 2) * 86400000; }; - Topics.getUnreadTids = function(cid, uid, start, stop, allowSeen, callback) { + Topics.getUnreadTids = function(cid, uid, start, stop, filter, callback) { if (!callback) { - callback = allowSeen; - allowSeen = true; + callback = filter; + filter = ''; } uid = parseInt(uid, 10); @@ -110,7 +110,12 @@ module.exports = function(Topics) { }); var tids = results.recentTids.filter(function(recentTopic) { - return !userRead[recentTopic.value] || allowSeen && recentTopic.score > userRead[recentTopic.value]; + switch (filter) { + default: + return !userRead[recentTopic.value] || recentTopic.score > userRead[recentTopic.value]; + case 'new': + return !userRead[recentTopic.value]; + } }).map(function(topic) { return topic.value; }).filter(function(tid, index, array) {