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

250 lines
6.5 KiB
JavaScript

'use strict';
9 years ago
var async = require('async');
var winston = require('winston');
var _ = require('underscore');
11 years ago
9 years ago
var db = require('../database');
var posts = require('../posts');
var topics = require('../topics');
var privileges = require('../privileges');
var batch = require('../batch');
module.exports = function (Categories) {
Categories.getRecentReplies = function (cid, uid, count, callback) {
if (!parseInt(count, 10)) {
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);
};
Categories.updateRecentTid = function (cid, tid, callback) {
async.parallel({
count: function (next) {
db.sortedSetCard('cid:' + cid + ':recent_tids', next);
},
numRecentReplies: function (next) {
db.getObjectField('category:' + cid, 'numRecentReplies', next);
},
}, function (err, results) {
if (err) {
return callback(err);
}
if (results.count < results.numRecentReplies) {
return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
}
async.waterfall([
function (next) {
db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next);
},
function (data, next) {
if (!data.length) {
return next();
}
db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
},
function (next) {
db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
},
], callback);
});
};
Categories.getRecentTopicReplies = function (categoryData, uid, callback) {
11 years ago
if (!Array.isArray(categoryData) || !categoryData.length) {
return callback();
11 years ago
}
async.waterfall([
function (next) {
var keys = categoryData.map(function (category) {
return 'cid:' + category.cid + ':recent_tids';
});
db.getSortedSetsMembers(keys, next);
},
function (results, next) {
var tids = _.flatten(results);
11 years ago
tids = tids.filter(function (tid, index, array) {
return !!tid && array.indexOf(tid) === index;
});
privileges.topics.filterTids('read', tids, uid, next);
},
function (tids, next) {
getTopics(tids, uid, next);
},
function (topics, next) {
assignTopicsToCategories(categoryData, topics);
10 years ago
bubbleUpChildrenPosts(categoryData);
next();
},
], callback);
11 years ago
};
function getTopics(tids, uid, callback) {
var topicData;
10 years ago
async.waterfall([
function (next) {
topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount'], next);
10 years ago
},
function (_topicData, next) {
9 years ago
topicData = _topicData;
topicData.forEach(function (topic) {
9 years ago
if (topic) {
topic.teaserPid = topic.teaserPid || topic.mainPid;
}
});
var cids = _topicData.map(function (topic) {
return topic && topic.cid;
}).filter(function (cid, index, array) {
return cid && array.indexOf(cid) === index;
});
async.parallel({
categoryData: async.apply(Categories.getCategoriesFields, cids, ['cid', 'parentCid']),
teasers: async.apply(topics.getTeasers, _topicData, uid),
}, next);
9 years ago
},
function (results, next) {
var parentCids = {};
results.categoryData.forEach(function (category) {
parentCids[category.cid] = category.parentCid;
});
results.teasers.forEach(function (teaser, index) {
if (teaser) {
teaser.cid = topicData[index].cid;
8 years ago
teaser.parentCid = parseInt(parentCids[teaser.cid], 10) || 0;
teaser.tid = undefined;
teaser.uid = undefined;
teaser.user.uid = undefined;
teaser.topic = {
slug: topicData[index].slug,
title: topicData[index].title,
};
}
10 years ago
});
results.teasers = results.teasers.filter(Boolean);
next(null, results.teasers);
},
10 years ago
], callback);
}
function assignTopicsToCategories(categories, topics) {
categories.forEach(function (category) {
category.posts = topics.filter(function (topic) {
return topic.cid && (parseInt(topic.cid, 10) === parseInt(category.cid, 10) ||
parseInt(topic.parentCid, 10) === parseInt(category.cid, 10));
}).sort(function (a, b) {
return b.pid - a.pid;
}).slice(0, parseInt(category.numRecentReplies, 10));
});
}
10 years ago
function bubbleUpChildrenPosts(categoryData) {
categoryData.forEach(function (category) {
10 years ago
if (category.posts.length) {
return;
}
10 years ago
var posts = [];
getPostsRecursive(category, posts);
10 years ago
posts.sort(function (a, b) {
return b.pid - a.pid;
10 years ago
});
if (posts.length) {
category.posts = [posts[0]];
10 years ago
}
});
}
10 years ago
function getPostsRecursive(category, posts) {
category.posts.forEach(function (p) {
10 years ago
posts.push(p);
});
category.children.forEach(function (child) {
10 years ago
getPostsRecursive(child, posts);
});
}
Categories.moveRecentReplies = function (tid, oldCid, cid, callback) {
callback = callback || function () {};
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;
}
batch.processArray(pids, function (pids, next) {
async.waterfall([
function (next) {
posts.getPostsFields(pids, ['timestamp'], next);
},
function (postData, next) {
var timestamps = postData.map(function (post) {
return post && post.timestamp;
});
async.parallel([
function (next) {
db.sortedSetRemove('cid:' + oldCid + ':pids', pids, next);
},
function (next) {
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids, next);
},
], next);
},
], next);
}, function (err) {
10 years ago
if (err) {
winston.error(err.stack);
}
callback(err);
11 years ago
});
});
};
11 years ago
function updatePostCount(tid, oldCid, newCid) {
topics.getTopicField(tid, 'postcount', function (err, postCount) {
11 years ago
if (err) {
return winston.error(err.message);
}
if (!parseInt(postCount, 10)) {
return;
}
11 years ago
async.parallel([
function (next) {
11 years ago
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
},
function (next) {
11 years ago
db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
},
], function (err) {
if (err) {
winston.error(err.message);
}
11 years ago
});
});
}
};