This commit is contained in:
barisusakli
2016-05-09 23:39:00 +03:00
parent 6392cd31df
commit 69e25fe4d5
3 changed files with 46 additions and 1 deletions

View File

@@ -327,5 +327,37 @@ topicsController.teaser = function(req, res, next) {
});
};
topicsController.pagination = function(req, res, callback) {
var tid = req.params.topic_id;
var currentPage = parseInt(req.query.page, 10) || 1;
if (!utils.isNumber(tid)) {
return callback();
}
async.parallel({
privileges: async.apply(privileges.topics.get, tid, req.uid),
settings: async.apply(user.getSettings, req.uid),
topic: async.apply(topics.getTopicData, tid)
}, function (err, results) {
if (err || !results.topic) {
return callback(err);
}
if (!results.privileges.read || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) {
return helpers.notAllowed(req, res);
}
var postCount = parseInt(results.topic.postcount, 10);
var pageCount = Math.max(1, Math.ceil((postCount - 1) / results.settings.postsPerPage));
var paginationData = pagination.create(currentPage, pageCount);
paginationData.rel.forEach(function(rel) {
rel.href = nconf.get('url') + '/topic/' + results.topic.slug + rel.href;
});
res.json(paginationData);
});
};
module.exports = topicsController;