diff --git a/src/avatars.ts b/src/avatars.ts index 88522ee..c7ae870 100644 --- a/src/avatars.ts +++ b/src/avatars.ts @@ -1,5 +1,6 @@ import { EventEmitter, Uri } from 'vscode'; import { GravatarDefaultStyle } from './config'; +import { configuration } from './configuration'; import { ContextKeys } from './constants'; import { Container } from './container'; import { getContext } from './context'; @@ -186,19 +187,13 @@ function hasAvatarExpired(avatar: Avatar) { return Date.now() >= avatar.timestamp + retryDecay[Math.min(avatar.retries, retryDecay.length - 1)]; } -function getAvatarUriFromGravatar( - hash: string, - size: number, - defaultStyle: GravatarDefaultStyle = GravatarDefaultStyle.Robot, -): Uri { - return Uri.parse(`https://www.gravatar.com/avatar/${hash}?s=${size}&d=${defaultStyle}`); +function getAvatarUriFromGravatar(hash: string, size: number, defaultStyle?: GravatarDefaultStyle): Uri { + return Uri.parse( + `https://www.gravatar.com/avatar/${hash}?s=${size}&d=${defaultStyle ?? getDefaultGravatarStyle()}`, + ); } -export function getAvatarUriFromGravatarEmail( - email: string, - size: number, - defaultStyle: GravatarDefaultStyle = GravatarDefaultStyle.Robot, -): Uri { +export function getAvatarUriFromGravatarEmail(email: string, size: number, defaultStyle?: GravatarDefaultStyle): Uri { return getAvatarUriFromGravatar(md5(email.trim().toLowerCase(), 'hex'), size, defaultStyle); } @@ -307,3 +302,16 @@ export function resetAvatarCache(reset: 'all' | 'failed' | 'fallback') { break; } } + +let defaultGravatarsStyle: GravatarDefaultStyle | undefined = undefined; +function getDefaultGravatarStyle() { + if (defaultGravatarsStyle == null) { + defaultGravatarsStyle = configuration.get('defaultGravatarsStyle', undefined, GravatarDefaultStyle.Robot); + } + return defaultGravatarsStyle; +} + +export function setDefaultGravatarsStyle(style: GravatarDefaultStyle) { + defaultGravatarsStyle = style; + resetAvatarCache('fallback'); +} diff --git a/src/container.ts b/src/container.ts index f6d1dc4..c91e428 100644 --- a/src/container.ts +++ b/src/container.ts @@ -5,7 +5,7 @@ import { Autolinks } from './annotations/autolinks'; import { FileAnnotationController } from './annotations/fileAnnotationController'; import { LineAnnotationController } from './annotations/lineAnnotationController'; import { ActionRunners } from './api/actionRunners'; -import { resetAvatarCache } from './avatars'; +import { setDefaultGravatarsStyle } from './avatars'; import { GitCodeLensController } from './codelens/codeLensController'; import type { ToggleFileAnnotationCommandArgs } from './commands'; import type { FileAnnotationType, ModeConfig } from './configuration'; @@ -253,7 +253,7 @@ export class Container { } if (configuration.changed(e, 'defaultGravatarsStyle')) { - resetAvatarCache('fallback'); + setDefaultGravatarsStyle(configuration.get('defaultGravatarsStyle')); } if (configuration.changed(e, 'mode')) {