Skip to content

feat: custom user avatar upload and delete - #13949

Merged
Meldiron merged 15 commits into
appwrite:mainfrom
imtia33:feat/custom-avatar
Sep 29, 2026
Merged

Meldiron merged 15 commits into
appwrite:mainfrom
imtia33:feat/custom-avatar

Conversation

@imtia33

@imtia33 imtia33 commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds custom user avatar upload and delete to the Avatars service.

New endpoints:

  • PUT /v1/avatars/photo — Upload a custom profile photo (PNG, JPG, GIF, WebP). Supports chunked uploads for files over 5 MB via Content-Range. Concurrent uploads for the same user are serialised with a per-user distributed lock. The new photo is only activated after the file is fully assembled — no downtime between replacements.
  • DELETE /v1/avatars/photo — Remove the custom photo and revert to the existing fallback chain (OAuth2 identity photo → Gravatar → Libravatar → Initials → static placeholder). Idempotent.

GET /v1/avatars/photo updated: Custom uploaded photo is now tried first before any existing provider.

Storage: A new avatars collection tracks uploads per-project. Storage is metered via a StatsResources gauge (avatars.storage) — the same pattern as files.storage. Avatar files are cleaned up when a user is deleted.

Migration: avatars collection and users.avatarPath attribute are created inside the existing V25 migration switch (same pattern as pushLedger) so existing projects are covered without a separate migration version.

Test Plan

E2E tests added to AvatarsCustomClientTest and AvatarsCustomServerTest:

  • Single-shot upload (PNG, WebP) — asserts chunksTotal, chunksUploaded, mimeType, sizeActual
  • Chunked upload across the 5 MB boundary — asserts completion and idempotent re-send of final chunk
  • Photo replacement — old file removed, new one served immediately
  • GET /v1/avatars/photo after upload — custom photo wins the provider chain
  • DELETE + verify fallback chain resumes + idempotent second delete
  • Failure cases: no file (400), unsupported extension (400), oversized (400), invalid Content-Range (400)
  • API-key requests to PUT/DELETE — assert 401

Run with:

docker compose exec appwrite test tests/e2e/Services/Avatars/

All 39 tests pass, 773 assertions.

Related PRs and Issues

  • N/A

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?
    • SDK methods (avatars.updatePhoto, avatars.deletePhoto) are generated automatically from the Method labels on the new actions. No manual spec changes needed — app/config/sdks.php is untouched.

@ghost

ghost commented Sep 28, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The new retry caps work correctly and clean up as intended, and I found no problems in the new changes.

The PR adds custom avatar upload (PUT /v1/avatars/photo) and delete (DELETE /v1/avatars/photo). The photo ID and size live on the user document, and the custom photo is tried first in the GET /v1/avatars/photo provider chain. It also migrates the new user attributes, removes photo folders on user deletion, meters avatar storage in the stats workers, and adds E2E tests.

Latest changes: Update and Delete now stop retrying after 5 optimistic-concurrency conflicts and throw DOCUMENT_UPDATE_CONFLICT. Update also removes the newly uploaded file before giving up.

Verdict New comments Fixed Still open
💬 Commented 0 0 0

Note

@imtia33 does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

📂 Walkthrough · 16
File Change
app/config/collections/common.php Add photoId and photoSize attributes to users.
src/Appwrite/Migration/Version/V25.php Create the new user attributes for existing projects.
app/config/events.php Add the avatar photo update event.
app/config/roles.php Grant avatar write access.
app/config/scopes/project.php Add the avatar write scope.
app/init/constants.php Define the photo storage folder constant.
src/Appwrite/AvatarPhotos/Providers/Custom.php Provider that serves the user's uploaded photo.
src/Appwrite/Platform/Modules/Avatars/Http/Photo/Get.php Try the custom photo before other providers.
src/Appwrite/Platform/Modules/Avatars/Http/Photo/Update.php Validate and store an uploaded photo. Switch photoId with a timestamp-guarded update that retries up to 5 times on conflict, then cleans up and errors. Remove the previous file.
src/Appwrite/Platform/Modules/Avatars/Http/Photo/Delete.php Delete the photo file, then clear the attributes while the photo is unchanged. Retries are capped at 5, after which it returns a conflict error.
src/Appwrite/Platform/Modules/Avatars/Services/Http.php Register the new Update and Delete actions.
src/Appwrite/Platform/Workers/Deletes.php Remove the photo folder when a user is deleted.
src/Appwrite/Platform/Workers/StatsResources.php Measure avatar storage by summing users.photoSize.
src/Appwrite/Platform/Workers/StatsUsage.php Support avatar storage metrics.
tests/e2e/Services/Avatars/AvatarsCustomClientTest.php E2E coverage for upload, replace, delete, fallback and validation.
tests/e2e/Services/Avatars/AvatarsCustomServerTest.php Assert API-key requests to the new routes are rejected.

Reviewed the commits since 8fe5de2 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Tier B · 1 blocking finding to address. Summary

@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[High risk] Adds user photo upload and delete with new database schema and storage.

The PR is not yet safe to merge because deletion can leave the user pointing to a removed photo after repeated update conflicts.

Fix All in Claude CodeFindings

  1. P1 Deletion leaves a stale photo ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Avatars/Http/Photo/Delete.php:115-116
If five concurrent user updates conflict with a photo deletion, this new limit returns an error after the photo file has been removed but before `photoId` is cleared. GET then finds the file missing and serves a fallback instead of the user's photo until a retry succeeds.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR adds single-request custom avatar uploads, prioritizes uploaded photos on GET, and adds deletion, user-cleanup, and storage-usage handling.

  • Upload validation and tests now cover PNG, JPEG, and WebP delivery; ranged uploads are rejected.
  • The new deletion retry limit can leave a missing file referenced by the user record.

Reviews (10) · Last reviewed commit: "fix: cap photo update retries on conflic..."

- Store the photo ID in users.avatar instead of the full path
- Rename avatars collection to photos and drop its path attribute
- Rename Avatar response model to Photo
- Lock uploads per upload ID instead of per user; drop the lock from delete
- Generate the chunked test payload instead of shipping a 17 MB fixture

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Tier B · 1 blocking finding to address. Summary

- Lock per user around chunks, activation and deletion via the lock pool
- Validate Content-Range shape and actual chunk size against the range
- Accept only PNG, JPEG, GIF and WebP by content
- Delete storage before records so failed deletes stay retriable
- Retry activation when a completed upload was never made active
- Audit photo deletion against the session user
- Store photos under _photos/ so bucket deletion can't remove them
- Test the served image instead of upload metadata

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Meldiron and others added 2 commits September 29, 2026 13:20
Replace the users.avatar pointer with a users.photos subquery attribute
that returns the newest completed photo, cached with the user document.
Photo rows are the only state; Update and Delete purge the user cache
after changing them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Rename users.photos to users.photo and return the live photo with
findOne instead of a one-item list.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Drop chunked uploads: a photo is one request of up to one upload chunk
(5MB), validated by content before it is stored. Every photos row is now
a finished photo, so the chunk counters, upload metadata and the per-user
lock go away; each upload removes only older photos, which lets
concurrent uploads settle on the newest.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Replace the photos collection and its subquery with photoId and
photoSize attributes on users, drop the stored mime type, and accept
PNG and JPEG only. Files live in a per-user folder that user deletion
removes whole, which also clears anything a racing request left behind.
Update now returns the user.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Tier B · 1 blocking finding to address. Summary

Meldiron and others added 2 commits September 29, 2026 13:56
Move photo file deletion out of the provider and into the endpoints and
the Deletes worker.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Replace the provider's getPath() with APP_STORAGE_PHOTOS and build the
path where it is needed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@Meldiron Meldiron left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

- Serialise photo updates and deletes per user, so the replaced photo is
  always the one whose file is removed
- Return MODEL_ACCOUNT instead of MODEL_USER, which exposed the password hash
- Accept WebP alongside PNG and JPEG
- Check that JPEG and WebP uploads are served

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Meldiron and others added 2 commits September 29, 2026 14:34
Only remove the uploaded file when the user was not switched to it yet;
a failure after the switch is logged and the request succeeds.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The lock added more code than the race it prevents is worth. A file left
behind by concurrent requests is removed with the user's photo folder
when the user is deleted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Tier B · 1 blocking finding to address. Summary

Meldiron and others added 2 commits September 29, 2026 17:30
Swoole drops a multipart request without a boundary before it reaches the app, so the test got a bare 400 instead of the missing-file error.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Photo updates and deletes are guarded by the user's update timestamp, so a concurrent upload is never cleared by a delete and a replaced photo is never left behind.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants