From 1657829069b5d172aa42f6fd64f9042cbe441c29 Mon Sep 17 00:00:00 2001 From: Brian Bolte <3034257+bolte-17@users.noreply.github.com> Date: Tue, 6 Aug 2019 15:02:08 -0400 Subject: [PATCH] Uses GitHub Avatar for GH 'noreply' email address 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. --- src/avatars.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/avatars.ts b/src/avatars.ts index 7265e1a..53cad9c 100644 --- a/src/avatars.ts +++ b/src/avatars.ts @@ -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(/^(?:(?\d+)\+)?(?[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; @@ -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;