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/posts/category.js

85 lines
1.8 KiB
JavaScript

'use strict';
9 years ago
var async = require('async');
8 years ago
var _ = require('underscore');
9 years ago
var db = require('../database');
9 years ago
var topics = require('../topics');
module.exports = function (Posts) {
Posts.getCidByPid = function (pid, callback) {
async.waterfall([
function (next) {
Posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
topics.getTopicField(tid, 'cid', next);
},
], callback);
};
Posts.getCidsByPids = function (pids, callback) {
8 years ago
var tids;
var postData;
async.waterfall([
function (next) {
Posts.getPostsFields(pids, ['tid'], next);
},
function (_postData, next) {
postData = _postData;
tids = postData.map(function (post) {
return post.tid;
}).filter(function (tid, index, array) {
return tid && array.indexOf(tid) === index;
});
8 years ago
topics.getTopicsFields(tids, ['cid'], next);
},
function (topicData, next) {
var map = {};
8 years ago
topicData.forEach(function (topic, index) {
if (topic) {
map[tids[index]] = topic.cid;
}
});
8 years ago
var cids = postData.map(function (post) {
return map[post.tid];
});
8 years ago
next(null, cids);
},
8 years ago
], callback);
};
9 years ago
Posts.filterPidsByCid = function (pids, cid, callback) {
9 years ago
if (!cid) {
return callback(null, pids);
}
8 years ago
if (!Array.isArray(cid) || cid.length === 1) {
// Single cid
db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids, function (err, isMembers) {
8 years ago
if (err) {
return callback(err);
}
pids = pids.filter(function (pid, index) {
8 years ago
return pid && isMembers[index];
});
callback(null, pids);
9 years ago
});
8 years ago
} else {
// Multiple cids
8 years ago
async.map(cid, function (cid, next) {
8 years ago
Posts.filterPidsByCid(pids, cid, next);
8 years ago
}, function (err, pidsArr) {
8 years ago
if (err) {
return callback(err);
}
callback(null, _.union.apply(_, pidsArr));
});
}
9 years ago
};
};