|
|
|
@ -5,8 +5,6 @@ var async = require('async');
|
|
|
|
|
var nconf = require('nconf');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var privileges = require('../privileges');
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var topics = require('../topics');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
@ -19,9 +17,7 @@ var validFilter = {'': true, 'new': true, 'watched': true};
|
|
|
|
|
|
|
|
|
|
recentController.get = function (req, res, next) {
|
|
|
|
|
var page = parseInt(req.query.page, 10) || 1;
|
|
|
|
|
var pageCount = 1;
|
|
|
|
|
var stop = 0;
|
|
|
|
|
var topicCount = 0;
|
|
|
|
|
var settings;
|
|
|
|
|
var cid = req.query.cid;
|
|
|
|
|
var filter = req.params.filter || '';
|
|
|
|
@ -37,9 +33,6 @@ recentController.get = function (req, res, next) {
|
|
|
|
|
settings: function (next) {
|
|
|
|
|
user.getSettings(req.uid, next);
|
|
|
|
|
},
|
|
|
|
|
tids: function (next) {
|
|
|
|
|
db.getSortedSetRevRange(cid ? 'cid:' + cid + ':tids' : 'topics:recent', 0, 199, next);
|
|
|
|
|
},
|
|
|
|
|
watchedCategories: function (next) {
|
|
|
|
|
helpers.getWatchedCategories(req.uid, cid, next);
|
|
|
|
|
}
|
|
|
|
@ -48,25 +41,17 @@ recentController.get = function (req, res, next) {
|
|
|
|
|
function (results, next) {
|
|
|
|
|
settings = results.settings;
|
|
|
|
|
categoryData = results.watchedCategories;
|
|
|
|
|
filterTids(results.tids, req.uid, categoryData.categories, filter, next);
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
|
|
|
|
|
var start = Math.max(0, (page - 1) * settings.topicsPerPage);
|
|
|
|
|
stop = start + settings.topicsPerPage - 1;
|
|
|
|
|
|
|
|
|
|
topicCount = tids.length;
|
|
|
|
|
pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
|
|
|
|
|
tids = tids.slice(start, stop + 1);
|
|
|
|
|
|
|
|
|
|
topics.getTopicsByTids(tids, req.uid, next);
|
|
|
|
|
topics.getRecentTopics(cid, req.uid, start, stop, filter, next);
|
|
|
|
|
}
|
|
|
|
|
], function (err, topics) {
|
|
|
|
|
], function (err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
|
data.topics = topics;
|
|
|
|
|
data.categories = categoryData.categories;
|
|
|
|
|
data.selectedCategory = categoryData.selectedCategory;
|
|
|
|
|
data.nextStart = stop + 1;
|
|
|
|
@ -95,50 +80,16 @@ recentController.get = function (req, res, next) {
|
|
|
|
|
return filter && filter.selected;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
|
|
|
|
|
data.pagination = pagination.create(page, pageCount, req.query);
|
|
|
|
|
|
|
|
|
|
if (req.path.startsWith('/api/recent') || req.path.startsWith('/recent')) {
|
|
|
|
|
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.querystring = cid ? ('?cid=' + validator.escape(String(cid))) : '';
|
|
|
|
|
res.render('recent', data);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function filterTids(tids, uid, watchedCategories, filter, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (filter === 'watched') {
|
|
|
|
|
topics.filterWatchedTids(tids, uid, next);
|
|
|
|
|
} else if (filter === 'new') {
|
|
|
|
|
topics.filterNewTids(tids, uid, next);
|
|
|
|
|
} else {
|
|
|
|
|
topics.filterNotIgnoredTids(tids, uid, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
privileges.topics.filterTids('read', tids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
topics.getTopicsFields(tids, ['tid', 'cid'], next);
|
|
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
|
|
|
|
var watchedCids = watchedCategories.map(function (category) {
|
|
|
|
|
return category && parseInt(category.cid, 10);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tids = topicData.filter(function (topic, index) {
|
|
|
|
|
if (topic) {
|
|
|
|
|
var topicCid = parseInt(topic.cid, 10);
|
|
|
|
|
return watchedCids.indexOf(topicCid) !== -1;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}).map(function (topic) {
|
|
|
|
|
return topic.tid;
|
|
|
|
|
});
|
|
|
|
|
next(null, tids);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = recentController;
|