From c3a903142a48d4f486147e6a0d939b2922e83d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 20 Oct 2018 16:31:16 -0400 Subject: [PATCH] closes #6850 --- src/database.js | 6 +++ src/posts/data.js | 108 ++++++++++++++++----------------------------- src/topics/data.js | 8 +--- 3 files changed, 44 insertions(+), 78 deletions(-) diff --git a/src/database.js b/src/database.js index c62255306b..0924fffbaf 100644 --- a/src/database.js +++ b/src/database.js @@ -11,4 +11,10 @@ if (!databaseName) { var primaryDB = require('./database/' + databaseName); +primaryDB.parseIntField = function (data, field) { + if (data.hasOwnProperty(field)) { + data[field] = parseInt(data[field], 10); + } +}; + module.exports = primaryDB; diff --git a/src/posts/data.js b/src/posts/data.js index bc7df89f57..a05de93305 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -5,98 +5,58 @@ var async = require('async'); var db = require('../database'); var plugins = require('../plugins'); +const intFields = ['uid', 'pid', 'tid', 'deleted']; + module.exports = function (Posts) { - Posts.getPostData = function (pid, callback) { + Posts.getPostsFields = function (pids, fields, callback) { + if (!Array.isArray(pids) || !pids.length) { + return callback(null, []); + } + + var keys = pids.map(pid => 'post:' + pid); + async.waterfall([ function (next) { - db.getObject('post:' + pid, next); + if (fields.length) { + db.getObjectsFields(keys, fields, next); + } else { + db.getObjects(keys, next); + } }, - function (data, next) { - plugins.fireHook('filter:post.getPostData', { post: data }, next); + function (posts, next) { + plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next); }, function (data, next) { - next(null, data.post); + data.posts.forEach(modifyPost); + next(null, Array.isArray(data.posts) ? data.posts : null); }, ], callback); }; + Posts.getPostData = function (pid, callback) { + Posts.getPostsFields([pid], [], function (err, posts) { + callback(err, posts && posts.length ? posts[0] : null); + }); + }; + Posts.getPostsData = function (pids, callback) { - async.waterfall([ - function (next) { - db.getObjects(pids.map(pid => 'post:' + pid), next); - }, - function (data, next) { - plugins.fireHook('filter:post.getPostsData', { posts: data }, next); - }, - function (data, next) { - next(null, data.posts); - }, - ], callback); + Posts.getPostsFields(pids, [], callback); }; Posts.getPostField = function (pid, field, callback) { - async.waterfall([ - function (next) { - Posts.getPostFields(pid, [field], next); - }, - function (data, next) { - next(null, data[field]); - }, - ], callback); + Posts.getPostFields(pid, [field], function (err, post) { + callback(err, post ? post[field] : null); + }); }; Posts.getPostFields = function (pid, fields, callback) { - async.waterfall([ - function (next) { - db.getObjectFields('post:' + pid, fields, next); - }, - function (data, next) { - data.pid = pid; - - plugins.fireHook('filter:post.getFields', { posts: [data], fields: fields }, next); - }, - function (data, next) { - next(null, (data && Array.isArray(data.posts) && data.posts.length) ? data.posts[0] : null); - }, - ], callback); - }; - - Posts.getPostsFields = function (pids, fields, callback) { - if (!Array.isArray(pids) || !pids.length) { - return callback(null, []); - } - - var keys = pids.map(function (pid) { - return 'post:' + pid; + Posts.getPostsFields([pid], fields, function (err, posts) { + callback(err, posts ? posts[0] : null); }); - - async.waterfall([ - function (next) { - db.getObjectsFields(keys, fields, next); - }, - function (posts, next) { - plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next); - }, - function (data, next) { - next(null, (data && Array.isArray(data.posts)) ? data.posts : null); - }, - ], callback); }; Posts.setPostField = function (pid, field, value, callback) { - async.waterfall([ - function (next) { - db.setObjectField('post:' + pid, field, value, next); - }, - function (next) { - var data = { - pid: pid, - }; - data[field] = value; - plugins.fireHook('action:post.setFields', { data: data }); - next(); - }, - ], callback); + Posts.setPostFields(pid, { [field]: value }, callback); }; Posts.setPostFields = function (pid, data, callback) { @@ -112,3 +72,9 @@ module.exports = function (Posts) { ], callback); }; }; + +function modifyPost(post) { + if (post) { + intFields.forEach(field => db.parseIntField(post, field)); + } +} diff --git a/src/topics/data.js b/src/topics/data.js index f66f2725ab..29e6eafcf0 100644 --- a/src/topics/data.js +++ b/src/topics/data.js @@ -98,7 +98,7 @@ function modifyTopic(topic) { return; } - intFields.forEach(field => parseIntField(topic, field)); + intFields.forEach(field => db.parseIntField(topic, field)); if (topic.hasOwnProperty('title')) { topic.titleRaw = topic.title; @@ -124,9 +124,3 @@ function modifyTopic(topic) { topic.votes = topic.upvotes - topic.downvotes; } } - -function parseIntField(topic, field) { - if (topic.hasOwnProperty(field)) { - topic[field] = parseInt(topic[field], 10); - } -}