-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathUserUtils.js
231 lines (210 loc) · 7.22 KB
/
UserUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import _ from 'underscore';
import lodashGet from 'lodash/get';
import CONST from '../CONST';
import hashCode from './hashCode';
import * as Expensicons from '../components/Icon/Expensicons';
import * as defaultAvatars from '../components/Icon/DefaultAvatars';
/**
* Searches through given loginList for any contact method / login with an error.
*
* Example that should return false:
* {{
* [email protected]: {
* errorFields: {
* validateCodeSent: null
* }
* }
* }}
*
* Example that should return true:
* {{
* [email protected]: {
* errorFields: {
* validateCodeSent: { 18092081290: 'An error' }
* }
* }
* }}
*
* @param {Object} loginList
* @param {Object} loginList.errorFields
* @returns {Boolean}
*/
function hasLoginListError(loginList) {
return _.some(loginList, (login) => _.some(lodashGet(login, 'errorFields', {}), (field) => !_.isEmpty(field)));
}
/**
* Searches through given loginList for any contact method / login that requires
* an Info brick road status indicator. Currently this only applies if the user
* has an unvalidated contact method.
*
* @param {Object} loginList
* @param {String} loginList.validatedDate
* @returns {Boolean}
*/
function hasLoginListInfo(loginList) {
return _.some(loginList, (login) => _.isEmpty(login.validatedDate));
}
/**
* Gets the appropriate brick road indicator status for a given loginList.
* Error status is higher priority, so we check for that first.
*
* @param {Object} loginList
* @returns {String}
*/
function getLoginListBrickRoadIndicator(loginList) {
if (hasLoginListError(loginList)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
if (hasLoginListInfo(loginList)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
}
return '';
}
/**
* Hashes provided string and returns a value between [0, range)
* @param {String} text
* @param {Number} range
* @returns {Number}
*/
function hashText(text, range) {
return Math.abs(hashCode(text.toLowerCase())) % range;
}
/**
* Helper method to return the default avatar associated with the given accountID
* @param {Number} [accountID]
* @returns {String}
*/
function getDefaultAvatar(accountID = -1) {
if (accountID <= 0) {
return Expensicons.FallbackAvatar;
}
if (Number(accountID) === CONST.ACCOUNT_ID.CONCIERGE) {
return Expensicons.ConciergeAvatar;
}
// 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.
const accountIDHashBucket = (accountID % CONST.DEFAULT_AVATAR_COUNT) + 1;
return defaultAvatars[`Avatar${accountIDHashBucket}`];
}
/**
* Helper method to return default avatar URL associated with login
*
* @param {Number} [accountID]
* @param {Boolean} [isNewDot]
* @returns {String}
*/
function getDefaultAvatarURL(accountID = '', isNewDot = false) {
if (Number(accountID) === CONST.ACCOUNT_ID.CONCIERGE) {
return CONST.CONCIERGE_ICON_URL;
}
// The default avatar for a user is based on a simple hash of their accountID.
// 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 = hashText(String(accountID), isNewDot ? CONST.DEFAULT_AVATAR_COUNT : CONST.OLD_DEFAULT_AVATAR_COUNT) + 1;
const avatarPrefix = isNewDot ? `default-avatar` : `avatar`;
return `${CONST.CLOUDFRONT_URL}/images/avatars/${avatarPrefix}_${accountIDHashBucket}.png`;
}
/**
* Given a user's avatar path, returns true if user doesn't have an avatar or if URL points to a default avatar
* @param {String} [avatarURL] - the avatar source from user's personalDetails
* @returns {Boolean}
*/
function isDefaultAvatar(avatarURL) {
if (
_.isString(avatarURL) &&
(avatarURL.includes('images/avatars/avatar_') || avatarURL.includes('images/avatars/default-avatar_') || avatarURL.includes('images/avatars/user/default'))
) {
return true;
}
// We use a hardcoded "default" Concierge avatar
if (_.isString(avatarURL) && (avatarURL === CONST.CONCIERGE_ICON_URL_2021 || avatarURL === CONST.CONCIERGE_ICON_URL)) {
return true;
}
if (!avatarURL) {
// If null URL, we should also use a default avatar
return true;
}
return false;
}
/**
* Provided a source URL, if source is a default avatar, return the associated SVG.
* Otherwise, return the URL pointing to a user-uploaded avatar.
*
* @param {String} avatarURL - the avatar source from user's personalDetails
* @param {Number} accountID - the accountID of the user
* @returns {String|Function}
*/
function getAvatar(avatarURL, accountID) {
return isDefaultAvatar(avatarURL) ? getDefaultAvatar(accountID) : avatarURL;
}
/**
* Provided an avatar URL, if avatar is a default avatar, return NewDot default avatar URL.
* Otherwise, return the URL pointing to a user-uploaded avatar.
*
* @param {String} avatarURL - the avatar source from user's personalDetails
* @param {Number} accountID - the accountID of the user
* @returns {String}
*/
function getAvatarUrl(avatarURL, accountID) {
return isDefaultAvatar(avatarURL) ? getDefaultAvatarURL(accountID, true) : avatarURL;
}
/**
* Avatars uploaded by users will have a _128 appended so that the asset server returns a small version.
* This removes that part of the URL so the full version of the image can load.
*
* @param {String} [avatarURL]
* @param {Number} [accountID]
* @returns {String|Function}
*/
function getFullSizeAvatar(avatarURL, accountID) {
const source = getAvatar(avatarURL, accountID);
if (!_.isString(source)) {
return source;
}
return source.replace('_128', '');
}
/**
* Small sized avatars end with _128.<file-type>. This adds the _128 at the end of the
* source URL (before the file type) if it doesn't exist there already.
*
* @param {String} avatarURL
* @param {Number} accountID
* @returns {String|Function}
*/
function getSmallSizeAvatar(avatarURL, accountID) {
const source = getAvatar(avatarURL, accountID);
if (!_.isString(source)) {
return source;
}
// Because other urls than CloudFront do not support dynamic image sizing (_SIZE suffix), the current source is already what we want to use here.
if (!CONST.CLOUDFRONT_DOMAIN_REGEX.test(source)) {
return source;
}
// If image source already has _128 at the end, the given avatar URL is already what we want to use here.
const lastPeriodIndex = source.lastIndexOf('.');
if (source.substring(lastPeriodIndex - 4, lastPeriodIndex) === '_128') {
return source;
}
return `${source.substring(0, lastPeriodIndex)}_128${source.substring(lastPeriodIndex)}`;
}
/**
* Generate a random accountID base on searchValue.
* @param {String} searchValue
* @returns {Number}
*/
function generateAccountID(searchValue) {
return hashText(searchValue, 2 ** 32);
}
export {
hashText,
hasLoginListError,
hasLoginListInfo,
getLoginListBrickRoadIndicator,
getDefaultAvatar,
getDefaultAvatarURL,
isDefaultAvatar,
getAvatar,
getAvatarUrl,
getSmallSizeAvatar,
getFullSizeAvatar,
generateAccountID,
};