mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor(user): all plugins to change list of icon background colours
One notable change is line 200, where a conditional was changed. The conditional used to check for `user.hasOwnProperty('picture')` and was added so that icons would only be included in the response if the picture was requested. This doesn't seem to apply as picture could be set regardless (see default avatar logic above), so I explicitly check `requestedFields` now.
This commit is contained in:
@@ -19,12 +19,6 @@ const intFields = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
module.exports = function (User) {
|
module.exports = function (User) {
|
||||||
const iconBackgrounds = [
|
|
||||||
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
|
|
||||||
'#009688', '#1b5e20', '#33691e', '#827717', '#e65100', '#ff5722',
|
|
||||||
'#795548', '#607d8b',
|
|
||||||
];
|
|
||||||
|
|
||||||
const fieldWhitelist = [
|
const fieldWhitelist = [
|
||||||
'uid', 'username', 'userslug', 'email', 'email:confirmed', 'joindate',
|
'uid', 'username', 'userslug', 'email', 'email:confirmed', 'joindate',
|
||||||
'lastonline', 'picture', 'fullname', 'location', 'birthday', 'website',
|
'lastonline', 'picture', 'fullname', 'location', 'birthday', 'website',
|
||||||
@@ -203,7 +197,8 @@ module.exports = function (User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// User Icons
|
// User Icons
|
||||||
if (user.hasOwnProperty('picture') && user.username && parseInt(user.uid, 10) && !meta.config.defaultAvatar) {
|
if (requestedFields.includes('picture') && user.username && parseInt(user.uid, 10) && !meta.config.defaultAvatar) {
|
||||||
|
const iconBackgrounds = await User.getIconBackgrounds(user.uid);
|
||||||
user['icon:text'] = (user.username[0] || '').toUpperCase();
|
user['icon:text'] = (user.username[0] || '').toUpperCase();
|
||||||
user['icon:bgColor'] = iconBackgrounds[Array.prototype.reduce.call(user.username, (cur, next) => cur + next.charCodeAt(), 0) % iconBackgrounds.length];
|
user['icon:bgColor'] = iconBackgrounds[Array.prototype.reduce.call(user.username, (cur, next) => cur + next.charCodeAt(), 0) % iconBackgrounds.length];
|
||||||
}
|
}
|
||||||
@@ -272,6 +267,17 @@ module.exports = function (User) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
User.getIconBackgrounds = async (uid = 0) => {
|
||||||
|
let iconBackgrounds = [
|
||||||
|
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
|
||||||
|
'#009688', '#1b5e20', '#33691e', '#827717', '#e65100', '#ff5722',
|
||||||
|
'#795548', '#607d8b',
|
||||||
|
];
|
||||||
|
|
||||||
|
({ iconBackgrounds } = await plugins.hooks.fire('filter:user.iconBackgrounds', { uid, iconBackgrounds }));
|
||||||
|
return iconBackgrounds;
|
||||||
|
};
|
||||||
|
|
||||||
User.getDefaultAvatar = function () {
|
User.getDefaultAvatar = function () {
|
||||||
if (!meta.config.defaultAvatar) {
|
if (!meta.config.defaultAvatar) {
|
||||||
return '';
|
return '';
|
||||||
|
|||||||
Reference in New Issue
Block a user