Sub-Task #122
Updated by Bricklou 20 days ago
Technical specification of the avatar API and of how an uploaded image becomes an avatar. The storage backends are in #307.
API:
* `PUT /api/v1/me/avatar` takes the image as the raw body, 2 MB at most, and returns the current user. `DELETE /api/v1/me/avatar` removes the avatar and succeeds when there is none.
* `GET /api/v1/users/{id}/avatar` returns the image to any signed-in user, with an `ETag` and a cache header. A request that already has the current `ETag` gets 304. A user without avatar gets 404.
* The user response and the user summaries carry `avatar_url`, versioned by the date of the avatar, so a cache can keep the image for good. It is null without avatar.
* An account that signs in through a login provider is refused with 403.
* Errors are Problem Details documented in the OpenAPI schema: 401 without a session, 413 for a file or an image that is too large, 422 for a file that is not a supported image.
Image:
* The image is decoded with limits on its size, cropped to its centre square, scaled to 256 pixels and stored as WebP. Metadata of the original is not kept. This is done by a helper shared with every feature that takes an image.
Data:
* The account keeps the key of its avatar and the date it was set. The new file is stored first, then the account points to it, then the previous file is deleted. If the account cannot be updated, the new file is deleted.
* Each change is written to the change log.
Acceptance criteria:
* [x] An image becomes the avatar, and is served as WebP with an `ETag`.
* [x] A file that is not an image, or is too large, is refused with the right status.
* [x] Replacing or removing the avatar deletes the previous file.
* [x] A login provider account cannot change its avatar.
* [x] Each change is in the change log.
* [x] Every case is covered by a test, with a real database.