mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
only parse if field is requested
This commit is contained in:
@@ -27,7 +27,7 @@ module.exports = function (Categories) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
function (categories, next) {
|
function (categories, next) {
|
||||||
categories.forEach(modifyCategory);
|
categories.forEach(category => modifyCategory(category, fields));
|
||||||
next(null, categories);
|
next(null, categories);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
@@ -73,12 +73,12 @@ module.exports = function (Categories) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function modifyCategory(category) {
|
function modifyCategory(category, fields) {
|
||||||
if (!category) {
|
if (!category) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
intFields.forEach(field => db.parseIntField(category, field));
|
db.parseIntFields(category, intFields, fields);
|
||||||
|
|
||||||
if (category.hasOwnProperty('name')) {
|
if (category.hasOwnProperty('name')) {
|
||||||
category.name = validator.escape(String(category.name || ''));
|
category.name = validator.escape(String(category.name || ''));
|
||||||
|
|||||||
@@ -11,10 +11,12 @@ if (!databaseName) {
|
|||||||
|
|
||||||
var primaryDB = require('./' + databaseName);
|
var primaryDB = require('./' + databaseName);
|
||||||
|
|
||||||
primaryDB.parseIntField = function (data, field) {
|
primaryDB.parseIntFields = function (data, intFields, requestedFields) {
|
||||||
if (data.hasOwnProperty(field)) {
|
intFields.forEach((field) => {
|
||||||
data[field] = parseInt(data[field], 10) || 0;
|
if (!requestedFields.length || requestedFields.includes(field)) {
|
||||||
}
|
data[field] = parseInt(data[field], 10) || 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = primaryDB;
|
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);
|
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) {
|
if (group) {
|
||||||
intFields.forEach(field => db.parseIntField(group, field));
|
db.parseIntFields(group, intFields, fields);
|
||||||
|
|
||||||
escapeGroupData(group);
|
escapeGroupData(group);
|
||||||
group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled;
|
group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled;
|
||||||
group.labelColor = validator.escape(String(group.labelColor || '#000000'));
|
group.labelColor = validator.escape(String(group.labelColor || '#000000'));
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ module.exports = function (Messaging) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
function (messages, next) {
|
function (messages, next) {
|
||||||
messages.forEach(modifyMessage);
|
messages.forEach(message => modifyMessage(message, fields));
|
||||||
next(null, messages);
|
next(null, messages);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
@@ -161,9 +161,9 @@ module.exports = function (Messaging) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function modifyMessage(message) {
|
function modifyMessage(message, fields) {
|
||||||
if (message) {
|
if (message) {
|
||||||
intFields.forEach(field => db.parseIntField(message, field));
|
db.parseIntFields(message, intFields, fields);
|
||||||
if (message.hasOwnProperty('timestamp')) {
|
if (message.hasOwnProperty('timestamp')) {
|
||||||
message.timestampISO = utils.toISOString(message.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);
|
plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next);
|
||||||
},
|
},
|
||||||
function (data, next) {
|
function (data, next) {
|
||||||
data.posts.forEach(modifyPost);
|
data.posts.forEach(post => modifyPost(post, fields));
|
||||||
next(null, Array.isArray(data.posts) ? data.posts : null);
|
next(null, Array.isArray(data.posts) ? data.posts : null);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
@@ -76,10 +76,9 @@ module.exports = function (Posts) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
function modifyPost(post) {
|
function modifyPost(post, fields) {
|
||||||
if (post) {
|
if (post) {
|
||||||
intFields.forEach(field => db.parseIntField(post, field));
|
db.parseIntFields(post, intFields, fields);
|
||||||
|
|
||||||
if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {
|
if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {
|
||||||
post.votes = post.upvotes - post.downvotes;
|
post.votes = post.upvotes - post.downvotes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ module.exports = function (Topics) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
function (topics, next) {
|
function (topics, next) {
|
||||||
topics.forEach(modifyTopic);
|
topics.forEach(topic => modifyTopic(topic, fields));
|
||||||
next(null, topics);
|
next(null, topics);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
@@ -97,12 +97,12 @@ function escapeTitle(topicData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function modifyTopic(topic) {
|
function modifyTopic(topic, fields) {
|
||||||
if (!topic) {
|
if (!topic) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
intFields.forEach(field => db.parseIntField(topic, field));
|
db.parseIntFields(topic, intFields, fields);
|
||||||
|
|
||||||
if (topic.hasOwnProperty('title')) {
|
if (topic.hasOwnProperty('title')) {
|
||||||
topic.titleRaw = topic.title;
|
topic.titleRaw = topic.title;
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ module.exports = function (User) {
|
|||||||
function (users, next) {
|
function (users, next) {
|
||||||
users = uidsToUsers(uids, uniqueUids, users);
|
users = uidsToUsers(uids, uniqueUids, users);
|
||||||
|
|
||||||
modifyUserData(users, fieldsToRemove, next);
|
modifyUserData(users, fields, fieldsToRemove, next);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
@@ -133,13 +133,13 @@ module.exports = function (User) {
|
|||||||
return uids.map(uid => 'user:' + uid);
|
return uids.map(uid => 'user:' + uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
function modifyUserData(users, fieldsToRemove, callback) {
|
function modifyUserData(users, requestedFields, fieldsToRemove, callback) {
|
||||||
users.forEach(function (user) {
|
users.forEach(function (user) {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
intFields.forEach(field => db.parseIntField(user, field));
|
db.parseIntFields(user, intFields, requestedFields);
|
||||||
|
|
||||||
if (user.hasOwnProperty('groupTitle')) {
|
if (user.hasOwnProperty('groupTitle')) {
|
||||||
parseGroupTitle(user);
|
parseGroupTitle(user);
|
||||||
|
|||||||
@@ -224,10 +224,23 @@ describe('Topic\'s', function () {
|
|||||||
assert(typeof topicData.uid === 'number');
|
assert(typeof topicData.uid === 'number');
|
||||||
assert(typeof topicData.cid === 'number');
|
assert(typeof topicData.cid === 'number');
|
||||||
assert(typeof topicData.mainPid === '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(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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user