Files
NodeBB/src/categories/recentreplies.js

251 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-03-12 21:41:53 -04:00
'use strict';
var async = require('async'),
2014-04-24 18:59:19 -04:00
winston = require('winston'),
validator = require('validator'),
2014-08-14 09:59:40 -04:00
_ = require('underscore'),
2014-04-24 18:59:19 -04:00
2014-06-24 21:57:33 -04:00
db = require('../database'),
posts = require('../posts'),
topics = require('../topics'),
privileges = require('../privileges');
2014-03-12 21:41:53 -04:00
module.exports = function(Categories) {
Categories.getRecentReplies = function(cid, uid, count, callback) {
2014-11-19 11:02:28 -05:00
if (!parseInt(count, 10)) {
2014-11-14 16:22:05 -05:00
return callback(null, []);
}
async.waterfall([
function(next) {
db.getSortedSetRevRange('cid:' + cid + ':pids', 0, count - 1, next);
},
function(pids, next) {
privileges.posts.filter('read', pids, uid, next);
},
function(pids, next) {
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
}
], callback);
2014-03-12 21:41:53 -04:00
};
2014-08-16 21:33:42 -04:00
Categories.getRecentTopicReplies = function(categoryData, uid, callback) {
2014-09-16 16:10:02 -04:00
if (!Array.isArray(categoryData) || !categoryData.length) {
2016-01-10 10:59:04 +02:00
return callback();
2014-09-16 16:10:02 -04:00
}
2014-08-14 09:59:40 -04:00
async.waterfall([
function(next) {
async.map(categoryData, getRecentTopicTids, next);
},
function(results, next) {
var tids = _.flatten(results);
2014-08-14 09:59:40 -04:00
tids = tids.filter(function(tid, index, array) {
return !!tid && array.indexOf(tid) === index;
});
privileges.topics.filterTids('read', tids, uid, next);
},
function(tids, next) {
getTopics(tids, next);
},
function(topics, next) {
assignTopicsToCategories(categoryData, topics);
2015-08-19 15:53:37 -04:00
bubbleUpChildrenPosts(categoryData);
next();
}
], callback);
2014-08-14 09:59:40 -04:00
};
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) {
2016-03-11 14:20:23 +02:00
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(1, count), Date.now(), '-inf', 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;
2015-07-10 16:43:25 -04:00
async.waterfall([
function (next) {
topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount'], next);
2015-07-10 16:43:25 -04:00
},
function (_topicData, next) {
topicData = _topicData;
topicData.forEach(function(topic) {
topic.teaserPid = topic.teaserPid || topic.mainPid;
2015-07-10 16:43:25 -04:00
});
topics.getTeasers(topicData, next);
2015-07-10 16:43:25 -04:00
},
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)
};
}
2015-07-10 16:43:25 -04:00
});
teasers = teasers.filter(Boolean);
next(null, teasers);
2015-07-10 16:43:25 -04:00
}
], 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));
});
}
2015-08-19 15:53:37 -04:00
function bubbleUpChildrenPosts(categoryData) {
categoryData.forEach(function(category) {
if (category.posts.length) {
return;
}
2015-08-20 14:23:20 -04:00
var posts = [];
getPostsRecursive(category, posts);
2015-08-19 15:53:37 -04:00
2015-08-20 14:23:20 -04:00
posts.sort(function(a, b) {
return b.pid - a.pid;
2015-08-20 14:23:20 -04:00
});
if (posts.length) {
category.posts = [posts[0]];
2015-08-19 15:53:37 -04:00
}
});
}
2015-08-20 14:23:20 -04:00
function getPostsRecursive(category, posts) {
category.posts.forEach(function(p) {
posts.push(p);
});
category.children.forEach(function(child) {
getPostsRecursive(child, posts);
});
}
Categories.moveRecentReplies = function(tid, oldCid, cid) {
2014-06-17 13:11:09 -04:00
updatePostCount(tid, oldCid, cid);
topics.getPids(tid, function(err, pids) {
2014-04-24 18:59:19 -04:00
if (err) {
return winston.error(err.message);
}
2014-09-16 23:28:04 -04:00
if (!Array.isArray(pids) || !pids.length) {
2014-04-24 18:59:19 -04:00
return;
}
2014-10-14 03:00:24 -04:00
var start = 0,
done = false,
batch = 50;
async.whilst(function() {
return !done;
}, function(next) {
var movePids = pids.slice(start, start + batch);
if (!movePids.length) {
done = true;
return next();
2014-04-24 18:59:19 -04:00
}
2014-09-16 23:28:04 -04:00
posts.getPostsFields(movePids, ['timestamp'], function(err, postData) {
if (err) {
2014-10-14 03:00:24 -04:00
return next(err);
}
2014-10-14 03:00:24 -04:00
var timestamps = postData.map(function(post) {
return post && post.timestamp;
});
async.parallel([
function(next) {
2014-11-07 17:15:01 -05:00
db.sortedSetRemove('cid:' + oldCid + ':pids', movePids, next);
2014-10-14 03:00:24 -04:00
},
function(next) {
2014-11-07 17:15:01 -05:00
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, movePids, next);
2014-10-14 03:00:24 -04:00
}
], function(err) {
if (err) {
return next(err);
}
start += batch;
next();
});
});
2014-10-14 03:00:24 -04:00
}, function(err) {
if (err) {
winston.error(err.stack);
}
2014-04-24 18:59:19 -04:00
});
2014-03-12 21:41:53 -04:00
});
};
2014-06-17 13:11:09 -04:00
function updatePostCount(tid, oldCid, newCid) {
topics.getTopicField(tid, 'postcount', function(err, postCount) {
if (err) {
return winston.error(err.message);
}
if (!parseInt(postCount, 10)) {
return;
}
2014-06-17 13:11:09 -04:00
async.parallel([
function(next) {
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
},
function(next) {
db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
}
], function(err) {
2014-06-23 16:22:05 -04:00
if (err) {
winston.error(err.message);
}
2014-06-17 13:11:09 -04:00
});
});
}
2014-03-12 21:41:53 -04:00
};