-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: add Gravatar support for user avatars #821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { getGravatarUrl } from '../gravatar' | ||
|
|
||
| describe('getGravatarUrl', () => { | ||
| it('generates a valid Gravatar URL with SHA-256 hash', async () => { | ||
| const url = await getGravatarUrl('test@example.com') | ||
| expect(url).toMatch( | ||
| /^https:\/\/www\.gravatar\.com\/avatar\/[a-f0-9]{64}\?d=404$/ | ||
| ) | ||
| }) | ||
|
|
||
| it('normalizes email by trimming and lowercasing', async () => { | ||
| const url1 = await getGravatarUrl('Test@Example.com') | ||
| const url2 = await getGravatarUrl(' test@example.com ') | ||
| const url3 = await getGravatarUrl('test@example.com') | ||
| expect(url1).toBe(url3) | ||
| expect(url2).toBe(url3) | ||
| }) | ||
|
|
||
| it('produces different hashes for different emails', async () => { | ||
| const url1 = await getGravatarUrl('alice@example.com') | ||
| const url2 = await getGravatarUrl('bob@example.com') | ||
| expect(url1).not.toBe(url2) | ||
| }) | ||
|
|
||
| it('generates correct hash for a known email', async () => { | ||
| // SHA-256 of "miurap400@gmail.com" can be verified independently | ||
| const url = await getGravatarUrl('miurap400@gmail.com') | ||
| expect(url).toContain('https://www.gravatar.com/avatar/') | ||
| expect(url).toContain('?d=404') | ||
|
|
||
| // Extract hash and verify it's a valid 64-char hex string | ||
| const hash = url | ||
| .replace('https://www.gravatar.com/avatar/', '') | ||
| .replace('?d=404', '') | ||
| expect(hash).toMatch(/^[a-f0-9]{64}$/) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import crypto from 'crypto' | ||
|
|
||
| /** | ||
| * Generate a Gravatar URL from an email address (server-side, synchronous). | ||
| * Uses Node.js crypto for SHA-256 hashing. | ||
| */ | ||
| export function getGravatarUrl(email: string): string { | ||
| const hash = crypto | ||
| .createHash('sha256') | ||
| .update(email.trim().toLowerCase()) | ||
| .digest('hex') | ||
| return `https://www.gravatar.com/avatar/${hash}?d=404` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /** | ||
| * Generate a Gravatar URL from an email address. | ||
| * Uses SHA-256 hashing via Web Crypto API (async, for client-side use). | ||
| * Returns the URL with `d=404` so that a 404 is returned for unregistered emails, | ||
| * allowing the UI to fall back to initials or a placeholder icon. | ||
| */ | ||
| export async function getGravatarUrl(email: string): Promise<string> { | ||
| const normalized = email.trim().toLowerCase() | ||
| const hashBuffer = await crypto.subtle.digest( | ||
| 'SHA-256', | ||
| new TextEncoder().encode(normalized) | ||
| ) | ||
| const hashHex = Array.from(new Uint8Array(hashBuffer)) | ||
| .map(b => b.toString(16).padStart(2, '0')) | ||
| .join('') | ||
| return `https://www.gravatar.com/avatar/${hashHex}?d=404` | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.