people: Cache gravatar url on person object.

This commit is contained in:
evykassirer
2024-10-25 12:29:14 -07:00
committed by Tim Abbott
parent c39ae45d95
commit 774f230074
2 changed files with 53 additions and 10 deletions

View File

@@ -809,27 +809,28 @@ export function small_avatar_url_for_person(person: User): string {
}
if (person.avatar_url === null) {
return gravatar_url_for_email(person.email);
person.avatar_url = gravatar_url_for_email(person.email);
return person.avatar_url;
}
return `/avatar/${person.user_id}`;
}
function medium_gravatar_url_for_email(email: string): string {
const hash = md5(email.toLowerCase());
const avatar_url = "https://secure.gravatar.com/avatar/" + hash + "?d=identicon";
const url = new URL(avatar_url, window.location.origin);
url.search += (url.search ? "&" : "") + "s=500";
return url.href;
}
export function medium_avatar_url_for_person(person: User): string {
/* Unlike the small avatar URL case, we don't generally have a
* medium avatar URL included in person objects. So only have the
* gravatar and server endpoints here. */
if (person.avatar_url === null) {
return medium_gravatar_url_for_email(person.email);
person.avatar_url = gravatar_url_for_email(person.email);
}
if (person.avatar_url !== undefined) {
const url = new URL(person.avatar_url, window.location.origin);
if (url.origin === "https://secure.gravatar.com") {
url.search += (url.search ? "&" : "") + "s=500";
return url.href;
}
}
// We need to attach a version to the URL as a cache-breaker so that the browser