From fbccf6e22f07f55f5d0c68abd57fb393df9e92c1 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 18 Feb 2021 16:20:10 -0500 Subject: [PATCH] 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. --- src/user/data.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/user/data.js b/src/user/data.js index bccea4c373..ceda1e6ddd 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -19,12 +19,6 @@ const intFields = [ ]; module.exports = function (User) { - const iconBackgrounds = [ - '#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', - '#009688', '#1b5e20', '#33691e', '#827717', '#e65100', '#ff5722', - '#795548', '#607d8b', - ]; - const fieldWhitelist = [ 'uid', 'username', 'userslug', 'email', 'email:confirmed', 'joindate', 'lastonline', 'picture', 'fullname', 'location', 'birthday', 'website', @@ -203,7 +197,8 @@ module.exports = function (User) { } // 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: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 () { if (!meta.config.defaultAvatar) { return '';