Skip to content

Commit

Permalink
Add getAvatarFromUser and getAvatarFromMessage
Browse files Browse the repository at this point in the history
We duplicate the same trivaial code for extracting an avatar's url
either from a User object or a Message object.

This code extracts that into two utility functions and replaces the
duplicate code with function calls. DRY.
  • Loading branch information
borisyankov committed Feb 12, 2019
1 parent cb67406 commit f13d571
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
9 changes: 2 additions & 7 deletions src/common/OwnAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { connect } from 'react-redux';
import type { GlobalState, User } from '../types';
import { getCurrentRealm, getSelfUserDetail } from '../selectors';
import ImageAvatar from './ImageAvatar';
import { getFullUrl } from '../utils/url';
import { getGravatarFromEmail } from '../utils/avatar';
import { getAvatarFromUser } from '../utils/avatar';

type Props = {|
user: User,
Expand All @@ -31,11 +30,7 @@ class OwnAvatar extends PureComponent<Props> {

render() {
const { user, size, realm, shape } = this.props;
const fullAvatarUrl =
typeof user.avatar_url === 'string'
? getFullUrl(user.avatar_url, realm)
: getGravatarFromEmail(user.email);

const fullAvatarUrl = getAvatarFromUser(user, realm);
return <ImageAvatar avatarUrl={fullAvatarUrl} size={size} shape={shape} />;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lightbox/Lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import LightboxHeader from './LightboxHeader';
import LightboxFooter from './LightboxFooter';
import { constructActionSheetButtons, executeActionSheetAction } from './LightboxActionSheet';
import { NAVBAR_SIZE } from '../styles';
import { getGravatarFromEmail } from '../utils/avatar';
import { getAvatarFromMessage } from '../utils/avatar';
import { navigateBack } from '../actions';

const styles = StyleSheet.create({
Expand Down Expand Up @@ -113,7 +113,7 @@ class Lightbox extends PureComponent<Props, State> {
<LightboxHeader
onPressBack={this.handlePressBack}
timestamp={message.timestamp}
avatarUrl={message.avatar_url || getGravatarFromEmail(message.sender_email)}
avatarUrl={getAvatarFromMessage(message)}
senderName={message.sender_full_name}
/>
</SlideAnimationView>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/avatar-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @flow strict */
/* @flow strict-local */
import { getMediumAvatar, getGravatarFromEmail } from '../avatar';

// avatarUrl can be converted to retrieve medium sized avatars(mediumAvatarUrl) if and only if
Expand Down
15 changes: 14 additions & 1 deletion src/utils/avatar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* @flow strict */
/* @flow strict-local */
import md5 from 'blueimp-md5';

import type { Message, Outbox, User } from '../types';
import { getFullUrl } from './url';

export const getMediumAvatar = (avatarUrl: string): string => {
const matches = new RegExp(/(\w+)\.png/g).exec(avatarUrl);

Expand All @@ -9,3 +12,13 @@ export const getMediumAvatar = (avatarUrl: string): string => {

export const getGravatarFromEmail = (email: string = '', size: number = 80): string =>
`https://secure.gravatar.com/avatar/${md5(email.toLowerCase())}?d=identicon&s=${size}`;

export const getAvatarFromUser = (user: User, realm: string): string =>
typeof user.avatar_url === 'string'
? getFullUrl(user.avatar_url, realm)
: getGravatarFromEmail(user.email);

export const getAvatarFromMessage = (message: Message | Outbox): string =>
typeof message.avatar_url === 'string'
? message.avatar_url
: getGravatarFromEmail(message.sender_email);
7 changes: 2 additions & 5 deletions src/webview/html/messageAsHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
RealmEmojiType,
} from '../../types';
import type { BackgroundData } from '../MessageList';
import { getGravatarFromEmail } from '../../utils/avatar';
import { getAvatarFromMessage } from '../../utils/avatar';
import { shortTime } from '../../utils/date';
import aggregateReactions from '../../reactions/aggregateReactions';
import { codeToEmojiMap } from '../../emoji/data';
Expand Down Expand Up @@ -96,10 +96,7 @@ $!${divOpenHtml}
}

const { sender_full_name, sender_email, timestamp } = message;
const avatarUrl =
typeof message.avatar_url === 'string'
? message.avatar_url
: getGravatarFromEmail(message.sender_email);
const avatarUrl = getAvatarFromMessage(message);
const subheaderHtml = template`
<div class="subheader">
<div class="username">
Expand Down
9 changes: 2 additions & 7 deletions src/webview/html/messageTypingAsHtml.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
/* @flow strict-local */
import template from './template';
import type { User } from '../../types';
import { getFullUrl } from '../../utils/url';
import { getGravatarFromEmail } from '../../utils/avatar';
import { getAvatarFromUser } from '../../utils/avatar';

const typingAvatar = (realm: string, user: User): string => template`
<div class="avatar">
<img
class="avatar-img"
data-email="${user.email}"
src="${
typeof user.avatar_url === 'string'
? getFullUrl(user.avatar_url, realm)
: getGravatarFromEmail(user.email)
}">
src="${getAvatarFromUser(user, realm)}">
</div>
`;

Expand Down

0 comments on commit f13d571

Please sign in to comment.