mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
posts.js refactor pt1
This commit is contained in:
140
src/posts/summary.js
Normal file
140
src/posts/summary.js
Normal file
@@ -0,0 +1,140 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
validator = require('validator'),
|
||||
S = require('string'),
|
||||
|
||||
db = require('../database'),
|
||||
user = require('../user'),
|
||||
plugins = require('../plugins'),
|
||||
categories = require('../categories'),
|
||||
postTools = require('../postTools'),
|
||||
utils = require('../../public/src/utils');
|
||||
|
||||
|
||||
module.exports = function(Posts) {
|
||||
|
||||
Posts.getPostSummaryByPids = function(pids, uid, options, callback) {
|
||||
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
|
||||
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
|
||||
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
|
||||
|
||||
if (!Array.isArray(pids) || !pids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
var keys = pids.map(function(pid) {
|
||||
return 'post:' + pid;
|
||||
});
|
||||
|
||||
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields);
|
||||
|
||||
db.getObjectsFields(keys, fields, function(err, posts) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
posts = posts.filter(function(p) {
|
||||
return !!p && parseInt(p.deleted, 10) !== 1;
|
||||
});
|
||||
|
||||
var uids = [], tids = [];
|
||||
for(var i=0; i<posts.length; ++i) {
|
||||
if (uids.indexOf(posts[i].uid) === -1) {
|
||||
uids.push(posts[i].uid);
|
||||
}
|
||||
if (tids.indexOf('topic:' + posts[i].tid) === -1) {
|
||||
tids.push('topic:' + posts[i].tid);
|
||||
}
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
users: function(next) {
|
||||
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
|
||||
},
|
||||
topicsAndCategories: function(next) {
|
||||
db.getObjectsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
var cids = topics.map(function(topic) {
|
||||
if (topic) {
|
||||
topic.title = validator.escape(topic.title);
|
||||
}
|
||||
return topic && topic.cid;
|
||||
}).filter(function(value, index, array) {
|
||||
return value && array.indexOf(value) === index;
|
||||
});
|
||||
|
||||
categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) {
|
||||
next(err, {topics: topics, categories: categories});
|
||||
});
|
||||
});
|
||||
},
|
||||
indices: function(next) {
|
||||
Posts.getPostIndices(posts, uid, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
function toObject(key, data) {
|
||||
var obj = {};
|
||||
for(var i=0; i<data.length; ++i) {
|
||||
obj[data[i][key]] = data[i];
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
function stripTags(content) {
|
||||
if (options.stripTags && content) {
|
||||
var s = S(content);
|
||||
return s.stripTags.apply(s, utils.stripTags).s;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
results.users = toObject('uid', results.users);
|
||||
results.topics = toObject('tid', results.topicsAndCategories.topics);
|
||||
results.categories = toObject('cid', results.topicsAndCategories.categories);
|
||||
|
||||
for (var i=0; i<posts.length; ++i) {
|
||||
posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1;
|
||||
}
|
||||
|
||||
posts = posts.filter(function(post) {
|
||||
return results.topics[post.tid] && parseInt(results.topics[post.tid].deleted, 10) !== 1;
|
||||
});
|
||||
|
||||
async.map(posts, function(post, next) {
|
||||
post.user = results.users[post.uid];
|
||||
post.topic = results.topics[post.tid];
|
||||
post.category = results.categories[post.topic.cid];
|
||||
post.relativeTime = utils.toISOString(post.timestamp);
|
||||
|
||||
if (!post.content || !options.parse) {
|
||||
post.content = stripTags(post.content);
|
||||
return next(null, post);
|
||||
}
|
||||
|
||||
postTools.parsePost(post, uid, function(err, post) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
post.content = stripTags(post.content);
|
||||
|
||||
next(null, post);
|
||||
});
|
||||
}, function(err, posts) {
|
||||
plugins.fireHook('filter:post.getPostSummaryByPids', {posts: posts, uid: uid}, function(err, postData) {
|
||||
callback(err, postData.posts);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user