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
|
|
|
|
2016-10-13 11:43:39 +02: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);
|
2019-11-08 18:38:58 -05:00
|
|
|
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)));
|
2021-02-04 00:52:25 -07:00
|
|
|
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
|
|
|
};
|