Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,7 @@ function generateAccountID(searchValue: string): number {
return hashText(searchValue, 2 ** 32);
}

/**
* Helper method to return the default avatar associated with the given accountID
*/
function getDefaultAvatar(accountID = -1, avatarURL?: string): IconAsset | undefined {
if (accountID === CONST.ACCOUNT_ID.CONCIERGE) {
return ConciergeAvatar;
}
if (accountID === CONST.ACCOUNT_ID.NOTIFICATIONS) {
return NotificationsAvatar;
}

function getAccountIDHashBucket(accountID = -1, avatarURL?: string) {
// There are 24 possible default avatars, so we choose which one this user has based
// on a simple modulo operation of their login number. Note that Avatar count starts at 1.

Expand All @@ -135,7 +125,21 @@ function getDefaultAvatar(accountID = -1, avatarURL?: string): IconAsset | undef
} else if (accountID > 0) {
accountIDHashBucket = ((accountID % CONST.DEFAULT_AVATAR_COUNT) + 1) as AvatarRange;
}
return accountIDHashBucket;
}

/**
* Helper method to return the default avatar associated with the given accountID
*/
function getDefaultAvatar(accountID = -1, avatarURL?: string): IconAsset | undefined {
if (accountID === CONST.ACCOUNT_ID.CONCIERGE) {
return ConciergeAvatar;
}
if (accountID === CONST.ACCOUNT_ID.NOTIFICATIONS) {
return NotificationsAvatar;
}

const accountIDHashBucket = getAccountIDHashBucket(accountID, avatarURL);
if (!accountIDHashBucket) {
return;
}
Expand All @@ -146,13 +150,12 @@ function getDefaultAvatar(accountID = -1, avatarURL?: string): IconAsset | undef
/**
* Helper method to return default avatar URL associated with the accountID
*/
function getDefaultAvatarURL(accountID: string | number = ''): string {
function getDefaultAvatarURL(accountID: string | number = '', avatarURL?: string): string {
if (Number(accountID) === CONST.ACCOUNT_ID.CONCIERGE) {
return CONST.CONCIERGE_ICON_URL;
}

// Note that Avatar count starts at 1 which is why 1 has to be added to the result (or else 0 would result in a broken avatar link)
const accountIDHashBucket = (Number(accountID) % CONST.DEFAULT_AVATAR_COUNT) + 1;
const accountIDHashBucket = getAccountIDHashBucket(Number(accountID) || -1, avatarURL);
const avatarPrefix = `default-avatar`;

return `${CONST.CLOUDFRONT_URL}/images/avatars/${avatarPrefix}_${accountIDHashBucket}.png`;
Expand Down Expand Up @@ -196,7 +199,7 @@ function getAvatar(avatarSource?: AvatarSource, accountID?: number): AvatarSourc
* @param accountID - the accountID of the user
*/
function getAvatarUrl(avatarSource: AvatarSource | undefined, accountID: number): AvatarSource {
return isDefaultAvatar(avatarSource) ? getDefaultAvatarURL(accountID) : avatarSource;
return isDefaultAvatar(avatarSource) ? getDefaultAvatarURL(accountID, avatarSource) : avatarSource;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/UserUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as defaultAvatars from '@components/Icon/DefaultAvatars';
import * as UserUtils from '@src/libs/UserUtils';

describe('UserUtils', () => {
Expand All @@ -6,12 +7,22 @@ describe('UserUtils', () => {
const defaultAvatar = UserUtils.getAvatar(avatarURL, 1);

expect(typeof defaultAvatar).toBe('function');

const defaultAvatarUrl = UserUtils.getAvatarUrl(avatarURL, 1);

expect(defaultAvatarUrl).toBe('https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png');
// Both defaultAvatar and defaultAvatarUrl must be `defaultAvatars.Avatar7`
expect(defaultAvatar === defaultAvatars.Avatar7).toBeTruthy();
});

it('should return the same url if url is not for default avatar', () => {
const avatarURL = 'https://test.com/images/some_avatar.png';
const avatar = UserUtils.getAvatar(avatarURL, 1);

expect(avatar).toEqual('https://test.com/images/some_avatar.png');

const avatarUrl = UserUtils.getAvatarUrl(avatarURL, 1);

expect(avatarUrl).toEqual('https://test.com/images/some_avatar.png');
});
});
Loading