From a1a52c4b4261096e0d0e65a62053de61b963f47d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 19 May 2020 09:48:06 +0200 Subject: [PATCH] Refactor avatar Don't use abotars, as they are no longer used in Jitsi Meet. --- app/features/settings/functions.js | 21 +++++++++++++++++++++ app/features/settings/middleware.js | 11 +++-------- app/features/settings/reducer.js | 5 ++--- package.json | 1 + 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/features/settings/functions.js b/app/features/settings/functions.js index b3a739230..60be33972 100644 --- a/app/features/settings/functions.js +++ b/app/features/settings/functions.js @@ -1,5 +1,26 @@ // @flow +import md5 from 'js-md5'; + +/** + * Generates an avatar URL for a user, given the name and email settings. + * + * @param {Object} state - The redux state. + * @returns {string} - The generated avatar URL. + */ +export function getAvatarURL(state: Object) { + const { email, name } = state.settings; + const encodedName = encodeURIComponent(name || ''); + + if (email) { + const md5email = md5.hex(email.trim().toLowerCase()); + + return `https://www.gravatar.com/avatar/${md5email}?d=https%3A%2F%2Fui-avatars.com%2Fapi%2F/${encodedName}/128`; + } + + return `https://ui-avatars.com/api/?name=${encodedName}&size=128`; +} + /** * Get's the value for the given setting, providing a default value. * diff --git a/app/features/settings/middleware.js b/app/features/settings/middleware.js index ece54ea79..49f3bc8f6 100644 --- a/app/features/settings/middleware.js +++ b/app/features/settings/middleware.js @@ -1,8 +1,8 @@ // @flow -import { getAvatarURL } from 'js-utils'; import { SET_EMAIL, SET_NAME } from './actionTypes'; import { setAvatarURL } from './actions'; +import { getAvatarURL } from './functions'; export default (store: Object) => (next: Function) => (action: Object) => { const result = next(action); @@ -10,14 +10,9 @@ export default (store: Object) => (next: Function) => (action: Object) => { switch (action.type) { case SET_EMAIL: - case SET_NAME: { - const avatarURL = getAvatarURL({ - email: state.settings.email, - id: state.settings.name - }); + case SET_NAME: + store.dispatch(setAvatarURL(getAvatarURL(state))); - store.dispatch(setAvatarURL(avatarURL)); - } } return result; diff --git a/app/features/settings/reducer.js b/app/features/settings/reducer.js index bed9a116d..ff6df6e51 100644 --- a/app/features/settings/reducer.js +++ b/app/features/settings/reducer.js @@ -1,7 +1,5 @@ // @flow -import { getAvatarURL } from 'js-utils'; - import { SET_ALWAYS_ON_TOP_WINDOW_ENABLED, SET_AUDIO_MUTED, @@ -12,6 +10,7 @@ import { SET_SERVER_TIMEOUT, SET_VIDEO_MUTED } from './actionTypes'; +import { getAvatarURL } from './functions'; type State = { avatarURL: string, @@ -28,7 +27,7 @@ const username = window.jitsiNodeAPI.osUserInfo().username; const DEFAULT_STATE = { alwaysOnTopWindowEnabled: true, - avatarURL: getAvatarURL({ id: username }), + avatarURL: getAvatarURL({ settings: { name: username } }), email: '', name: username, serverURL: undefined, diff --git a/package.json b/package.json index e4f47ee61..485677a1c 100644 --- a/package.json +++ b/package.json @@ -143,6 +143,7 @@ "file-loader": "6.0.0", "flow-bin": "0.109.0", "html-webpack-plugin": "4.0.4", + "js-md5": "0.7.3", "patch-package": "6.2.2", "precommit-hook": "3.0.0", "style-loader": "1.1.3",