You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/categories/recentreplies.js

192 lines
4.5 KiB
JavaScript

'use strict';
var async = require('async'),
11 years ago
winston = require('winston'),
11 years ago
_ = require('underscore'),
11 years ago
db = require('../database'),
posts = require('../posts'),
topics = require('../topics');
module.exports = function(Categories) {
Categories.getRecentReplies = function(cid, uid, count, callback) {
if (!parseInt(count, 10)) {
return callback(null, []);
}
10 years ago
db.getSortedSetRevRange('cid:' + cid + ':pids', 0, count - 1, function(err, pids) {
if (err || !pids || !pids.length) {
return callback(err, []);
}
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
});
};
Categories.getRecentTopicReplies = function(categoryData, uid, callback) {
11 years ago
if (!Array.isArray(categoryData) || !categoryData.length) {
return callback(null, []);
}
11 years ago
async.map(categoryData, getRecentTopicPids, function(err, results) {
if (err) {
return callback(err);
}
11 years ago
11 years ago
var pids = _.flatten(results);
pids = pids.filter(function(pid, index, array) {
return !!pid && array.indexOf(pid) === index;
});
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, function(err, posts) {
11 years ago
if (err) {
return callback(err);
}
categoryData.forEach(function(category) {
assignPostsToCategory(category, posts);
});
callback();
11 years ago
});
});
};
function assignPostsToCategory(category, posts) {
11 years ago
category.posts = posts.filter(function(post) {
return parseInt(post.category.cid, 10) === parseInt(category.cid, 10);
11 years ago
}).sort(function(a, b) {
return b.timestamp - a.timestamp;
11 years ago
}).slice(0, parseInt(category.numRecentReplies, 10));
}
11 years ago
function getRecentTopicPids(category, callback) {
var count = parseInt(category.numRecentReplies, 10);
if (!count) {
return callback(null, []);
}
10 years ago
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, function(err, pids) {
11 years ago
if (err || !Array.isArray(pids) || !pids.length) {
return callback(err, []);
}
if (count === 1) {
11 years ago
return callback(null, pids);
}
async.parallel({
pinnedTids: function(next) {
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
},
tids: function(next) {
10 years ago
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;
});
11 years ago
callback(null, pids);
});
});
});
11 years ago
}
Categories.moveRecentReplies = function(tid, oldCid, cid) {
11 years ago
updatePostCount(tid, oldCid, cid);
topics.getPids(tid, function(err, pids) {
11 years ago
if (err) {
return winston.error(err.message);
}
11 years ago
if (!Array.isArray(pids) || !pids.length) {
11 years ago
return;
}
10 years ago
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();
11 years ago
}
10 years ago
var keys = movePids.map(function(pid) {
return 'post:' + pid;
11 years ago
});
10 years ago
db.getObjectsFields(keys, ['timestamp'], function(err, postData) {
if (err) {
10 years ago
return next(err);
}
10 years ago
var timestamps = postData.map(function(post) {
return post && post.timestamp;
});
async.parallel([
function(next) {
10 years ago
db.sortedSetRemove('cid:' + oldCid + ':pids', movePids, next);
10 years ago
},
function(next) {
10 years ago
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, movePids, next);
10 years ago
}
], function(err) {
if (err) {
return next(err);
}
start += batch;
next();
});
});
10 years ago
}, function(err) {
if (err) {
winston.error(err.stack);
}
11 years ago
});
});
};
11 years ago
function updatePostCount(tid, oldCid, newCid) {
topics.getTopicField(tid, 'postcount', function(err, postCount) {
if (err) {
return winston.error(err.message);
}
if (!parseInt(postCount, 10)) {
return;
}
11 years ago
async.parallel([
function(next) {
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
},
function(next) {
db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
}
], function(err) {
if (err) {
winston.error(err.message);
}
11 years ago
});
});
}
};