You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.5 KiB
JavaScript

'use strict';
9 years ago
var async = require('async');
9 years ago
var nconf = require('nconf');
var validator = require('validator');
9 years ago
9 years ago
var user = require('../user');
9 years ago
var topics = require('../topics');
var meta = require('../meta');
var helpers = require('./helpers');
9 years ago
var pagination = require('../pagination');
var recentController = {};
var validFilter = {'': true, 'new': true, 'watched': true};
recentController.get = function (req, res, next) {
9 years ago
var page = parseInt(req.query.page, 10) || 1;
var stop = 0;
var settings;
var cid = req.query.cid;
var filter = req.params.filter || '';
var categoryData;
if (!validFilter[filter]) {
return next();
}
9 years ago
async.waterfall([
function (next) {
async.parallel({
settings: function (next) {
9 years ago
user.getSettings(req.uid, next);
},
watchedCategories: function (next) {
helpers.getWatchedCategories(req.uid, cid, next);
9 years ago
}
}, next);
},
function (results, next) {
settings = results.settings;
categoryData = results.watchedCategories;
9 years ago
var start = Math.max(0, (page - 1) * settings.topicsPerPage);
stop = start + settings.topicsPerPage - 1;
topics.getRecentTopics(cid, req.uid, start, stop, filter, next);
9 years ago
}
], function (err, data) {
if (err) {
return next(err);
}
9 years ago
data.categories = categoryData.categories;
data.selectedCategory = categoryData.selectedCategory;
9 years ago
data.nextStart = stop + 1;
data.set = 'topics:recent';
9 years ago
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
data.rssFeedUrl = nconf.get('relative_path') + '/recent.rss';
data.title = '[[pages:recent]]';
data.filters = [{
name: '[[unread:all-topics]]',
url: 'recent',
selected: filter === '',
filter: ''
}, {
name: '[[unread:new-topics]]',
url: 'recent/new',
selected: filter === 'new',
filter: 'new'
}, {
name: '[[unread:watched-topics]]',
url: 'recent/watched',
selected: filter === 'watched',
filter: 'watched'
}];
data.selectedFilter = data.filters.find(function (filter) {
return filter && filter.selected;
});
var pageCount = Math.max(1, Math.ceil(data.topicCount / settings.topicsPerPage));
data.pagination = pagination.create(page, pageCount, req.query);
9 years ago
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))) : '';
9 years ago
res.render('recent', data);
});
};
module.exports = recentController;