Files
NodeBB/src/categories/recentreplies.js

66 lines
1.4 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-03-12 21:41:53 -04:00
db = require('./../database'),
posts = require('./../posts'),
2014-05-15 20:49:47 -04:00
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
}
posts.getPostSummaryByPids(pids, 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
}
topics.getPids(tid, function(err, pids) {
2014-04-24 18:59:19 -04:00
if (err) {
return winston.error(err.message);
}
if (pids && !pids.length) {
return;
}
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
});
};
};