Files
NodeBB/src/categories/recentreplies.js

195 lines
4.7 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
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) {
privileges.categories.can('read', cid, uid, function(err, canRead) {
if (err || !canRead || !parseInt(count, 10)) {
return callback(err, []);
2014-03-12 21:41:53 -04:00
}
db.getSortedSetRevRange('cid:' + cid + ':pids', 0, count - 1, function(err, pids) {
if (err || !Array.isArray(pids) || !pids.length) {
return callback(err, []);
}
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, 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) {
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
},
function(posts, next) {
categoryData.forEach(function(category) {
assignPostsToCategory(category, posts);
});
next();
}
], callback);
2014-08-14 09:59:40 -04:00
};
function assignPostsToCategory(category, posts) {
2014-08-14 09:59:40 -04:00
category.posts = posts.filter(function(post) {
2014-09-07 13:03:06 -04:00
return parseInt(post.category.cid, 10) === parseInt(category.cid, 10);
2014-08-14 09:59:40 -04:00
}).sort(function(a, b) {
2014-09-07 13:03:06 -04:00
return b.timestamp - a.timestamp;
2014-08-14 09:59:40 -04:00
}).slice(0, parseInt(category.numRecentReplies, 10));
}
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;
});
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-10-14 03:00:24 -04:00
var keys = movePids.map(function(pid) {
return 'post:' + pid;
2014-09-16 23:28:04 -04:00
});
2014-10-14 03:00:24 -04:00
db.getObjectsFields(keys, ['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
};