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:
Barış Uşaklı
2026-02-11 11:38:03 -05:00
committed by GitHub
parent 52a807e795
commit c65af19985
9 changed files with 131 additions and 65 deletions

View File

@@ -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) {