diff --git a/src/controllers/categories.js b/src/controllers/categories.js index a0fb8db212..398ec070aa 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -5,15 +5,16 @@ var categoriesController = {}, qs = require('querystring'), nconf = require('nconf'), privileges = require('../privileges'), - user = require('./../user'), - categories = require('./../categories'), - topics = require('./../topics'), - meta = require('./../meta'); + user = require('../user'), + categories = require('../categories'), + topics = require('../topics'), + meta = require('../meta'); categoriesController.recent = function(req, res, next) { var uid = req.user ? req.user.uid : 0; - topics.getLatestTopics(uid, 0, 19, req.params.term, function (err, data) { - if(err) { + var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; + topics.getLatestTopics(uid, 0, end, req.params.term, function (err, data) { + if (err) { return next(err); } @@ -28,8 +29,8 @@ categoriesController.popular = function(req, res, next) { var term = req.params.term || 'daily'; - topics.getPopular(term, uid, function(err, data) { - if(err) { + topics.getPopular(term, uid, meta.config.topicsPerList, function(err, data) { + if (err) { return next(err); } @@ -41,12 +42,12 @@ categoriesController.popular = function(req, res, next) { categoriesController.unread = function(req, res, next) { var uid = req.user ? req.user.uid : 0; - - topics.getUnreadTopics(uid, 0, 20, function (err, data) { - if(err) { + var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; + topics.getUnreadTopics(uid, 0, end, function (err, data) { + if (err) { return next(err); } - + data.topics = data.topics.slice(0, parseInt(meta.config.topicsPerList, 10) || 20); res.render('unread', data); }); }; diff --git a/src/controllers/tags.js b/src/controllers/tags.js index 74c21a17fa..258a28b6c9 100644 --- a/src/controllers/tags.js +++ b/src/controllers/tags.js @@ -3,13 +3,14 @@ var tagsController = {}, async = require('async'), nconf = require('nconf'), - topics = require('./../topics'); + meta = require('../meta'), + topics = require('../topics'); tagsController.getTag = function(req, res, next) { var tag = req.params.tag; var uid = req.user ? req.user.uid : 0; - - topics.getTagTids(tag, 0, 19, function(err, tids) { + var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; + topics.getTagTids(tag, 0, end, function(err, tids) { if (err) { return next(err); } diff --git a/src/postTools.js b/src/postTools.js index 97d3e71928..a162561740 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -54,33 +54,44 @@ var winston = require('winston'), if (err) { return next(err); } + options.tags = options.tags || []; - if (isMainPost) { - title = title.trim(); - - var topicData = { - title: title, - slug: tid + '/' + utils.slugify(title) - }; - if (options.topic_thumb) { - topicData.thumb = options.topic_thumb; - } - db.setObject('topic:' + tid, topicData, function(err) { - plugins.fireHook('action:topic.edit', tid); + if (!isMainPost) { + return next(null, { + tid: tid, + isMainPost: false }); + } - topics.updateTags(tid, options.tags); + title = title.trim(); + + var topicData = { + title: title, + slug: tid + '/' + utils.slugify(title) + }; + if (options.topic_thumb) { + topicData.thumb = options.topic_thumb; } - next(null, { - tid: tid, - title: validator.escape(title), - isMainPost: isMainPost, - tags: options.tags.map(function(tag) { return {name:tag}; }) + db.setObject('topic:' + tid, topicData, function(err) { + plugins.fireHook('action:topic.edit', tid); }); - }); + topics.updateTags(tid, options.tags, function(err) { + if (err) { + return next(err); + } + topics.getTopicTagsObjects(tid, function(err, tags) { + next(err, { + tid: tid, + title: validator.escape(title), + isMainPost: isMainPost, + tags: tags + }); + }); + }); + }); }, content: function(next) { PostTools.parse(postData.content, next); diff --git a/src/topics/popular.js b/src/topics/popular.js index 895849eae8..167bf1cb83 100644 --- a/src/topics/popular.js +++ b/src/topics/popular.js @@ -8,7 +8,8 @@ var async = require('async'), module.exports = function(Topics) { - Topics.getPopular = function(term, uid, callback) { + Topics.getPopular = function(term, uid, count, callback) { + count = parseInt(count, 10) || 20; var terms = { daily: 'day', weekly: 'week', @@ -17,7 +18,7 @@ module.exports = function(Topics) { }; if (term === 'alltime') { - return getAllTimePopular(uid, callback); + return getAllTimePopular(uid, count, callback); } var since = terms[term] || 'day'; @@ -27,7 +28,7 @@ module.exports = function(Topics) { Topics.getLatestTids(0, -1, since, next); }, function(tids, next) { - getTopics(tids, uid, next); + getTopics(tids, uid, count, next); }, function(topics, next) { var tids = topics.map(function(topic) { @@ -49,13 +50,13 @@ module.exports = function(Topics) { ], callback); }; - function getAllTimePopular(uid, callback) { - Topics.getTopicsFromSet(uid, 'topics:posts', 0, 19, function(err, data) { + function getAllTimePopular(uid, count, callback) { + Topics.getTopicsFromSet(uid, 'topics:posts', 0, count - 1, function(err, data) { callback(err, data ? data.topics : null); }); } - function getTopics(tids, uid, callback) { + function getTopics(tids, uid, count, callback) { var keys = tids.map(function(tid) { return 'topic:' + tid; }); @@ -69,7 +70,7 @@ module.exports = function(Topics) { return parseInt(b.postcount, 10) - parseInt(a.postcount, 10); }); - topics = topics.slice(0, 20).map(function(topic) { + topics = topics.slice(0, count).map(function(topic) { return topic.tid; }); diff --git a/src/topics/tags.js b/src/topics/tags.js index 7a909bd35b..9feb917677 100644 --- a/src/topics/tags.js +++ b/src/topics/tags.js @@ -132,16 +132,19 @@ module.exports = function(Topics) { }); } - Topics.updateTags = function(tid, tags) { + Topics.updateTags = function(tid, tags, callback) { + callback = callback || function() {}; Topics.getTopicField(tid, 'timestamp', function(err, timestamp) { if (err) { - return winston.error(err.message); + return callback(err); } Topics.deleteTopicTags(tid, function(err) { - if (!err) { - Topics.createTags(tags, tid, timestamp); + if (err) { + return callback(err); } + + Topics.createTags(tags, tid, timestamp, callback); }); }); }; diff --git a/src/topics/unread.js b/src/topics/unread.js index 2754fa9a88..fce061cd07 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -4,10 +4,11 @@ var async = require('async'), winston = require('winston'), - db = require('./../database'), - user = require('./../user'), - notifications = require('./../notifications'), - categories = require('./../categories'), + db = require('../database'), + user = require('../user'), + meta = require('../meta'), + notifications = require('../notifications'), + categories = require('../categories'), privileges = require('../privileges'); module.exports = function(Topics) { @@ -27,8 +28,14 @@ module.exports = function(Topics) { return callback(null, unreadTids); } + var count = 0; + if (stop === -1) { + count = Infinity; + } else { + count = stop - start + 1; + } async.whilst(function() { - return unreadTids.length < 21 && !done; + return unreadTids.length < count && !done; }, function(next) { Topics.getLatestTids(start, stop, 'month', function(err, tids) { if (err) { @@ -49,19 +56,21 @@ module.exports = function(Topics) { return !read[index]; }); - unreadTids.push.apply(unreadTids, newtids); + privileges.topics.filter('read', newtids, uid, function(err, newtids) { + if (err) { + return next(err); + } + unreadTids.push.apply(unreadTids, newtids); - start = stop + 1; - stop = start + 19; + start = stop + 1; + stop = start + 19; - next(); + next(); + }); }); }); }, function(err) { - if (err) { - return callback(err); - } - privileges.topics.filter('read', unreadTids, uid, callback); + callback(err, unreadTids.slice(0, count)); }); }; @@ -162,7 +171,6 @@ module.exports = function(Topics) { }; Topics.markAsRead = function(tid, uid, callback) { - db.setAdd('tid:' + tid + ':read_by_uid', uid, function(err) { if (err) { return callback(err);