only parse if field is requested

This commit is contained in:
Barış Soner Uşaklı
2018-10-25 19:58:01 -04:00
parent 5527b96a7f
commit fb6067a7ca
8 changed files with 41 additions and 26 deletions

View File

@@ -30,7 +30,7 @@ module.exports = function (Posts) {
plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next);
},
function (data, next) {
data.posts.forEach(modifyPost);
data.posts.forEach(post => modifyPost(post, fields));
next(null, Array.isArray(data.posts) ? data.posts : null);
},
], callback);
@@ -76,10 +76,9 @@ module.exports = function (Posts) {
};
};
function modifyPost(post) {
function modifyPost(post, fields) {
if (post) {
intFields.forEach(field => db.parseIntField(post, field));
db.parseIntFields(post, intFields, fields);
if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {
post.votes = post.upvotes - post.downvotes;
}