Files
NodeBB/src/categories/recentreplies.js

257 lines
6.1 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'),
2014-08-14 09:59:40 -04:00
_ = require('underscore'),
2014-04-24 18:59:19 -04:00
2015-07-10 16:43:25 -04:00
meta = require('../meta'),
2014-06-24 21:57:33 -04:00
db = require('../database'),
posts = require('../posts'),
topics = require('../topics'),
privileges = require('../privileges'),
plugins = require('../plugins');
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) {
return callback(null, []);
}
2014-08-14 09:59:40 -04:00
async.waterfall([
function(next) {
async.map(categoryData, getRecentTopicPids, next);
},
function(results, next) {
var pids = _.flatten(results);
2014-08-14 09:59:40 -04:00
pids = pids.filter(function(pid, index, array) {
return !!pid && array.indexOf(pid) === index;
});
privileges.posts.filter('read', pids, uid, next);
},
function(pids, next) {
2015-07-10 16:43:25 -04:00
if (meta.config.teaserPost === 'first') {
getMainPosts(pids, uid, next);
} else {
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
}
},
function(posts, next) {
2015-08-19 15:53:37 -04:00
assignPostsToCategories(categoryData, posts);
bubbleUpChildrenPosts(categoryData);
next();
}
], callback);
2014-08-14 09:59:40 -04:00
};
2015-07-10 16:43:25 -04:00
function getMainPosts(pids, uid, callback) {
async.waterfall([
function(next) {
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
db.getObjectsFields(keys, ['tid'], next);
},
function(posts, next) {
var keys = posts.map(function(post) {
return 'topic:' + post.tid;
});
db.getObjectsFields(keys, ['mainPid'], next);
},
function(topics, next) {
var mainPids = topics.map(function(topic) {
return topic.mainPid;
});
posts.getPostSummaryByPids(mainPids, uid, {stripTags: true}, next);
}
], callback);
}
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.timestamp - a.timestamp;
});
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);
});
}
2015-08-19 15:53:37 -04:00
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));
});
2014-08-14 09:59:40 -04:00
}
2014-08-14 10:16:43 -04:00
function getRecentTopicPids(category, callback) {
var count = parseInt(category.numRecentReplies, 10);
2014-07-22 17:58:27 -04:00
if (!count) {
return callback(null, []);
}
2014-11-07 17:15:01 -05:00
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, function(err, pids) {
2014-08-14 09:59:40 -04:00
if (err || !Array.isArray(pids) || !pids.length) {
return callback(err, []);
}
2014-07-22 17:58:27 -04:00
if (count === 1) {
2014-08-14 09:59:40 -04:00
return callback(null, pids);
2014-07-22 17:58:27 -04:00
}
async.parallel({
pinnedTids: function(next) {
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
2014-07-22 17:58:27 -04:00
},
tids: function(next) {
2014-11-07 17:15:01 -05:00
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(0, count), Date.now(), 0, next);
2014-07-22 17:58:27 -04:00
}
}, function(err, results) {
if (err) {
return callback(err);
}
results.tids = results.tids.concat(results.pinnedTids);
2014-07-22 17:58:27 -04:00
async.map(results.tids, topics.getLatestUndeletedPid, function(err, topicPids) {
if (err) {
return callback(err);
}
2014-07-22 17:58:27 -04:00
pids = pids.concat(topicPids).filter(function(pid, index, array) {
return !!pid && array.indexOf(pid) === index;
2015-02-04 15:49:08 -05:00
}).sort(function(a, b) {
return b - a;
}).slice(0, count);
2014-08-14 09:59:40 -04:00
callback(null, pids);
2014-07-22 17:58:27 -04:00
});
});
});
2014-08-14 09:59:40 -04:00
}
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
};