2023-02-17 19:53:31 +00:00
|
|
|
import md5Parser from "md5";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2023-03-10 22:10:56 +00:00
|
|
|
/**
|
|
|
|
* Provided either an email or an MD5 hash, return the URL for the Gravatar
|
|
|
|
* image aborting early if neither is provided.
|
|
|
|
*/
|
2022-07-23 00:39:50 +00:00
|
|
|
export const defaultAvatarSrc = function ({ email, md5 }: { md5?: string; email?: string }) {
|
|
|
|
if (!email && !md5) return "";
|
|
|
|
|
|
|
|
if (email && !md5) {
|
2023-02-17 19:53:31 +00:00
|
|
|
md5 = md5Parser(email);
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 10:23:39 +00:00
|
|
|
return `https://www.gravatar.com/avatar/${md5}?s=160&d=mp&r=PG`;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
2022-07-27 22:12:27 +00:00
|
|
|
|
2023-03-10 22:10:56 +00:00
|
|
|
/**
|
|
|
|
* Given an avatar URL and a name, return the appropriate avatar URL. In the
|
|
|
|
* event that no avatar URL is provided, return a placeholder avatar URL from
|
|
|
|
* ui-avatars.com.
|
|
|
|
*
|
|
|
|
* ui-avatars.com is a free service that generates placeholder avatars based on
|
|
|
|
* a name. It is used here to provide a consistent placeholder avatar for users
|
|
|
|
* who have not uploaded an avatar.
|
|
|
|
*/
|
2022-07-27 22:12:27 +00:00
|
|
|
export function getPlaceholderAvatar(avatar: string | null | undefined, name: string | null) {
|
|
|
|
return avatar
|
|
|
|
? avatar
|
|
|
|
: "https://eu.ui-avatars.com/api/?background=fff&color=f9f9f9&bold=true&background=000000&name=" +
|
|
|
|
encodeURIComponent(name || "");
|
|
|
|
}
|