Browse Source

Fixes lint issues w/ new avatar code & fixes nits

main
Eric Amodio 5 years ago
parent
commit
56f73fbd03
4 changed files with 25 additions and 21 deletions
  1. +19
    -15
      src/avatars.ts
  2. +2
    -2
      src/container.ts
  3. +2
    -2
      src/git/models/commit.ts
  4. +2
    -2
      src/git/models/contributor.ts

+ 19
- 15
src/avatars.ts View File

@ -6,37 +6,41 @@ import { Strings } from './system';
import { ContactPresenceStatus } from './vsls/vsls';
import { Container } from './container';
const gravatarCache = new Map<string, Uri>();
const avatarCache = new Map<string, Uri>();
const missingGravatarHash = '00000000000000000000000000000000';
const presenceCache = new Map<ContactPresenceStatus, string>();
export function clearGravatarCache() {
gravatarCache.clear();
const gitHubNoReplyAddressRegex = /^(?:(?<userId>\d+)\+)?(?<userName>[a-zA-Z\d-]{1,39})@users.noreply.github.com$/;
export function clearAvatarCache() {
avatarCache.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;
function getAvatarUriFromGitHubNoReplyAddress(email: string | undefined, size: number = 16): Uri | undefined {
if (email == null || email.length === 0) return undefined;
const match = gitHubNoReplyAddressRegex.exec(email);
if (match == null) return undefined;
const [, userId, userName] = match;
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 {
export function getAvatarUri(email: string | undefined, fallback: GravatarDefaultStyle, size: number = 16): Uri {
const hash =
email != null && email.length !== 0 ? Strings.md5(email.trim().toLowerCase(), 'hex') : missingGravatarHash;
const key = `${hash}:${size}`;
let gravatar = gravatarCache.get(key);
if (gravatar !== undefined) return gravatar;
let avatar = avatarCache.get(key);
if (avatar !== undefined) return avatar;
gravatar =
getAvatarFromGithubNoreplyAddress(email, size) ||
avatar =
getAvatarUriFromGitHubNoReplyAddress(email, size) ||
Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`);
gravatarCache.set(key, gravatar);
avatarCache.set(key, avatar);
return gravatar;
return avatar;
}
export function getPresenceDataUri(status: ContactPresenceStatus) {

+ 2
- 2
src/container.ts View File

@ -7,7 +7,7 @@ import { Commands, ToggleFileBlameCommandArgs } from './commands';
import { AnnotationsToggleMode, Config, configuration, ConfigurationWillChangeEvent } from './configuration';
import { GitFileSystemProvider } from './git/fsProvider';
import { GitService } from './git/gitService';
import { clearGravatarCache } from './avatars';
import { clearAvatarCache } from './avatars';
import { LineHoverController } from './hovers/lineHoverController';
import { Keyboard } from './keyboard';
import { Logger, TraceLevel } from './logger';
@ -135,7 +135,7 @@ export class Container {
}
if (configuration.changed(e.change, configuration.name('defaultGravatarsStyle').value)) {
clearGravatarCache();
clearAvatarCache();
}
if (

+ 2
- 2
src/git/models/commit.ts View File

@ -6,7 +6,7 @@ import { Dates, memoize } from '../../system';
import { CommitFormatter } from '../formatters/formatters';
import { Git } from '../git';
import { GitUri } from '../gitUri';
import { getGravatarUri } from '../../avatars';
import { getAvatarUri } from '../../avatars';
export interface GitAuthor {
name: string;
@ -213,7 +213,7 @@ export abstract class GitCommit {
}
getGravatarUri(fallback: GravatarDefaultStyle, size: number = 16): Uri {
return getGravatarUri(this.email, fallback, size);
return getAvatarUri(this.email, fallback, size);
}
@memoize()

+ 2
- 2
src/git/models/contributor.ts View File

@ -1,7 +1,7 @@
'use strict';
import { Uri } from 'vscode';
import { GravatarDefaultStyle } from '../../configuration';
import { getGravatarUri } from '../../avatars';
import { getAvatarUri } from '../../avatars';
export class GitContributor {
static is(contributor: any): contributor is GitContributor {
@ -16,6 +16,6 @@ export class GitContributor {
) {}
getGravatarUri(fallback: GravatarDefaultStyle, size: number = 16): Uri {
return getGravatarUri(this.email, fallback, size);
return getAvatarUri(this.email, fallback, size);
}
}

Loading…
Cancel
Save