codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

feat(avatar): add opt-in monogram placeholder avatars #71

Merged
pat-s merged 1 commit from t3code/improve-avatar-placeholders into v16.0/forgejo-codefloe 2026-08-04 09:52:43 +00:00
Owner

Offer the initial of a name in white on a gradient tile as an alternative placeholder avatar, behind a new [picture] PLACEHOLDER_AVATAR setting.

The setting defaults to identicon, so nothing changes until an instance opts in.

Motivation

The identicon draws one foreground colour picked from 125 web-safe values.
That set includes muddy tones like #cccc99 and pale ones that nearly vanish against the white background it pairs them with.
At the 20px size used in comment threads and commit lists the mirrored 3×3 pattern reads as noise, and it carries no relation to the account it stands for.

The monogram style

Both gradient stops are built in OKLCH at a fixed lightness and chroma, so every hue carries the same visual weight instead of some landing acid-bright and others muddy.
The hue comes from the first two bytes of sha256(seed), the second stop sits 28° away, and the ramp is interpolated in Oklab down the diagonal.

The lightness constants are not a taste call: a test sweeps all 360 hues and asserts at least 4.5:1 contrast against white for both stops.
The worst case is 4.63:1, at hue 150.
A second test walks every pixel of 64 rendered tiles, covering the interpolated middle that the palette sweep alone does not reach.
Raise either lightness constant and those tests fail before anything ships.

The glyph comes from gofont/gobold, which golang.org/x/image already brings in, so no font asset is added to the binary.
Its Latin, Greek and Cyrillic coverage spans most account names.
A name the font cannot set, a CJK one for instance, renders the bare gradient rather than a row of missing-glyph boxes.

Gravatar and Libravatar

This is the part that decides whether the feature is visible at all.

On an instance with Gravatar or federated avatars enabled, AvatarLinkWithSize never reaches the local placeholder generator.
It hands the browser a provider URL carrying d=identicon, and the provider renders its own tile for every address it does not recognise.
A local placeholder, of either style, is simply never requested.

So when the monogram style is selected, the provider is given a link to our own renderer as its default image instead.
People who registered a picture with the provider keep it, and only the rest fall through to a monogram.

That link has to satisfy Gravatar's constraints on a custom default image: publicly reachable over HTTP or HTTPS on port 80 or 443, ending in a recognisable image extension, and carrying no querystring.
/avatars/<hash>?size=N satisfies none of the last two, and the provider fetches the URL itself rather than following a redirect to it, so a new route renders the tile directly at /avatar-placeholder/<hash>.png.

No attempt is made to guess whether ROOT_URL is actually reachable.
Silently ignoring a setting that was deliberately turned on is the failure mode this whole change exists to remove.
The requirement is documented in app.example.ini instead, along with the advice to disable the provider on an instance that is not public.

Keeping out of upstream's way

The change is built as new files wherever it could be, so that merging upstream stays cheap.
modules/avatar/avatar.go and modules/avatar/identicon/ are not touched at all: the new PlaceholderImage sits alongside upstream's RandomImage and calls it for the identicon style rather than replacing it.

The complete diff to pre-existing code:

File Lines What
models/avatars/avatar.go 12 thread the default-image URL to the provider, falling back to the previous hardcoded identicon
custom/conf/app.example.ini 11 document the setting
models/user/avatar.go 2 RandomImage → PlaceholderImage, and the error string
models/repo/avatar.go 2 the same
routers/web/web.go 2 register the render route
modules/setting/setting.go 1 register the loader, next to the existing loadPagesFrom
README.md 1 custom-changes row

Everything else is new: modules/avatar/monogram/, modules/avatar/placeholder.go, modules/setting/codefloe_avatar.go, models/avatars/codefloe_placeholder.go, routers/web/user/codefloe_placeholder_avatar.go, services/doctor/regenerate_avatars.go, and their tests.

The setting

[picture]
; identicon (default) or monogram
PLACEHOLDER_AVATAR = monogram

The value is matched case-insensitively and trimmed, and anything unrecognised logs a warning naming the valid values before falling back to the identicon.
A config-management tool that capitalises a value differently should not silently disable the feature, and a typo should not leave an operator with no explanation.

Regeneration

Locally stored placeholders are rendered once and cached forever, so flipping the setting would otherwise only affect accounts created afterwards.
The regenerate-avatars doctor check re-renders existing ones in whichever style is currently configured, which makes the setting switchable in both directions.

Its report names the style it used, so a run that picked up no configuration is distinguishable from one that did.

It selects users by use_custom_avatar = false and repositories by an avatar path equal to the bare repository ID, which is what separates a generated repository avatar from an uploaded one.
Uploaded images are never touched.
It overwrites in place rather than deleting first, so an interrupted run leaves everyone with an avatar, just a mix of old and new until it is run again.

It also rewrites the downsized copies under resized/<size>/.
That part is easy to miss and has its own assertion: the web handler in routers/web/resizing.go only recomputes a resized avatar when it is missing, never when it is stale, so leaving the cache alone would keep serving the old tile at every small size indefinitely.

The check is IsDefault: false, so a routine forgejo doctor check will not touch anything.

# report how many avatars would be regenerated, writing nothing
forgejo doctor check --run regenerate-avatars
# regenerate them
forgejo doctor check --run regenerate-avatars --fix

Behaviour change

None until PLACEHOLDER_AVATAR is set to monogram.

After switching, locally stored placeholders change hue as well as shape once the doctor check runs: the seed is unchanged but the seed-to-colour mapping differs between the two styles.
On a Gravatar or Libravatar instance, anyone without a picture registered with the provider changes from the provider's tile to a monogram; anyone with one is unaffected.
Uploaded avatars, avatar_default.png and avatar_actions.png are unaffected either way.

Accounts whose names share a first letter look more alike under the monogram style than identicons made them, since only the gradient distinguishes them.
That is the deliberate trade for a placeholder that is legible at 20px and actually tied to the name.

The new route is public and unauthenticated, because the provider has to fetch it.
It reveals nothing new: the email hash already appears in the page next to the account it belongs to, so resolving one to that account's initial tells a reader nothing the same page did not.

Testing

  • go test over modules/avatar/..., modules/setting, models/avatars, models/user, models/repo, services/user, services/doctor.
  • sqlite integration: TestPlaceholderAvatarRoute* and TestDoctorRegenerate* (new), plus the existing TestUserAvatar*, TestRepoAvatar*, TestAvatarAnchorDestination and TestPrecompute*.
  • make lint-go reports 0 issues and leaves the .deadcode-out baseline unchanged.
Offer the initial of a name in white on a gradient tile as an alternative placeholder avatar, behind a new `[picture] PLACEHOLDER_AVATAR` setting. **The setting defaults to `identicon`, so nothing changes until an instance opts in.** ## Motivation The identicon draws one foreground colour picked from 125 web-safe values. That set includes muddy tones like `#cccc99` and pale ones that nearly vanish against the white background it pairs them with. At the 20px size used in comment threads and commit lists the mirrored 3×3 pattern reads as noise, and it carries no relation to the account it stands for. ## The monogram style Both gradient stops are built in OKLCH at a fixed lightness and chroma, so every hue carries the same visual weight instead of some landing acid-bright and others muddy. The hue comes from the first two bytes of `sha256(seed)`, the second stop sits 28° away, and the ramp is interpolated in Oklab down the diagonal. The lightness constants are not a taste call: a test sweeps all 360 hues and asserts at least 4.5:1 contrast against white for both stops. The worst case is 4.63:1, at hue 150. A second test walks every pixel of 64 rendered tiles, covering the interpolated middle that the palette sweep alone does not reach. Raise either lightness constant and those tests fail before anything ships. The glyph comes from `gofont/gobold`, which `golang.org/x/image` already brings in, so no font asset is added to the binary. Its Latin, Greek and Cyrillic coverage spans most account names. A name the font cannot set, a CJK one for instance, renders the bare gradient rather than a row of missing-glyph boxes. ## Gravatar and Libravatar This is the part that decides whether the feature is visible at all. On an instance with Gravatar or federated avatars enabled, `AvatarLinkWithSize` never reaches the local placeholder generator. It hands the browser a provider URL carrying `d=identicon`, and the provider renders its own tile for every address it does not recognise. A local placeholder, of either style, is simply never requested. So when the monogram style is selected, the provider is given a link to our own renderer as its default image instead. People who registered a picture with the provider keep it, and only the rest fall through to a monogram. That link has to satisfy Gravatar's constraints on a custom default image: publicly reachable over HTTP or HTTPS on port 80 or 443, ending in a recognisable image extension, and carrying no querystring. `/avatars/<hash>?size=N` satisfies none of the last two, and the provider fetches the URL itself rather than following a redirect to it, so a new route renders the tile directly at `/avatar-placeholder/<hash>.png`. No attempt is made to guess whether `ROOT_URL` is actually reachable. Silently ignoring a setting that was deliberately turned on is the failure mode this whole change exists to remove. The requirement is documented in `app.example.ini` instead, along with the advice to disable the provider on an instance that is not public. ## Keeping out of upstream's way The change is built as new files wherever it could be, so that merging upstream stays cheap. `modules/avatar/avatar.go` and `modules/avatar/identicon/` are **not** touched at all: the new `PlaceholderImage` sits alongside upstream's `RandomImage` and calls it for the identicon style rather than replacing it. The complete diff to pre-existing code: | File | Lines | What | | --- | --- | --- | | `models/avatars/avatar.go` | 12 | thread the default-image URL to the provider, falling back to the previous hardcoded `identicon` | | `custom/conf/app.example.ini` | 11 | document the setting | | `models/user/avatar.go` | 2 | `RandomImage` → `PlaceholderImage`, and the error string | | `models/repo/avatar.go` | 2 | the same | | `routers/web/web.go` | 2 | register the render route | | `modules/setting/setting.go` | 1 | register the loader, next to the existing `loadPagesFrom` | | `README.md` | 1 | custom-changes row | Everything else is new: `modules/avatar/monogram/`, `modules/avatar/placeholder.go`, `modules/setting/codefloe_avatar.go`, `models/avatars/codefloe_placeholder.go`, `routers/web/user/codefloe_placeholder_avatar.go`, `services/doctor/regenerate_avatars.go`, and their tests. ## The setting ```ini [picture] ; identicon (default) or monogram PLACEHOLDER_AVATAR = monogram ``` The value is matched case-insensitively and trimmed, and anything unrecognised logs a warning naming the valid values before falling back to the identicon. A config-management tool that capitalises a value differently should not silently disable the feature, and a typo should not leave an operator with no explanation. ## Regeneration Locally stored placeholders are rendered once and cached forever, so flipping the setting would otherwise only affect accounts created afterwards. The `regenerate-avatars` doctor check re-renders existing ones in whichever style is currently configured, which makes the setting switchable in both directions. Its report names the style it used, so a run that picked up no configuration is distinguishable from one that did. It selects users by `use_custom_avatar = false` and repositories by an avatar path equal to the bare repository ID, which is what separates a generated repository avatar from an uploaded one. Uploaded images are never touched. It overwrites in place rather than deleting first, so an interrupted run leaves everyone with an avatar, just a mix of old and new until it is run again. It also rewrites the downsized copies under `resized/<size>/`. That part is easy to miss and has its own assertion: the web handler in `routers/web/resizing.go` only recomputes a resized avatar when it is **missing**, never when it is stale, so leaving the cache alone would keep serving the old tile at every small size indefinitely. The check is `IsDefault: false`, so a routine `forgejo doctor check` will not touch anything. ``` # report how many avatars would be regenerated, writing nothing forgejo doctor check --run regenerate-avatars # regenerate them forgejo doctor check --run regenerate-avatars --fix ``` ## Behaviour change None until `PLACEHOLDER_AVATAR` is set to `monogram`. After switching, locally stored placeholders change hue as well as shape once the doctor check runs: the seed is unchanged but the seed-to-colour mapping differs between the two styles. On a Gravatar or Libravatar instance, anyone without a picture registered with the provider changes from the provider's tile to a monogram; anyone with one is unaffected. Uploaded avatars, `avatar_default.png` and `avatar_actions.png` are unaffected either way. Accounts whose names share a first letter look more alike under the monogram style than identicons made them, since only the gradient distinguishes them. That is the deliberate trade for a placeholder that is legible at 20px and actually tied to the name. The new route is public and unauthenticated, because the provider has to fetch it. It reveals nothing new: the email hash already appears in the page next to the account it belongs to, so resolving one to that account's initial tells a reader nothing the same page did not. ## Testing - `go test` over `modules/avatar/...`, `modules/setting`, `models/avatars`, `models/user`, `models/repo`, `services/user`, `services/doctor`. - sqlite integration: `TestPlaceholderAvatarRoute*` and `TestDoctorRegenerate*` (new), plus the existing `TestUserAvatar*`, `TestRepoAvatar*`, `TestAvatarAnchorDestination` and `TestPrecompute*`. - `make lint-go` reports 0 issues and leaves the `.deadcode-out` baseline unchanged.
pat-s force-pushed t3code/improve-avatar-placeholders from 7bdefd37f6
Some checks failed
testing / frontend-checks (pull_request) Successful in 1m22s
testing / backend-checks (pull_request) Successful in 7m50s
testing / test-unit (pull_request) Successful in 8m50s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m33s
testing / test-remote-cacher (redis) (pull_request) Successful in 2m35s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m55s
testing / test-e2e (pull_request) Successful in 33m19s
testing / test-mysql (pull_request) Successful in 34m10s
testing / test-sqlite (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
to 95dbee51ff
All checks were successful
testing / backend-checks (pull_request) Successful in 5m48s
testing / frontend-checks (pull_request) Successful in 1m17s
testing / test-unit (pull_request) Successful in 6m0s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m54s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m30s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m33s
testing / test-e2e (pull_request) Successful in 22m17s
testing / test-mysql (pull_request) Successful in 26m0s
testing / test-sqlite (pull_request) Successful in 44m16s
testing / test-pgsql (pull_request) Successful in 46m19s
2026-08-04 07:14:54 +00:00
Compare
pat-s changed title from feat(avatar): replace identicon placeholders with monograms to feat(avatar): add opt-in monogram placeholder avatars 2026-08-04 07:15:27 +00:00
pat-s force-pushed t3code/improve-avatar-placeholders from 95dbee51ff
All checks were successful
testing / backend-checks (pull_request) Successful in 5m48s
testing / frontend-checks (pull_request) Successful in 1m17s
testing / test-unit (pull_request) Successful in 6m0s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m54s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m30s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m33s
testing / test-e2e (pull_request) Successful in 22m17s
testing / test-mysql (pull_request) Successful in 26m0s
testing / test-sqlite (pull_request) Successful in 44m16s
testing / test-pgsql (pull_request) Successful in 46m19s
to b11294208e
Some checks failed
testing / backend-checks (pull_request) Has been cancelled
testing / frontend-checks (pull_request) Has been cancelled
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
2026-08-04 08:34:45 +00:00
Compare
pat-s force-pushed t3code/improve-avatar-placeholders from b11294208e
Some checks failed
testing / backend-checks (pull_request) Has been cancelled
testing / frontend-checks (pull_request) Has been cancelled
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
to 462456b045
All checks were successful
testing / backend-checks (pull_request) Successful in 8m54s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m24s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m3s
testing / test-sqlite (pull_request) Successful in 43m25s
testing / frontend-checks (pull_request) Successful in 2m15s
testing / test-unit (pull_request) Successful in 8m3s
testing / test-remote-cacher (garnet) (pull_request) Successful in 1m19s
testing / test-e2e (pull_request) Successful in 33m30s
testing / test-mysql (pull_request) Successful in 35m54s
testing / test-pgsql (pull_request) Successful in 54m5s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
2026-08-04 08:46:36 +00:00
Compare
pat-s merged commit 68318b3b2c into v16.0/forgejo-codefloe 2026-08-04 09:52:43 +00:00
pat-s deleted branch t3code/improve-avatar-placeholders 2026-08-04 09:52:43 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!71
No description provided.