Switch to a named filter for unread to allow future expansion.

Don't use the query string as it is not passed to infinite scroll.
v1.18.x
Ben Lubar 9 years ago
parent a61d947295
commit 217f0c5652

@ -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);
}

@ -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');

@ -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);

@ -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) {

Loading…
Cancel
Save