|
|
|
@ -3,14 +3,13 @@
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
|
winston = require('winston'),
|
|
|
|
|
validator = require('validator'),
|
|
|
|
|
_ = require('underscore'),
|
|
|
|
|
|
|
|
|
|
meta = require('../meta'),
|
|
|
|
|
db = require('../database'),
|
|
|
|
|
posts = require('../posts'),
|
|
|
|
|
topics = require('../topics'),
|
|
|
|
|
privileges = require('../privileges'),
|
|
|
|
|
plugins = require('../plugins');
|
|
|
|
|
privileges = require('../privileges');
|
|
|
|
|
|
|
|
|
|
module.exports = function(Categories) {
|
|
|
|
|
Categories.getRecentReplies = function(cid, uid, count, callback) {
|
|
|
|
@ -38,25 +37,21 @@ module.exports = function(Categories) {
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
async.map(categoryData, getRecentTopicPids, next);
|
|
|
|
|
async.map(categoryData, getRecentTopicTids, next);
|
|
|
|
|
},
|
|
|
|
|
function(results, next) {
|
|
|
|
|
var pids = _.flatten(results);
|
|
|
|
|
var tids = _.flatten(results);
|
|
|
|
|
|
|
|
|
|
pids = pids.filter(function(pid, index, array) {
|
|
|
|
|
return !!pid && array.indexOf(pid) === index;
|
|
|
|
|
tids = tids.filter(function(tid, index, array) {
|
|
|
|
|
return !!tid && array.indexOf(tid) === index;
|
|
|
|
|
});
|
|
|
|
|
privileges.posts.filter('read', pids, uid, next);
|
|
|
|
|
privileges.topics.filterTids('read', tids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function(pids, next) {
|
|
|
|
|
if (meta.config.teaserPost === 'first') {
|
|
|
|
|
getMainPosts(pids, uid, next);
|
|
|
|
|
} else {
|
|
|
|
|
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
|
|
|
|
|
}
|
|
|
|
|
function(tids, next) {
|
|
|
|
|
getTopics(tids, next);
|
|
|
|
|
},
|
|
|
|
|
function(posts, next) {
|
|
|
|
|
assignPostsToCategories(categoryData, posts);
|
|
|
|
|
function(topics, next) {
|
|
|
|
|
assignTopicsToCategories(categoryData, topics);
|
|
|
|
|
|
|
|
|
|
bubbleUpChildrenPosts(categoryData);
|
|
|
|
|
|
|
|
|
@ -65,29 +60,86 @@ module.exports = function(Categories) {
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getMainPosts(pids, uid, callback) {
|
|
|
|
|
function getRecentTopicTids(category, callback) {
|
|
|
|
|
var count = parseInt(category.numRecentReplies, 10);
|
|
|
|
|
if (!count) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count === 1) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, next);
|
|
|
|
|
},
|
|
|
|
|
function (pid, next) {
|
|
|
|
|
posts.getPostField(pid, 'tid', next);
|
|
|
|
|
},
|
|
|
|
|
function (tid, next) {
|
|
|
|
|
next(null, [tid]);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
pinnedTids: function(next) {
|
|
|
|
|
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
|
|
|
|
|
},
|
|
|
|
|
tids: function(next) {
|
|
|
|
|
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(1, count), Date.now(), 0, next);
|
|
|
|
|
}
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.tids = results.tids.concat(results.pinnedTids);
|
|
|
|
|
|
|
|
|
|
callback(null, results.tids);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTopics(tids, callback) {
|
|
|
|
|
var topicData;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
|
|
|
|
var keys = pids.map(function(pid) {
|
|
|
|
|
return 'post:' + pid;
|
|
|
|
|
});
|
|
|
|
|
db.getObjectsFields(keys, ['tid'], next);
|
|
|
|
|
function (next) {
|
|
|
|
|
topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount'], next);
|
|
|
|
|
},
|
|
|
|
|
function(posts, next) {
|
|
|
|
|
var keys = posts.map(function(post) {
|
|
|
|
|
return 'topic:' + post.tid;
|
|
|
|
|
function (_topicData, next) {
|
|
|
|
|
topicData = _topicData;
|
|
|
|
|
topicData.forEach(function(topic) {
|
|
|
|
|
topic.teaserPid = topic.teaserPid || topic.mainPid;
|
|
|
|
|
});
|
|
|
|
|
db.getObjectsFields(keys, ['mainPid'], next);
|
|
|
|
|
|
|
|
|
|
topics.getTeasers(topicData, next);
|
|
|
|
|
},
|
|
|
|
|
function(topics, next) {
|
|
|
|
|
var mainPids = topics.map(function(topic) {
|
|
|
|
|
return topic.mainPid;
|
|
|
|
|
function (teasers, next) {
|
|
|
|
|
teasers.forEach(function(teaser, index) {
|
|
|
|
|
if (teaser) {
|
|
|
|
|
teaser.cid = topicData[index].cid;
|
|
|
|
|
teaser.tid = teaser.uid = teaser.user.uid = undefined;
|
|
|
|
|
teaser.topic = {
|
|
|
|
|
slug: topicData[index].slug,
|
|
|
|
|
title: validator.escape(topicData[index].title)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
posts.getPostSummaryByPids(mainPids, uid, {stripTags: true}, next);
|
|
|
|
|
teasers = teasers.filter(Boolean);
|
|
|
|
|
next(null, teasers);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assignTopicsToCategories(categories, topics) {
|
|
|
|
|
categories.forEach(function(category) {
|
|
|
|
|
category.posts = topics.filter(function(topic) {
|
|
|
|
|
return topic.cid && parseInt(topic.cid, 10) === parseInt(category.cid, 10);
|
|
|
|
|
}).sort(function(a, b) {
|
|
|
|
|
return b.pid - a.pid;
|
|
|
|
|
}).slice(0, parseInt(category.numRecentReplies, 10));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bubbleUpChildrenPosts(categoryData) {
|
|
|
|
|
categoryData.forEach(function(category) {
|
|
|
|
|
if (category.posts.length) {
|
|
|
|
@ -97,7 +149,7 @@ module.exports = function(Categories) {
|
|
|
|
|
getPostsRecursive(category, posts);
|
|
|
|
|
|
|
|
|
|
posts.sort(function(a, b) {
|
|
|
|
|
return b.timestamp - a.timestamp;
|
|
|
|
|
return b.pid - a.pid;
|
|
|
|
|
});
|
|
|
|
|
if (posts.length) {
|
|
|
|
|
category.posts = [posts[0]];
|
|
|
|
@ -115,64 +167,6 @@ module.exports = function(Categories) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assignPostsToCategories(categories, posts) {
|
|
|
|
|
categories.forEach(function(category) {
|
|
|
|
|
category.posts = posts.filter(function(post) {
|
|
|
|
|
return post.category && (parseInt(post.category.cid, 10) === parseInt(category.cid, 10) ||
|
|
|
|
|
parseInt(post.category.parentCid, 10) === parseInt(category.cid, 10));
|
|
|
|
|
}).sort(function(a, b) {
|
|
|
|
|
return b.timestamp - a.timestamp;
|
|
|
|
|
}).slice(0, parseInt(category.numRecentReplies, 10));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRecentTopicPids(category, callback) {
|
|
|
|
|
var count = parseInt(category.numRecentReplies, 10);
|
|
|
|
|
if (!count) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, function(err, pids) {
|
|
|
|
|
if (err || !Array.isArray(pids) || !pids.length) {
|
|
|
|
|
return callback(err, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count === 1) {
|
|
|
|
|
return callback(null, pids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
pinnedTids: function(next) {
|
|
|
|
|
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
|
|
|
|
|
},
|
|
|
|
|
tids: function(next) {
|
|
|
|
|
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(0, count), Date.now(), 0, next);
|
|
|
|
|
}
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.tids = results.tids.concat(results.pinnedTids);
|
|
|
|
|
|
|
|
|
|
async.map(results.tids, topics.getLatestUndeletedPid, function(err, topicPids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pids = pids.concat(topicPids).filter(function(pid, index, array) {
|
|
|
|
|
return !!pid && array.indexOf(pid) === index;
|
|
|
|
|
}).sort(function(a, b) {
|
|
|
|
|
return b - a;
|
|
|
|
|
}).slice(0, count);
|
|
|
|
|
|
|
|
|
|
callback(null, pids);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Categories.moveRecentReplies = function(tid, oldCid, cid) {
|
|
|
|
|
updatePostCount(tid, oldCid, cid);
|
|
|
|
|
topics.getPids(tid, function(err, pids) {
|
|
|
|
|