-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathImageUtils.js
More file actions
48 lines (42 loc) · 1.86 KB
/
Copy pathImageUtils.js
File metadata and controls
48 lines (42 loc) · 1.86 KB
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
import md5 from "md5";
import { getRandomNumber } from "./Algos";
export const getRandomAvatar = () => {
const randomNumber = getRandomNumber(1, 6);
if (window.location.origin.includes("localhost")) {
return "https://yoda.hypeople.studio/yoda-admin-template/react/static/media/memoji-1.afa5922f.png";
}
return `https://app.requestly.io/assets/img/memoji/png/analytics-marketing-team-${randomNumber}.png`;
};
export const generateGravatarURL = (email = "sagar@requestly.io") => {
return `https://www.gravatar.com/avatar/${md5(email)}?s=200&d=${getRandomAvatar()}`;
};
/**
* True when `url`'s host is Gravatar. Parses the URL and matches on the hostname
* rather than a substring — `url.includes("gravatar.com")` would also match
* unrelated hosts such as `gravatar.com.evil.com` or `host/path?ref=gravatar.com`.
* Unparseable values return false (treated as "not a Gravatar URL").
*/
const isGravatarUrl = (url) => {
try {
const host = new URL(url).hostname.toLowerCase();
return host === "gravatar.com" || host.endsWith(".gravatar.com");
} catch {
return false;
}
};
/**
* Resolves the avatar to display for a user.
*
* Precedence: the auth provider's photo (e.g. Google) when we have a real one,
* otherwise a Gravatar resolved live from the current login email — so changes a
* user makes on Gravatar are reflected here. For email/password accounts we persist
* a synthetic gravatar.com URL as `photoURL`, so any Gravatar-hosted value is treated
* as "no provider photo" and re-resolved live from the email. `d=mp` is Gravatar's
* deterministic fallback, shown when the email has no Gravatar image.
*/
export const getUserAvatarUrl = (email = "", providerPhotoURL = "") => {
if (providerPhotoURL && !isGravatarUrl(providerPhotoURL)) {
return providerPhotoURL;
}
return `https://www.gravatar.com/avatar/${md5(email)}?s=200&d=mp`;
};