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

42 lines
1.2 KiB
JavaScript

'use strict';
9 years ago
const _ = require('lodash');
const db = require('../database');
const topics = require('../topics');
module.exports = function (Posts) {
Posts.getCidByPid = async function (pid) {
const tid = await Posts.getPostField(pid, 'tid');
return await topics.getTopicField(tid, 'cid');
};
Posts.getCidsByPids = async function (pids) {
const postData = await Posts.getPostsFields(pids, ['tid']);
const tids = _.uniq(postData.map(post => post && post.tid).filter(Boolean));
const topicData = await topics.getTopicsFields(tids, ['cid']);
const tidToTopic = _.zipObject(tids, topicData);
const cids = postData.map(post => tidToTopic[post.tid] && tidToTopic[post.tid].cid);
return cids;
};
9 years ago
Posts.filterPidsByCid = async function (pids, cid) {
9 years ago
if (!cid) {
return pids;
9 years ago
}
8 years ago
if (!Array.isArray(cid) || cid.length === 1) {
return await filterPidsBySingleCid(pids, cid);
}
const pidsArr = await Promise.all(cid.map(c => Posts.filterPidsByCid(pids, c)));
return _.union.apply(_, pidsArr);
};
async function filterPidsBySingleCid(pids, cid) {
const isMembers = await db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids);
return pids.filter((pid, index) => pid && isMembers[index]);
}
8 years ago
};