mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 19:21:04 +01:00
refactor user/category data
This commit is contained in:
@@ -6,37 +6,86 @@ var winston = require('winston');
|
||||
|
||||
var db = require('../database');
|
||||
|
||||
const intFields = ['cid', 'parentCid', 'disabled', 'isSection', 'order', 'topic_count', 'post_count'];
|
||||
|
||||
module.exports = function (Categories) {
|
||||
Categories.getCategoryData = function (cid, callback) {
|
||||
Categories.getCategoriesFields = function (cids, fields, callback) {
|
||||
if (!Array.isArray(cids) || !cids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
var keys = cids.map(cid => 'category:' + cid);
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObject('category:' + cid, next);
|
||||
if (fields.length) {
|
||||
db.getObjectsFields(keys, fields, next);
|
||||
} else {
|
||||
db.getObjects(keys, next);
|
||||
}
|
||||
},
|
||||
function (category, next) {
|
||||
modifyCategory(category);
|
||||
next(null, category);
|
||||
function (categories, next) {
|
||||
categories.forEach(modifyCategory);
|
||||
next(null, categories);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getCategoryData = function (cid, callback) {
|
||||
Categories.getCategoriesFields([cid], [], function (err, categories) {
|
||||
callback(err, categories && categories.length ? categories[0] : null);
|
||||
});
|
||||
};
|
||||
|
||||
Categories.getCategoriesData = function (cids, callback) {
|
||||
Categories.getCategoriesFields(cids, [], callback);
|
||||
};
|
||||
|
||||
Categories.getCategoryField = function (cid, field, callback) {
|
||||
Categories.getCategoryFields(cid, [field], function (err, category) {
|
||||
callback(err, category ? category[field] : null);
|
||||
});
|
||||
};
|
||||
|
||||
Categories.getCategoryFields = function (cid, fields, callback) {
|
||||
Categories.getCategoriesFields([cid], fields, function (err, categories) {
|
||||
callback(err, categories ? categories[0] : null);
|
||||
});
|
||||
};
|
||||
|
||||
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
|
||||
winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
|
||||
Categories.getCategoriesFields(cids, fields, callback);
|
||||
};
|
||||
|
||||
Categories.getAllCategoryFields = function (fields, callback) {
|
||||
async.waterfall([
|
||||
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
|
||||
function (cids, next) {
|
||||
Categories.getCategoriesFields(cids, fields, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.setCategoryField = function (cid, field, value, callback) {
|
||||
db.setObjectField('category:' + cid, field, value, callback);
|
||||
};
|
||||
|
||||
Categories.incrementCategoryFieldBy = function (cid, field, value, callback) {
|
||||
db.incrObjectFieldBy('category:' + cid, field, value, callback);
|
||||
};
|
||||
};
|
||||
|
||||
function modifyCategory(category) {
|
||||
if (!category) {
|
||||
return;
|
||||
}
|
||||
|
||||
intFields.forEach(field => db.parseIntField(category, field));
|
||||
|
||||
if (category.hasOwnProperty('name')) {
|
||||
category.name = validator.escape(String(category.name || ''));
|
||||
}
|
||||
if (category.hasOwnProperty('disabled')) {
|
||||
category.disabled = parseInt(category.disabled, 10) === 1;
|
||||
}
|
||||
if (category.hasOwnProperty('isSection')) {
|
||||
category.isSection = parseInt(category.isSection, 10) === 1;
|
||||
}
|
||||
|
||||
if (category.hasOwnProperty('icon')) {
|
||||
category.icon = category.icon || 'hidden';
|
||||
@@ -61,57 +110,3 @@ module.exports = function (Categories) {
|
||||
category.descriptionParsed = category.descriptionParsed || category.description;
|
||||
}
|
||||
}
|
||||
|
||||
Categories.getCategoryField = function (cid, field, callback) {
|
||||
db.getObjectField('category:' + cid, field, callback);
|
||||
};
|
||||
|
||||
Categories.getCategoriesFields = function (cids, fields, callback) {
|
||||
if (!Array.isArray(cids) || !cids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
var keys = cids.map(function (cid) {
|
||||
return 'category:' + cid;
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (fields.length) {
|
||||
db.getObjectsFields(keys, fields, next);
|
||||
} else {
|
||||
db.getObjects(keys, next);
|
||||
}
|
||||
},
|
||||
function (categories, next) {
|
||||
categories.forEach(modifyCategory);
|
||||
next(null, categories);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
|
||||
winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
|
||||
Categories.getCategoriesFields(cids, fields, callback);
|
||||
};
|
||||
|
||||
Categories.getAllCategoryFields = function (fields, callback) {
|
||||
async.waterfall([
|
||||
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
|
||||
function (cids, next) {
|
||||
Categories.getCategoriesFields(cids, fields, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Categories.getCategoryFields = function (cid, fields, callback) {
|
||||
db.getObjectFields('category:' + cid, fields, callback);
|
||||
};
|
||||
|
||||
Categories.setCategoryField = function (cid, field, value, callback) {
|
||||
db.setObjectField('category:' + cid, field, value, callback);
|
||||
};
|
||||
|
||||
Categories.incrementCategoryFieldBy = function (cid, field, value, callback) {
|
||||
db.incrObjectFieldBy('category:' + cid, field, value, callback);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -11,6 +11,8 @@ var meta = require('../meta');
|
||||
var plugins = require('../plugins');
|
||||
var utils = require('../utils');
|
||||
|
||||
const intFields = ['uid', 'postcount', 'topiccount', 'banned'];
|
||||
|
||||
module.exports = function (User) {
|
||||
var iconBackgrounds = [
|
||||
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
|
||||
@@ -27,26 +29,12 @@ module.exports = function (User) {
|
||||
'cover:position', 'groupTitle',
|
||||
];
|
||||
|
||||
User.getUserField = function (uid, field, callback) {
|
||||
User.getUserFields(uid, [field], function (err, user) {
|
||||
callback(err, user ? user[field] : null);
|
||||
});
|
||||
};
|
||||
|
||||
User.getUserFields = function (uid, fields, callback) {
|
||||
User.getUsersFields([uid], fields, function (err, users) {
|
||||
callback(err, users ? users[0] : null);
|
||||
});
|
||||
};
|
||||
|
||||
User.getUsersFields = function (uids, fields, callback) {
|
||||
if (!Array.isArray(uids) || !uids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
uids = uids.map(function (uid) {
|
||||
return isNaN(uid) ? 0 : uid;
|
||||
});
|
||||
uids = uids.map(uid => (isNaN(uid) ? 0 : uid));
|
||||
|
||||
var fieldsToRemove = [];
|
||||
function addField(field) {
|
||||
@@ -76,8 +64,9 @@ module.exports = function (User) {
|
||||
},
|
||||
function (results, next) {
|
||||
if (fields.length) {
|
||||
const whitelistSet = new Set(results.whitelist);
|
||||
fields = fields.filter(function (field) {
|
||||
var isFieldWhitelisted = field && results.whitelist.includes(field);
|
||||
var isFieldWhitelisted = field && whitelistSet.has(field);
|
||||
if (!isFieldWhitelisted) {
|
||||
winston.verbose('[user/getUsersFields] ' + field + ' removed because it is not whitelisted, see `filter:user.whitelistFields`');
|
||||
}
|
||||
@@ -97,6 +86,19 @@ module.exports = function (User) {
|
||||
], callback);
|
||||
};
|
||||
|
||||
|
||||
User.getUserField = function (uid, field, callback) {
|
||||
User.getUserFields(uid, [field], function (err, user) {
|
||||
callback(err, user ? user[field] : null);
|
||||
});
|
||||
};
|
||||
|
||||
User.getUserFields = function (uid, fields, callback) {
|
||||
User.getUsersFields([uid], fields, function (err, users) {
|
||||
callback(err, users ? users[0] : null);
|
||||
});
|
||||
};
|
||||
|
||||
User.getMultipleUserFields = function (uids, fields, callback) {
|
||||
winston.warn('[deprecated] User.getMultipleUserFields is deprecated please use User.getUsersFields');
|
||||
User.getUsersFields(uids, fields, callback);
|
||||
@@ -137,6 +139,9 @@ module.exports = function (User) {
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
intFields.forEach(field => db.parseIntField(user, field));
|
||||
|
||||
if (user.hasOwnProperty('groupTitle')) {
|
||||
parseGroupTitle(user);
|
||||
}
|
||||
@@ -218,16 +223,7 @@ module.exports = function (User) {
|
||||
};
|
||||
|
||||
User.setUserField = function (uid, field, value, callback) {
|
||||
callback = callback || function () {};
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.setObjectField('user:' + uid, field, value, next);
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('action:user.set', { uid: uid, field: field, value: value, type: 'set' });
|
||||
next();
|
||||
},
|
||||
], callback);
|
||||
User.setUserFields(uid, { [field]: value }, callback);
|
||||
};
|
||||
|
||||
User.setUserFields = function (uid, data, callback) {
|
||||
|
||||
Reference in New Issue
Block a user