Skip to content

Commit

Permalink
Uses GitHub Avatar for GH 'noreply' email address
Browse files Browse the repository at this point in the history
Partial fix for #281- covers the `*@users.noreply.github.com` case.

The `users.noreply.github.com` domain comes from users
making use GitHub's 'email privacy' feature.
Since the noreply address doesn't accept email, a user can't link
their preferred Gravatar to this type of email address.

The upside is that this address is easily parsable into:
* (optional) Github User ID
* Github User Name

We can use either to create the associated avatar link
without actually having to do an additonal request
against Github's user search API.
  • Loading branch information
bolte-17 authored and eamodio committed Aug 20, 2019
1 parent 09ee20c commit 1657829
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/avatars.ts
Expand Up @@ -15,6 +15,14 @@ export function clearGravatarCache() {
gravatarCache.clear();
}

function getAvatarFromGithubNoreplyAddress(email: string | undefined, size: number = 16): Uri | undefined {
if (!email) return undefined;
const match = email.match(/^(?:(?<userId>\d+)\+)?(?<userName>[a-zA-Z\d-]{1,39})@users.noreply.github.com$/);
if (!match || !match.groups) return undefined;
const { userName, userId } = match.groups;
return Uri.parse(`https://avatars.githubusercontent.com/${userId ? `u/${userId}` : userName}?size=${size}`);
}

export function getGravatarUri(email: string | undefined, fallback: GravatarDefaultStyle, size: number = 16): Uri {
const hash =
email != null && email.length !== 0 ? Strings.md5(email.trim().toLowerCase(), 'hex') : missingGravatarHash;
Expand All @@ -23,7 +31,9 @@ export function getGravatarUri(email: string | undefined, fallback: GravatarDefa
let gravatar = gravatarCache.get(key);
if (gravatar !== undefined) return gravatar;

gravatar = Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`);
gravatar =
getAvatarFromGithubNoreplyAddress(email, size) ||
Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`);
gravatarCache.set(key, gravatar);

return gravatar;
Expand Down

0 comments on commit 1657829

Please sign in to comment.