Skip to content

Commit

Permalink
Refactor avatar
Browse files Browse the repository at this point in the history
Don't use abotars, as they are no longer used in Jitsi Meet.
  • Loading branch information
saghul committed May 20, 2020
1 parent 9b84fe5 commit a1a52c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
21 changes: 21 additions & 0 deletions app/features/settings/functions.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down
11 changes: 3 additions & 8 deletions app/features/settings/middleware.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
// @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);
const state = store.getState();

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;
Expand Down
5 changes: 2 additions & 3 deletions app/features/settings/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @flow

import { getAvatarURL } from 'js-utils';

import {
SET_ALWAYS_ON_TOP_WINDOW_ENABLED,
SET_AUDIO_MUTED,
Expand All @@ -12,6 +10,7 @@ import {
SET_SERVER_TIMEOUT,
SET_VIDEO_MUTED
} from './actionTypes';
import { getAvatarURL } from './functions';

type State = {
avatarURL: string,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a1a52c4

Please sign in to comment.