|
1 | 1 | 'use strict'; |
2 | 2 | import { Dates, Strings } from '../../system'; |
3 | 3 | import { Uri } from 'vscode'; |
4 | | -import { configuration, DateStyle } from '../../configuration'; |
| 4 | +import { configuration, DateStyle, GravatarDefault } from '../../configuration'; |
5 | 5 | import { GlyphChars } from '../../constants'; |
6 | 6 | import { Container } from '../../container'; |
7 | 7 | import { Git } from '../git'; |
8 | 8 | import { GitUri } from '../gitUri'; |
9 | 9 | import * as path from 'path'; |
10 | 10 |
|
| 11 | +const gravatarCache: Map<string, Uri> = new Map(); |
| 12 | +export function clearGravatarCache() { |
| 13 | + gravatarCache.clear(); |
| 14 | +} |
| 15 | + |
11 | 16 | export interface GitAuthor { |
12 | 17 | name: string; |
13 | 18 | lineCount: number; |
@@ -58,6 +63,7 @@ export abstract class GitCommit { |
58 | 63 | public readonly repoPath: string, |
59 | 64 | public readonly sha: string, |
60 | 65 | public readonly author: string, |
| 66 | + public readonly email: string | undefined, |
61 | 67 | public readonly date: Date, |
62 | 68 | public readonly message: string, |
63 | 69 | fileName: string, |
@@ -165,6 +171,27 @@ export abstract class GitCommit { |
165 | 171 | return GitUri.getFormattedPath(this.fileName, separator); |
166 | 172 | } |
167 | 173 |
|
| 174 | + getGravatarUri(fallback: GravatarDefault): Uri { |
| 175 | + const key = this.email |
| 176 | + ? `${ this.email.trim().toLowerCase() }` |
| 177 | + : ''; |
| 178 | + |
| 179 | + let gravatar = gravatarCache.get(key); |
| 180 | + if (gravatar !== undefined) return gravatar; |
| 181 | + |
| 182 | + gravatar = Uri.parse(`https://www.gravatar.com/avatar/${this.email ? Strings.md5(this.email, 'hex') : '00000000000000000000000000000000'}.jpg?s=22&d=${fallback}`); |
| 183 | + |
| 184 | + // HACK: Monkey patch Uri.toString to avoid the unwanted query string encoding |
| 185 | + const originalToStringFn = gravatar.toString; |
| 186 | + gravatar.toString = function(skipEncoding?: boolean | undefined) { |
| 187 | + return originalToStringFn.call(gravatar, true); |
| 188 | + }; |
| 189 | + |
| 190 | + gravatarCache.set(key, gravatar); |
| 191 | + |
| 192 | + return gravatar; |
| 193 | + } |
| 194 | + |
168 | 195 | async resolvePreviousFileSha(): Promise<void> { |
169 | 196 | if (this._resolvedPreviousFileSha !== undefined) return; |
170 | 197 |
|
|
0 commit comments