mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-24 07:31:15 +01:00
refactor: add createFieldChecker (#13973)
* refactor: add createFieldChecker * refactor: use hasField in topic/data.js * refactor: use hasField in categories/data.js * test: fix category nickname logic * test: fix spec
This commit is contained in:
@@ -58,20 +58,26 @@ module.exports = function (Posts) {
|
||||
function modifyPost(post, fields) {
|
||||
if (post) {
|
||||
db.parseIntFields(post, intFields, fields);
|
||||
if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {
|
||||
|
||||
const hasField = utils.createFieldChecker(fields);
|
||||
|
||||
if (hasField('upvotes') && hasField('downvotes')) {
|
||||
post.votes = post.upvotes - post.downvotes;
|
||||
}
|
||||
if (post.hasOwnProperty('timestamp')) {
|
||||
|
||||
if (hasField('timestamp')) {
|
||||
post.timestampISO = utils.toISOString(post.timestamp);
|
||||
}
|
||||
if (post.hasOwnProperty('edited')) {
|
||||
|
||||
if (hasField('edited')) {
|
||||
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
|
||||
}
|
||||
if (!fields.length || fields.includes('attachments')) {
|
||||
|
||||
if (hasField('attachments')) {
|
||||
post.attachments = (post.attachments || '').split(',').filter(Boolean);
|
||||
}
|
||||
|
||||
if (!fields.length || fields.includes('uploads')) {
|
||||
if (hasField('uploads')) {
|
||||
try {
|
||||
post.uploads = post.uploads ? JSON.parse(post.uploads) : [];
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user