From fb6067a7cadc8ca222ddaeba8211521ebede3f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 25 Oct 2018 19:58:01 -0400 Subject: [PATCH] only parse if field is requested --- src/categories/data.js | 6 +++--- src/database/index.js | 10 ++++++---- src/groups/data.js | 7 ++++--- src/messaging/data.js | 6 +++--- src/posts/data.js | 7 +++---- src/topics/data.js | 6 +++--- src/user/data.js | 6 +++--- test/topics.js | 19 ++++++++++++++++--- 8 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/categories/data.js b/src/categories/data.js index 349ef4c788..ef5ced757b 100644 --- a/src/categories/data.js +++ b/src/categories/data.js @@ -27,7 +27,7 @@ module.exports = function (Categories) { } }, function (categories, next) { - categories.forEach(modifyCategory); + categories.forEach(category => modifyCategory(category, fields)); next(null, categories); }, ], callback); @@ -73,12 +73,12 @@ module.exports = function (Categories) { }; }; -function modifyCategory(category) { +function modifyCategory(category, fields) { if (!category) { return; } - intFields.forEach(field => db.parseIntField(category, field)); + db.parseIntFields(category, intFields, fields); if (category.hasOwnProperty('name')) { category.name = validator.escape(String(category.name || '')); diff --git a/src/database/index.js b/src/database/index.js index f2d1c031e2..21484c5492 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -11,10 +11,12 @@ if (!databaseName) { var primaryDB = require('./' + databaseName); -primaryDB.parseIntField = function (data, field) { - if (data.hasOwnProperty(field)) { - data[field] = parseInt(data[field], 10) || 0; - } +primaryDB.parseIntFields = function (data, intFields, requestedFields) { + intFields.forEach((field) => { + if (!requestedFields.length || requestedFields.includes(field)) { + data[field] = parseInt(data[field], 10) || 0; + } + }); }; module.exports = primaryDB; diff --git a/src/groups/data.js b/src/groups/data.js index 2bf0418891..2115c35dc9 100644 --- a/src/groups/data.js +++ b/src/groups/data.js @@ -41,7 +41,7 @@ module.exports = function (Groups) { }); } - groupData.forEach(modifyGroup); + groupData.forEach(group => modifyGroup(group, fields)); plugins.fireHook('filter:groups.get', { groups: groupData }, next); }, @@ -80,9 +80,10 @@ module.exports = function (Groups) { }; }; -function modifyGroup(group) { +function modifyGroup(group, fields) { if (group) { - intFields.forEach(field => db.parseIntField(group, field)); + db.parseIntFields(group, intFields, fields); + escapeGroupData(group); group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled; group.labelColor = validator.escape(String(group.labelColor || '#000000')); diff --git a/src/messaging/data.js b/src/messaging/data.js index df90620139..84ac7e8c19 100644 --- a/src/messaging/data.js +++ b/src/messaging/data.js @@ -27,7 +27,7 @@ module.exports = function (Messaging) { } }, function (messages, next) { - messages.forEach(modifyMessage); + messages.forEach(message => modifyMessage(message, fields)); next(null, messages); }, ], callback); @@ -161,9 +161,9 @@ module.exports = function (Messaging) { }; }; -function modifyMessage(message) { +function modifyMessage(message, fields) { if (message) { - intFields.forEach(field => db.parseIntField(message, field)); + db.parseIntFields(message, intFields, fields); if (message.hasOwnProperty('timestamp')) { message.timestampISO = utils.toISOString(message.timestamp); } diff --git a/src/posts/data.js b/src/posts/data.js index 45eaf8d845..aa53fca6c1 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -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; } diff --git a/src/topics/data.js b/src/topics/data.js index 02d8fac323..9b4edbf944 100644 --- a/src/topics/data.js +++ b/src/topics/data.js @@ -29,7 +29,7 @@ module.exports = function (Topics) { } }, function (topics, next) { - topics.forEach(modifyTopic); + topics.forEach(topic => modifyTopic(topic, fields)); next(null, topics); }, ], callback); @@ -97,12 +97,12 @@ function escapeTitle(topicData) { } } -function modifyTopic(topic) { +function modifyTopic(topic, fields) { if (!topic) { return; } - intFields.forEach(field => db.parseIntField(topic, field)); + db.parseIntFields(topic, intFields, fields); if (topic.hasOwnProperty('title')) { topic.titleRaw = topic.title; diff --git a/src/user/data.js b/src/user/data.js index 190c1bc511..907d622c42 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -85,7 +85,7 @@ module.exports = function (User) { function (users, next) { users = uidsToUsers(uids, uniqueUids, users); - modifyUserData(users, fieldsToRemove, next); + modifyUserData(users, fields, fieldsToRemove, next); }, ], callback); }; @@ -133,13 +133,13 @@ module.exports = function (User) { return uids.map(uid => 'user:' + uid); } - function modifyUserData(users, fieldsToRemove, callback) { + function modifyUserData(users, requestedFields, fieldsToRemove, callback) { users.forEach(function (user) { if (!user) { return; } - intFields.forEach(field => db.parseIntField(user, field)); + db.parseIntFields(user, intFields, requestedFields); if (user.hasOwnProperty('groupTitle')) { parseGroupTitle(user); diff --git a/test/topics.js b/test/topics.js index 213f906a3e..53938bfd16 100644 --- a/test/topics.js +++ b/test/topics.js @@ -224,10 +224,23 @@ describe('Topic\'s', function () { assert(typeof topicData.uid === 'number'); assert(typeof topicData.cid === 'number'); assert(typeof topicData.mainPid === 'number'); - assert(typeof topicData.deleted === 'number'); - assert(typeof topicData.locked === 'number'); - assert(typeof topicData.pinned === 'number'); + assert(typeof topicData.timestamp === 'number'); + assert.strictEqual(topicData.upvotes, 0); + assert.strictEqual(topicData.downvotes, 0); + assert.strictEqual(topicData.votes, 0); + assert.strictEqual(topicData.deleted, 0); + assert.strictEqual(topicData.locked, 0); + assert.strictEqual(topicData.pinned, 0); + done(); + }); + }); + + it('should get a single field', function (done) { + topics.getTopicFields(newTopic.tid, ['slug'], function (err, data) { + assert.ifError(err); + assert(Object.keys(data).length === 1); + assert(data.hasOwnProperty('slug')); done(); }); });