feat(avatar): add opt-in monogram placeholder avatars #71
No reviewers
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
codefloe/forgejo-codefloe!71
Loading…
Reference in a new issue
No description provided.
Delete branch "t3code/improve-avatar-placeholders"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Offer the initial of a name in white on a gradient tile as an alternative placeholder avatar, behind a new
[picture] PLACEHOLDER_AVATARsetting.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
#cccc99and 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, whichgolang.org/x/imagealready 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,
AvatarLinkWithSizenever 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=Nsatisfies 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_URLis 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.iniinstead, 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.goandmodules/avatar/identicon/are not touched at all: the newPlaceholderImagesits alongside upstream'sRandomImageand calls it for the identicon style rather than replacing it.The complete diff to pre-existing code:
models/avatars/avatar.goidenticoncustom/conf/app.example.inimodels/user/avatar.goRandomImage→PlaceholderImage, and the error stringmodels/repo/avatar.gorouters/web/web.gomodules/setting/setting.goloadPagesFromREADME.mdEverything 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
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-avatarsdoctor 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 = falseand 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.goonly 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 routineforgejo doctor checkwill not touch anything.Behaviour change
None until
PLACEHOLDER_AVATARis set tomonogram.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.pngandavatar_actions.pngare 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 testovermodules/avatar/...,modules/setting,models/avatars,models/user,models/repo,services/user,services/doctor.TestPlaceholderAvatarRoute*andTestDoctorRegenerate*(new), plus the existingTestUserAvatar*,TestRepoAvatar*,TestAvatarAnchorDestinationandTestPrecompute*.make lint-goreports 0 issues and leaves the.deadcode-outbaseline unchanged.7bdefd37f695dbee51fffeat(avatar): replace identicon placeholders with monogramsto feat(avatar): add opt-in monogram placeholder avatars95dbee51ffb11294208eb11294208e462456b045