mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
only parse if field is requested
This commit is contained in:
@@ -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 || ''));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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'));
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user