mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-18 03:31:03 +01:00
refactor user/category data
This commit is contained in:
@@ -6,74 +6,16 @@ var winston = require('winston');
|
|||||||
|
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
|
|
||||||
|
const intFields = ['cid', 'parentCid', 'disabled', 'isSection', 'order', 'topic_count', 'post_count'];
|
||||||
|
|
||||||
module.exports = function (Categories) {
|
module.exports = function (Categories) {
|
||||||
Categories.getCategoryData = function (cid, callback) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
db.getObject('category:' + cid, next);
|
|
||||||
},
|
|
||||||
function (category, next) {
|
|
||||||
modifyCategory(category);
|
|
||||||
next(null, category);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Categories.getCategoriesData = function (cids, callback) {
|
|
||||||
Categories.getCategoriesFields(cids, [], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
function modifyCategory(category) {
|
|
||||||
if (!category) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (category.hasOwnProperty('post_count')) {
|
|
||||||
category.post_count = category.post_count || 0;
|
|
||||||
category.totalPostCount = category.post_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (category.hasOwnProperty('topic_count')) {
|
|
||||||
category.topic_count = category.topic_count || 0;
|
|
||||||
category.totalTopicCount = category.topic_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (category.image) {
|
|
||||||
category.backgroundImage = category.image;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (category.description) {
|
|
||||||
category.description = validator.escape(String(category.description));
|
|
||||||
category.descriptionParsed = category.descriptionParsed || category.description;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Categories.getCategoryField = function (cid, field, callback) {
|
|
||||||
db.getObjectField('category:' + cid, field, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Categories.getCategoriesFields = function (cids, fields, callback) {
|
Categories.getCategoriesFields = function (cids, fields, callback) {
|
||||||
if (!Array.isArray(cids) || !cids.length) {
|
if (!Array.isArray(cids) || !cids.length) {
|
||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys = cids.map(function (cid) {
|
var keys = cids.map(cid => 'category:' + cid);
|
||||||
return 'category:' + cid;
|
|
||||||
});
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
if (fields.length) {
|
if (fields.length) {
|
||||||
@@ -89,6 +31,28 @@ module.exports = function (Categories) {
|
|||||||
], callback);
|
], 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) {
|
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
|
||||||
winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
|
winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
|
||||||
Categories.getCategoriesFields(cids, fields, callback);
|
Categories.getCategoriesFields(cids, fields, callback);
|
||||||
@@ -103,10 +67,6 @@ module.exports = function (Categories) {
|
|||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.getCategoryFields = function (cid, fields, callback) {
|
|
||||||
db.getObjectFields('category:' + cid, fields, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Categories.setCategoryField = function (cid, field, value, callback) {
|
Categories.setCategoryField = function (cid, field, value, callback) {
|
||||||
db.setObjectField('category:' + cid, field, value, callback);
|
db.setObjectField('category:' + cid, field, value, callback);
|
||||||
};
|
};
|
||||||
@@ -115,3 +75,38 @@ module.exports = function (Categories) {
|
|||||||
db.incrObjectFieldBy('category:' + 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('icon')) {
|
||||||
|
category.icon = category.icon || 'hidden';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (category.hasOwnProperty('post_count')) {
|
||||||
|
category.post_count = category.post_count || 0;
|
||||||
|
category.totalPostCount = category.post_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (category.hasOwnProperty('topic_count')) {
|
||||||
|
category.topic_count = category.topic_count || 0;
|
||||||
|
category.totalTopicCount = category.topic_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (category.image) {
|
||||||
|
category.backgroundImage = category.image;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (category.description) {
|
||||||
|
category.description = validator.escape(String(category.description));
|
||||||
|
category.descriptionParsed = category.descriptionParsed || category.description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ var meta = require('../meta');
|
|||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
var utils = require('../utils');
|
var utils = require('../utils');
|
||||||
|
|
||||||
|
const intFields = ['uid', 'postcount', 'topiccount', 'banned'];
|
||||||
|
|
||||||
module.exports = function (User) {
|
module.exports = function (User) {
|
||||||
var iconBackgrounds = [
|
var iconBackgrounds = [
|
||||||
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
|
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
|
||||||
@@ -27,26 +29,12 @@ module.exports = function (User) {
|
|||||||
'cover:position', 'groupTitle',
|
'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) {
|
User.getUsersFields = function (uids, fields, callback) {
|
||||||
if (!Array.isArray(uids) || !uids.length) {
|
if (!Array.isArray(uids) || !uids.length) {
|
||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
uids = uids.map(function (uid) {
|
uids = uids.map(uid => (isNaN(uid) ? 0 : uid));
|
||||||
return isNaN(uid) ? 0 : uid;
|
|
||||||
});
|
|
||||||
|
|
||||||
var fieldsToRemove = [];
|
var fieldsToRemove = [];
|
||||||
function addField(field) {
|
function addField(field) {
|
||||||
@@ -76,8 +64,9 @@ module.exports = function (User) {
|
|||||||
},
|
},
|
||||||
function (results, next) {
|
function (results, next) {
|
||||||
if (fields.length) {
|
if (fields.length) {
|
||||||
|
const whitelistSet = new Set(results.whitelist);
|
||||||
fields = fields.filter(function (field) {
|
fields = fields.filter(function (field) {
|
||||||
var isFieldWhitelisted = field && results.whitelist.includes(field);
|
var isFieldWhitelisted = field && whitelistSet.has(field);
|
||||||
if (!isFieldWhitelisted) {
|
if (!isFieldWhitelisted) {
|
||||||
winston.verbose('[user/getUsersFields] ' + field + ' removed because it is not whitelisted, see `filter:user.whitelistFields`');
|
winston.verbose('[user/getUsersFields] ' + field + ' removed because it is not whitelisted, see `filter:user.whitelistFields`');
|
||||||
}
|
}
|
||||||
@@ -97,6 +86,19 @@ module.exports = function (User) {
|
|||||||
], callback);
|
], 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) {
|
User.getMultipleUserFields = function (uids, fields, callback) {
|
||||||
winston.warn('[deprecated] User.getMultipleUserFields is deprecated please use User.getUsersFields');
|
winston.warn('[deprecated] User.getMultipleUserFields is deprecated please use User.getUsersFields');
|
||||||
User.getUsersFields(uids, fields, callback);
|
User.getUsersFields(uids, fields, callback);
|
||||||
@@ -137,6 +139,9 @@ module.exports = function (User) {
|
|||||||
if (!user) {
|
if (!user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intFields.forEach(field => db.parseIntField(user, field));
|
||||||
|
|
||||||
if (user.hasOwnProperty('groupTitle')) {
|
if (user.hasOwnProperty('groupTitle')) {
|
||||||
parseGroupTitle(user);
|
parseGroupTitle(user);
|
||||||
}
|
}
|
||||||
@@ -218,16 +223,7 @@ module.exports = function (User) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
User.setUserField = function (uid, field, value, callback) {
|
User.setUserField = function (uid, field, value, callback) {
|
||||||
callback = callback || function () {};
|
User.setUserFields(uid, { [field]: value }, callback);
|
||||||
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 = function (uid, data, callback) {
|
User.setUserFields = function (uid, data, callback) {
|
||||||
|
|||||||
Reference in New Issue
Block a user