A remote user is name@host here, in users.username, in paths (/@alice@music.example) and in mentions, through one handle rule in shared/validation.ts that paths, MENTION_RE and Markdown all build on; Markdown undoes GFM's email autolink of a handle. Their name, email and avatar come from their home at each sign-in and by its signed events (federation_outbox, delivered by the federation job with backoff): a rename at home leaves the old handle an alias here, and a password change or a withdrawn grant ends their sessions here. Their password, email, avatar and name can't change here (assertLocalUser), nor can they sign in elsewhere from here. An admin keeps the trusted and blocked instances on /manage (federation:manage): a trusted instance's users sign in from a button on the login page, a blocked one's can't and are signed out at once, and any other's join with an invite from the register page. The /authorize page asks the user once per instance, and /settings lists the instances their account signs in to, each disconnectable; /login?next= brings a signed-out visitor back to /authorize. Emails stay unique among local accounts only, and migration 0024 adds the tables, makes that index partial and reserves authorize and federation as category slugs. remote/gerbeur-instance.ts is the only way to other instances: https and public addresses only, no redirects, no certificate fallback, unless GERBEUR_FEDERATION_ALLOW_PRIVATE lets two instances on one machine talk. 298 backend tests and 702 frontend tests pass, and deno check, deno fmt, deno lint, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked end to end with two built instances on one machine: sign-in through the other's login and consent pages, consent skipped once granted and asked again after disconnecting, a rename and a password change at home reaching the other, and blocking; screenshots of the login, consent, profile, settings and manage pages at 1280 and 390px. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
/**
|
|
* Stores and serves the users' avatars. The profile page uploads the avatar,
|
|
* and the client's `Avatar` component loads it, with the user's `updatedAt` as
|
|
* a cache-busting version.
|
|
*
|
|
* - `PUT /api/me/avatar` — signed in. Replaces the caller's avatar (multipart
|
|
* `file`, at most 5 MB), sends `user_updated` to everyone, and answers the
|
|
* caller's user. 400 for an account from another instance, whose avatar
|
|
* comes from there. The instances the caller signed in to are told.
|
|
* - `GET /api/users/:key/avatar` — public. Serves a user's avatar; 404 without
|
|
* one.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { Router } from "@oak/oak";
|
|
import { authMiddleware } from "../middleware/auth.ts";
|
|
import {
|
|
assertLocalUser,
|
|
getUserById,
|
|
resolveUserKey,
|
|
setUserAvatar,
|
|
} from "../services/user-service.ts";
|
|
import { type APIResponse } from "../../shared/api.ts";
|
|
import { APIException } from "../lib/api-exception.ts";
|
|
import type { SelfUser } from "../../shared/user.ts";
|
|
import { toSelfUser } from "../model/user.ts";
|
|
import { serveUploadedFile } from "../lib/upload.ts";
|
|
import { notifyClients } from "../services/identity-provider-service.ts";
|
|
import { AVATARS_DIR } from "../config.ts";
|
|
|
|
/**
|
|
* Mounted by `api/main.ts` behind the error, Open Graph and CORS middleware.
|
|
* An `APIException` thrown by a handler becomes the JSON error response there.
|
|
*/
|
|
const router: Router = new Router();
|
|
|
|
router.put("/api/me/avatar", authMiddleware, async (ctx) => {
|
|
const authPayload = ctx.state.user;
|
|
const contentType = ctx.request.headers.get("content-type") ?? "";
|
|
if (!contentType.includes("multipart/form-data")) {
|
|
throw new APIException(400, "Expected multipart/form-data");
|
|
}
|
|
|
|
const body = await ctx.request.body.formData();
|
|
const file = body.get("file");
|
|
|
|
if (!(file instanceof File)) {
|
|
throw new APIException(400, "Missing file field");
|
|
}
|
|
|
|
assertLocalUser(getUserById(authPayload.userId));
|
|
const user = await setUserAvatar(
|
|
authPayload.userId,
|
|
new Uint8Array(await file.arrayBuffer()),
|
|
);
|
|
await notifyClients(user.id, { type: "update" });
|
|
const responseBody: APIResponse<SelfUser> = {
|
|
success: true,
|
|
data: toSelfUser(user),
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/api/users/:key/avatar", async (ctx) => {
|
|
let user;
|
|
try {
|
|
user = resolveUserKey(ctx.params.key);
|
|
} catch {
|
|
ctx.response.status = 404;
|
|
return;
|
|
}
|
|
|
|
if (!user.avatarMime) {
|
|
ctx.response.status = 404;
|
|
return;
|
|
}
|
|
|
|
await serveUploadedFile(ctx, `${AVATARS_DIR}/${user.id}`, user.avatarMime);
|
|
});
|
|
|
|
export default router;
|