Files
NodeBB/src/categories/recentreplies.js

98 lines
2.2 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-06-24 21:57:33 -04:00
db = require('../database'),
posts = require('../posts'),
topics = require('../topics');
2014-03-12 21:41:53 -04:00
module.exports = function(Categories) {
Categories.getRecentReplies = function(cid, uid, count, callback) {
if(!parseInt(count, 10)) {
2014-03-12 21:41:53 -04:00
return callback(null, []);
}
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
if (err || !pids || !pids.length) {
return callback(err, []);
2014-03-12 21:41:53 -04:00
}
2014-07-07 17:36:10 -04:00
posts.getPostSummaryByPids(pids, {stripTags: true}, callback);
2014-03-12 21:41:53 -04:00
});
};
Categories.moveRecentReplies = function(tid, oldCid, cid) {
2014-04-24 18:59:19 -04:00
function movePost(postData, next) {
async.parallel([
function(next) {
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, postData.pid, next);
},
function(next) {
db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid, next);
2014-03-12 21:41:53 -04:00
}
2014-04-24 18:59:19 -04:00
], next);
2014-03-12 21:41:53 -04:00
}
2014-06-17 13:11:09 -04:00
updatePostCount(tid, oldCid, cid);
async.parallel({
mainPid: function(next) {
topics.getTopicField(tid, 'mainPid', next);
},
pids: function(next) {
topics.getPids(tid, next);
}
}, function(err, results) {
2014-04-24 18:59:19 -04:00
if (err) {
return winston.error(err.message);
}
2014-06-24 21:57:33 -04:00
if (!results.mainPid && results.pids && !results.pids.length) {
2014-04-24 18:59:19 -04:00
return;
}
var pids = [results.mainPid].concat(results.pids);
2014-04-24 18:59:19 -04:00
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
db.getObjectsFields(keys, ['pid', 'timestamp'], function(err, postData) {
if (err) {
return winston.error(err.message);
}
async.each(postData, movePost, function(err) {
if (err) {
winston.error(err.message);
}
});
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);
}
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
};