Files
NodeBB/src/posts/category.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-11-09 01:12:24 -05:00
'use strict';
2016-09-16 18:39:45 +03:00
2019-07-17 00:17:21 -04:00
const _ = require('lodash');
const db = require('../database');
const topics = require('../topics');
2024-01-16 11:20:54 -05:00
const activitypub = require('../activitypub');
2014-11-09 01:12:24 -05:00
module.exports = function (Posts) {
2019-07-17 00:17:21 -04:00
Posts.getCidByPid = async function (pid) {
const tid = await Posts.getPostField(pid, 'tid');
2024-01-16 11:20:54 -05:00
if (!tid && activitypub.helpers.isUri(pid)) {
return -1; // fediverse pseudo-category
}
2019-07-17 00:17:21 -04:00
return await topics.getTopicField(tid, 'cid');
2014-11-09 01:12:24 -05:00
};
2019-07-17 00:17:21 -04:00
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);
2019-07-17 00:17:21 -04:00
return cids;
2014-11-09 01:12:24 -05:00
};
2016-09-16 18:39:45 +03:00
2019-07-17 00:17:21 -04:00
Posts.filterPidsByCid = async function (pids, cid) {
2016-09-16 18:39:45 +03:00
if (!cid) {
2019-07-17 00:17:21 -04:00
return pids;
2016-09-16 18:39:45 +03:00
}
2016-10-13 13:12:38 -04:00
if (!Array.isArray(cid) || cid.length === 1) {
2019-07-17 00:17:21 -04:00
return await filterPidsBySingleCid(pids, cid);
2017-05-11 17:16:22 -04:00
}
2019-07-17 00:17:21 -04:00
const pidsArr = await Promise.all(cid.map(c => Posts.filterPidsByCid(pids, c)));
return _.union(...pidsArr);
2017-05-11 17:16:22 -04:00
};
2019-07-17 00:17:21 -04:00
async function filterPidsBySingleCid(pids, cid) {
2021-02-03 23:59:08 -07:00
const isMembers = await db.isSortedSetMembers(`cid:${parseInt(cid, 10)}:pids`, pids);
2019-07-17 00:17:21 -04:00
return pids.filter((pid, index) => pid && isMembers[index]);
2017-05-11 17:16:22 -04:00
}
2017-02-18 02:30:48 -07:00
};