Files
NodeBB/src/posts/summary.js

106 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-11-09 01:12:24 -05:00
'use strict';
2019-07-17 19:05:55 -04:00
const validator = require('validator');
const _ = require('lodash');
2014-11-09 01:12:24 -05:00
2019-07-17 19:05:55 -04:00
const topics = require('../topics');
const user = require('../user');
const plugins = require('../plugins');
const categories = require('../categories');
const utils = require('../utils');
2014-11-09 01:12:24 -05:00
module.exports = function (Posts) {
2019-07-17 19:05:55 -04:00
Posts.getPostSummaryByPids = async function (pids, uid, options) {
2014-11-18 22:19:17 -05:00
if (!Array.isArray(pids) || !pids.length) {
2019-07-17 19:05:55 -04:00
return [];
}
2014-11-09 01:12:24 -05:00
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
2021-03-15 14:03:35 -04:00
const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);
2019-07-17 19:05:55 -04:00
let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
posts = await user.blocks.filter(uid, posts);
const uids = _.uniq(posts.map(p => p && p.uid));
const tids = _.uniq(posts.map(p => p && p.tid));
const [users, topicsAndCategories] = await Promise.all([
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status']),
2019-07-17 19:05:55 -04:00
getTopicAndCategories(tids),
]);
const uidToUser = toObject('uid', users);
const tidToTopic = toObject('tid', topicsAndCategories.topics);
const cidToCategory = toObject('cid', topicsAndCategories.categories);
2021-02-04 00:01:39 -07:00
posts.forEach((post) => {
2021-02-04 02:07:29 -07:00
// If the post author isn't represented in the retrieved users' data,
// then it means they were deleted, assume guest.
2019-07-17 19:05:55 -04:00
if (!uidToUser.hasOwnProperty(post.uid)) {
post.uid = 0;
}
post.user = uidToUser[post.uid];
2021-03-15 14:03:35 -04:00
Posts.overrideGuestHandle(post, post.handle);
post.handle = undefined;
2019-07-17 19:05:55 -04:00
post.topic = tidToTopic[post.tid];
post.category = post.topic && cidToCategory[post.topic.cid];
post.isMainPost = post.topic && post.pid === post.topic.mainPid;
post.deleted = post.deleted === 1;
post.timestampISO = utils.toISOString(post.timestamp);
});
2019-07-17 19:05:55 -04:00
posts = posts.filter(post => tidToTopic[post.tid]);
posts = await parsePosts(posts, options);
const result = await plugins.hooks.fire('filter:post.getPostSummaryByPids', { posts: posts, uid: uid });
2019-07-17 19:05:55 -04:00
return result.posts;
};
2016-08-16 19:46:59 +02:00
2019-07-17 19:05:55 -04:00
async function parsePosts(posts, options) {
2021-03-15 14:03:35 -04:00
return await Promise.all(posts.map(async (post) => {
2019-07-17 19:05:55 -04:00
if (!post.content || !options.parse) {
post.content = post.content ? validator.escape(String(post.content)) : post.content;
return post;
}
post = await Posts.parsePost(post);
if (options.stripTags) {
post.content = stripTags(post.content);
}
return post;
2021-03-15 14:03:35 -04:00
}));
}
2014-11-11 18:55:45 -05:00
2019-07-17 19:05:55 -04:00
async function getTopicAndCategories(tids) {
const topicsData = await topics.getTopicsFields(tids, [
'uid', 'tid', 'title', 'cid', 'tags', 'slug',
'deleted', 'scheduled', 'postcount', 'mainPid', 'teaserPid',
]);
2019-07-17 19:05:55 -04:00
const cids = _.uniq(topicsData.map(topic => topic && topic.cid));
const categoriesData = await categories.getCategoriesFields(cids, [
'cid', 'name', 'icon', 'slug', 'parentCid',
'bgColor', 'color', 'backgroundImage', 'imageClass',
]);
2019-07-17 19:05:55 -04:00
return { topics: topicsData, categories: categoriesData };
2014-11-11 18:55:45 -05:00
}
function toObject(key, data) {
2021-02-04 00:06:15 -07:00
const obj = {};
for (let i = 0; i < data.length; i += 1) {
2014-11-11 18:55:45 -05:00
obj[data[i][key]] = data[i];
}
return obj;
}
function stripTags(content) {
if (content) {
2017-10-13 21:02:41 -06:00
return utils.stripHTMLTags(content, utils.stripTags);
2014-11-11 18:55:45 -05:00
}
return content;
}
2016-07-05 03:38:15 +05:00
};