Files
NodeBB/src/posts/summary.js

151 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-11-09 01:12:24 -05:00
'use strict';
2016-04-29 20:35:49 +03:00
var async = require('async');
var validator = require('validator');
2017-09-12 11:41:52 -04:00
var _ = require('lodash');
2014-11-09 01:12:24 -05:00
var topics = require('../topics');
2016-04-29 20:35:49 +03:00
var user = require('../user');
var plugins = require('../plugins');
var categories = require('../categories');
var utils = require('../utils');
2014-11-09 01:12:24 -05:00
module.exports = function (Posts) {
Posts.getPostSummaryByPids = function (pids, uid, options, callback) {
2014-11-18 22:19:17 -05:00
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
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 : [];
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes'].concat(options.extraFields);
var posts;
async.waterfall([
function (next) {
Posts.getPostsFields(pids, fields, next);
},
function (_posts, next) {
posts = _posts.filter(Boolean);
var uids = [];
var topicKeys = [];
posts.forEach(function (post, i) {
if (uids.indexOf(posts[i].uid) === -1) {
uids.push(posts[i].uid);
}
if (topicKeys.indexOf(posts[i].tid) === -1) {
topicKeys.push(posts[i].tid);
}
});
async.parallel({
users: function (next) {
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
topicsAndCategories: function (next) {
getTopicAndCategories(topicKeys, next);
2017-02-17 19:31:21 -07:00
},
}, next);
},
function (results, next) {
2014-11-09 01:12:24 -05:00
results.users = toObject('uid', results.users);
results.topics = toObject('tid', results.topicsAndCategories.topics);
results.categories = toObject('cid', results.topicsAndCategories.categories);
posts.forEach(function (post) {
2015-07-24 10:22:21 -04:00
// If the post author isn't represented in the retrieved users' data, then it means they were deleted, assume guest.
if (!results.users.hasOwnProperty(post.uid)) {
post.uid = 0;
}
2014-11-09 01:12:24 -05:00
post.user = results.users[post.uid];
post.topic = results.topics[post.tid];
2016-12-22 12:19:07 +03:00
post.category = post.topic && results.categories[post.topic.cid];
post.isMainPost = post.topic && parseInt(post.pid, 10) === parseInt(post.topic.mainPid, 10);
2016-10-13 13:20:45 +03:00
post.deleted = parseInt(post.deleted, 10) === 1;
post.upvotes = parseInt(post.upvotes, 10) || 0;
post.downvotes = parseInt(post.downvotes, 10) || 0;
post.votes = post.upvotes - post.downvotes;
2016-03-03 20:13:30 +02:00
post.timestampISO = utils.toISOString(post.timestamp);
});
2014-11-09 01:12:24 -05:00
posts = posts.filter(function (post) {
return results.topics[post.tid];
});
2014-11-09 01:12:24 -05:00
parsePosts(posts, options, next);
},
function (posts, next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.getPostSummaryByPids', { posts: posts, uid: uid }, next);
},
function (data, next) {
next(null, data.posts);
2017-02-17 19:31:21 -07:00
},
], callback);
};
2016-08-16 19:46:59 +02:00
function parsePosts(posts, options, callback) {
async.map(posts, function (post, next) {
2017-05-25 16:40:03 -04:00
async.waterfall([
function (next) {
if (!post.content || !options.parse) {
post.content = post.content ? validator.escape(String(post.content)) : post.content;
return next(null, post);
}
Posts.parsePost(post, next);
},
function (post, next) {
if (options.stripTags) {
post.content = stripTags(post.content);
}
next(null, post);
},
], next);
}, callback);
}
2014-11-11 18:55:45 -05:00
function getTopicAndCategories(tids, callback) {
2017-05-25 16:40:03 -04:00
var topicsData;
async.waterfall([
function (next) {
topics.getTopicsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted', 'postcount', 'mainPid'], next);
},
function (_topicsData, next) {
topicsData = _topicsData;
var cids = topicsData.map(function (topic) {
if (topic) {
topic.title = String(topic.title);
topic.deleted = parseInt(topic.deleted, 10) === 1;
}
2017-09-12 11:41:52 -04:00
return topic && parseInt(topic.cid, 10);
2017-05-25 16:40:03 -04:00
});
2017-09-12 11:51:38 -04:00
cids = _.uniq(cids);
2017-09-12 11:41:52 -04:00
2017-05-25 16:40:03 -04:00
categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], next);
},
function (categoriesData, next) {
next(null, { topics: topicsData, categories: categoriesData });
},
], callback);
2014-11-11 18:55:45 -05:00
}
function toObject(key, data) {
var obj = {};
for (var 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
};