Node.js based forum software built for the modern web
refactor user/category data
Barış Soner Uşaklıf53984aeae9d
diff --git a/src/categories/data.js b/src/categories/data.jsindex 8eb24b2179..3b64d9fe7f 100644--- a/src/categories/data.js+++ b/src/categories/data.js@@ -6,74 +6,16 @@ var winston = require('winston');var db = require('../database');-module.exports = function (Categories) {- Categories.getCategoryData = function (cid, callback) {- async.waterfall([- function (next) {- db.getObject('category:' + cid, next);- },- function (category, next) {- modifyCategory(category);- next(null, category);- },- ], callback);- };-- Categories.getCategoriesData = function (cids, callback) {- Categories.getCategoriesFields(cids, [], callback);- };-- function modifyCategory(category) {- if (!category) {- return;- }-- if (category.hasOwnProperty('name')) {- category.name = validator.escape(String(category.name || ''));- }- if (category.hasOwnProperty('disabled')) {- category.disabled = parseInt(category.disabled, 10) === 1;- }- if (category.hasOwnProperty('isSection')) {- category.isSection = parseInt(category.isSection, 10) === 1;- }-- if (category.hasOwnProperty('icon')) {- category.icon = category.icon || 'hidden';- }-- if (category.hasOwnProperty('post_count')) {- category.post_count = category.post_count || 0;- category.totalPostCount = category.post_count;- }-- if (category.hasOwnProperty('topic_count')) {- category.topic_count = category.topic_count || 0;- category.totalTopicCount = category.topic_count;- }-- if (category.image) {- category.backgroundImage = category.image;- }-- if (category.description) {- category.description = validator.escape(String(category.description));- category.descriptionParsed = category.descriptionParsed || category.description;- }- }-- Categories.getCategoryField = function (cid, field, callback) {- db.getObjectField('category:' + cid, field, callback);- };+const intFields = ['cid', 'parentCid', 'disabled', 'isSection', 'order', 'topic_count', 'post_count'];+module.exports = function (Categories) {Categories.getCategoriesFields = function (cids, fields, callback) {if (!Array.isArray(cids) || !cids.length) {return callback(null, []);}- var keys = cids.map(function (cid) {- return 'category:' + cid;- });+ var keys = cids.map(cid => 'category:' + cid);+async.waterfall([function (next) {if (fields.length) {@@ -89,6 +31,28 @@ module.exports = function (Categories) {], callback);};+ Categories.getCategoryData = function (cid, callback) {+ Categories.getCategoriesFields([cid], [], function (err, categories) {+ callback(err, categories && categories.length ? categories[0] : null);+ });+ };++ Categories.getCategoriesData = function (cids, callback) {+ Categories.getCategoriesFields(cids, [], callback);+ };++ Categories.getCategoryField = function (cid, field, callback) {+ Categories.getCategoryFields(cid, [field], function (err, category) {+ callback(err, category ? category[field] : null);+ });+ };++ Categories.getCategoryFields = function (cid, fields, callback) {+ Categories.getCategoriesFields([cid], fields, function (err, categories) {+ callback(err, categories ? categories[0] : null);+ });+ };+Categories.getMultipleCategoryFields = function (cids, fields, callback) {winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');Categories.getCategoriesFields(cids, fields, callback);@@ -103,10 +67,6 @@ module.exports = function (Categories) {], callback);};- Categories.getCategoryFields = function (cid, fields, callback) {- db.getObjectFields('category:' + cid, fields, callback);- };-Categories.setCategoryField = function (cid, field, value, callback) {db.setObjectField('category:' + cid, field, value, callback);};@@ -115,3 +75,38 @@ module.exports = function (Categories) {db.incrObjectFieldBy('category:' + cid, field, value, callback);};};++function modifyCategory(category) {+ if (!category) {+ return;+ }++ intFields.forEach(field => db.parseIntField(category, field));++ if (category.hasOwnProperty('name')) {+ category.name = validator.escape(String(category.name || ''));+ }++ if (category.hasOwnProperty('icon')) {+ category.icon = category.icon || 'hidden';+ }++ if (category.hasOwnProperty('post_count')) {+ category.post_count = category.post_count || 0;+ category.totalPostCount = category.post_count;+ }++ if (category.hasOwnProperty('topic_count')) {+ category.topic_count = category.topic_count || 0;+ category.totalTopicCount = category.topic_count;+ }++ if (category.image) {+ category.backgroundImage = category.image;+ }++ if (category.description) {+ category.description = validator.escape(String(category.description));+ category.descriptionParsed = category.descriptionParsed || category.description;+ }+}diff --git a/src/user/data.js b/src/user/data.jsindex f157b06ad4..d80a115ef3 100644--- a/src/user/data.js+++ b/src/user/data.js@@ -11,6 +11,8 @@ var meta = require('../meta');var plugins = require('../plugins');var utils = require('../utils');+const intFields = ['uid', 'postcount', 'topiccount', 'banned'];+module.exports = function (User) {var iconBackgrounds = ['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',@@ -27,26 +29,12 @@ module.exports = function (User) {'cover:position', 'groupTitle',];- User.getUserField = function (uid, field, callback) {- User.getUserFields(uid, [field], function (err, user) {- callback(err, user ? user[field] : null);- });- };-- User.getUserFields = function (uid, fields, callback) {- User.getUsersFields([uid], fields, function (err, users) {- callback(err, users ? users[0] : null);- });- };-User.getUsersFields = function (uids, fields, callback) {if (!Array.isArray(uids) || !uids.length) {return callback(null, []);}- uids = uids.map(function (uid) {- return isNaN(uid) ? 0 : uid;- });+ uids = uids.map(uid => (isNaN(uid) ? 0 : uid));var fieldsToRemove = [];function addField(field) {@@ -76,8 +64,9 @@ module.exports = function (User) {},function (results, next) {if (fields.length) {+ const whitelistSet = new Set(results.whitelist);fields = fields.filter(function (field) {- var isFieldWhitelisted = field && results.whitelist.includes(field);+ var isFieldWhitelisted = field && whitelistSet.has(field);if (!isFieldWhitelisted) {winston.verbose('[user/getUsersFields] ' + field + ' removed because it is not whitelisted, see `filter:user.whitelistFields`');}@@ -97,6 +86,19 @@ module.exports = function (User) {], callback);};++ User.getUserField = function (uid, field, callback) {+ User.getUserFields(uid, [field], function (err, user) {+ callback(err, user ? user[field] : null);+ });+ };++ User.getUserFields = function (uid, fields, callback) {+ User.getUsersFields([uid], fields, function (err, users) {+ callback(err, users ? users[0] : null);+ });+ };+User.getMultipleUserFields = function (uids, fields, callback) {winston.warn('[deprecated] User.getMultipleUserFields is deprecated please use User.getUsersFields');User.getUsersFields(uids, fields, callback);@@ -137,6 +139,9 @@ module.exports = function (User) {if (!user) {return;}++ intFields.forEach(field => db.parseIntField(user, field));+if (user.hasOwnProperty('groupTitle')) {parseGroupTitle(user);}@@ -218,16 +223,7 @@ module.exports = function (User) {};User.setUserField = function (uid, field, value, callback) {- callback = callback || function () {};- async.waterfall([- function (next) {- db.setObjectField('user:' + uid, field, value, next);- },- function (next) {- plugins.fireHook('action:user.set', { uid: uid, field: field, value: value, type: 'set' });- next();- },- ], callback);+ User.setUserFields(uid, { [field]: value }, callback);};User.setUserFields = function (uid, data, callback) {