Skip to content

Refactor avatar package, support default avatar fallback#36788

Merged
wxiaoguang merged 3 commits intogo-gitea:mainfrom
wxiaoguang:fix-avatar
Mar 1, 2026
Merged

Refactor avatar package, support default avatar fallback#36788
wxiaoguang merged 3 commits intogo-gitea:mainfrom
wxiaoguang:fix-avatar

Conversation

@wxiaoguang
Copy link
Copy Markdown
Contributor

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 1, 2026
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code labels Mar 1, 2026
@wxiaoguang wxiaoguang force-pushed the fix-avatar branch 2 times, most recently from 4fbd40b to 0384531 Compare March 1, 2026 06:38
@wxiaoguang wxiaoguang requested a review from Copilot March 1, 2026 06:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #34715 where avatars return 404 errors when offline mode is enabled (because Gravatar URLs can't be fetched). The core fix is to return a randomly-generated identicon image instead of a 404 when a requested avatar file doesn't exist in storage. The PR also refactors the avatar serving infrastructure, simplifies the identicon.New() and RandomImage*() APIs by removing error returns, and consolidates HTTP cache helper functions into clearer public/private variants.

Changes:

  • When a user/repo avatar is not found in storage, instead of returning HTTP 404, the server now generates and serves a deterministic random identicon (seeded by the avatar path) with proper public cache-control headers.
  • The HandleGenericETagCache / HandleGenericETagTimeCache functions are replaced by HandleGenericETagPublicCache / HandleGenericETagPrivateCache, which now always require a lastModified *time.Time parameter, and are used consistently across file/LFS/attachment serving endpoints.
  • The RandomImageSize, RandomImage, and identicon.New() APIs are simplified to never return errors (the identicon package now clamps size values instead of returning an error for out-of-range inputs).

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
routers/web/base.go Core fix: serves a fallback random avatar instead of 404 when avatar is not found
modules/avatar/avatar.go Simplifies RandomImageSize/RandomImage to return image.Image (no error)
modules/avatar/identicon/identicon.go New() no longer returns error; clamps size; uses default dark colors if none provided
modules/avatar/avatar_test.go Removes old error-checking tests; adds benchmark
modules/httpcache/httpcache.go Removes HandleGenericETagCache; adds HandleGenericETagPublicCache and HandleGenericETagPrivateCache
modules/httpcache/httpcache_test.go Rewrites test to use table-driven approach for new private cache function
modules/assetfs/embed.go Exports GetExecutableModTime for use in avatar fallback handler
models/user/avatar.go Simplifies error handling with new infallible RandomImage API
models/repo/avatar.go Simplifies error handling; fixes error message capitalization
routers/common/serve.go Uses HandleGenericETagPrivateCache instead of removed function
routers/web/repo/download.go Uses HandleGenericETagPrivateCache for blob/LFS caching
routers/web/repo/attachment.go Uses HandleGenericETagPrivateCache for attachment caching
routers/api/v1/repo/file.go Uses HandleGenericETagPrivateCache for raw file/LFS API caching
tests/integration/user_avatar_test.go Extends test to verify avatar content, redirect behavior, and the new fallback response

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wxiaoguang wxiaoguang force-pushed the fix-avatar branch 2 times, most recently from d81e3d2 to 0d4117f Compare March 1, 2026 07:46
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 1, 2026
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 1, 2026
@wxiaoguang wxiaoguang enabled auto-merge (squash) March 1, 2026 13:19
@wxiaoguang wxiaoguang merged commit 2c624d4 into go-gitea:main Mar 1, 2026
26 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Mar 1, 2026
@wxiaoguang wxiaoguang deleted the fix-avatar branch March 1, 2026 13:33
zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 2, 2026
* giteaofficial/main: (21 commits)
  Enable docker layer caching for `dry-run` and `nightly` container builds (go-gitea#36738)
  Add admin badge to navbar avatar (go-gitea#36790)
  WorkflowDispatch api optionally return runid (go-gitea#36706)
  upgrade minimatch (go-gitea#36760)
  Add `never` option to `PUBLIC_URL_DETECTION` configuration (go-gitea#36785)
  Refactor avatar package, support default avatar fallback (go-gitea#36788)
  Mark unused&immature activitypub as "not implemented" (go-gitea#36789)
  Add “Copy Source” to markup comment menu (go-gitea#36726)
  Update Nix flake (go-gitea#36787)
  Implements OIDC RP-Initiated Logout (go-gitea#36724)
  Fix README symlink resolution in subdirectories like .github (go-gitea#36775)
  [skip ci] Updated translations via Crowdin
  Correct spelling (go-gitea#36783)
  refactor: replace legacy tw-flex utility classes with flex-text-block/inline (go-gitea#36778)
  Fix `no-content` message not rendering after comment edit (go-gitea#36733)
  Fix typos and grammar in English locale (go-gitea#36751)
  Move Fomantic dropdown CSS to custom module (go-gitea#36530)
  Use "Enable Gravatar" but not "Disable" (go-gitea#36771)
  feat: add branch_count to repository API (go-gitea#35351) (go-gitea#36743)
  Deprecate RenderWithErr (go-gitea#36769)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avatars broken (404) after turning on offline mode

5 participants