refactor user/category data

v1.18.x
Barış Soner Uşaklı 6 years ago
parent c3a903142a
commit f53984aeae

@ -6,37 +6,86 @@ 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) { Categories.getCategoriesFields = function (cids, fields, callback) {
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(cid => 'category:' + cid);
async.waterfall([ async.waterfall([
function (next) { function (next) {
db.getObject('category:' + cid, next); if (fields.length) {
db.getObjectsFields(keys, fields, next);
} else {
db.getObjects(keys, next);
}
}, },
function (category, next) { function (categories, next) {
modifyCategory(category); categories.forEach(modifyCategory);
next(null, category); next(null, 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.getCategoriesData = function (cids, callback) {
Categories.getCategoriesFields(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) { function modifyCategory(category) {
if (!category) { if (!category) {
return; return;
} }
intFields.forEach(field => db.parseIntField(category, field));
if (category.hasOwnProperty('name')) { if (category.hasOwnProperty('name')) {
category.name = validator.escape(String(category.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')) { if (category.hasOwnProperty('icon')) {
category.icon = category.icon || 'hidden'; category.icon = category.icon || 'hidden';
@ -61,57 +110,3 @@ module.exports = function (Categories) {
category.descriptionParsed = category.descriptionParsed || 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) {
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 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) {

Loading…
Cancel
Save