Adds is_site_admin-gated site administration: user management (list, deactivate/reactivate, reset password, promote/demote), room management (list all rooms including private ones, archive/unarchive, force-transfer ownership), and an audit log of every admin action. Backend: User.is_active (deactivation) and Room.is_archived (archive) are new columns; AdminAuditLog is a new table matching ARCHITECTURE.md's admin_audit_log design, written to in the same transaction as every mutating admin action. require_site_admin (dependencies.py) gates all /api/admin/* routes. get_current_user now rechecks is_active on every request, so deactivating a user kills their already-open session immediately, not just future logins. An admin can't deactivate or demote their own account (the one self-lockout guard included). Archived rooms drop out of the open-room browse list but stay readable for existing members. Frontend: new /admin route (AdminRoute guard, redirects non-admins to /rooms) with a tabbed Users / Rooms / Audit log / Settings page, plus an "Admin" link in the account menu for site admins. Bot/integration management and system settings -- both listed in the original issue -- are intentionally not here: bot management has nothing to manage until Phase 7 builds the actual bot data model, and there's no settings storage or concrete setting to configure yet. Settings has an empty placeholder tab; bot management is deferred entirely to Phase 7. Confirmed this scope cut with the repo owner before implementing. New tests/test_admin.py (14 tests, full suite now 58/58) covers every admin endpoint's permission gate, the self-action guards, deactivation's immediate effect on an already-open session, and that every mutating action produces exactly one audit log row. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
10 KiB
KeepItTalking backend (Phase 1 + 2 + 4 + 5 + 6)
FastAPI + SQLAlchemy 2.0 (async) + PostgreSQL + Redis. Implements auth, room
CRUD (open and private), room roles (owner/admin/member) and invites, a
WebSocket chat endpoint that fans out across multiple app-server instances
via Redis pub/sub, Web Push notifications for offline room members, and a
site-admin portal (user/room management + an audit log). See
../ARCHITECTURE.md for the full system design and the phased build plan.
This is an invite-only site: there is no public registration endpoint. Accounts are created by an operator on the app server — see step 4 below.
Local dev setup
1. Postgres
Any local Postgres 14+ works. The quickest option is a container:
docker run -d --name chatapp-postgres \
-e POSTGRES_USER=chatapp -e POSTGRES_PASSWORD=chatapp -e POSTGRES_DB=chatapp \
-p 5432:5432 postgres:16-alpine
Then create the test database (used by the test suite, kept separate from dev data):
docker exec chatapp-postgres psql -U chatapp -d chatapp -c "CREATE DATABASE chatapp_test;"
(Docker here is purely a local-dev convenience for standing up Postgres quickly —
the actual deployment target has no containers at all, see ARCHITECTURE.md §9.)
2. Redis
Used for cross-instance WebSocket fan-out and presence (see the section below). Required — there's no in-memory fallback.
docker run -d --name chatapp-redis -p 6379:6379 redis:7-alpine
3. Python environment
cd backend
python3 -m venv .venv
.venv/bin/pip install -e ".[dev]"
cp .env.example .env
# edit .env: set SESSION_SECRET to a long random string, e.g.
# python3 -c "import secrets; print(secrets.token_urlsafe(32))"
4. Migrations
.venv/bin/alembic upgrade head
5. Create a user
There's no public sign-up. Create accounts directly with the CLI (add
--admin to grant is_site_admin, which unlocks the admin portal at
/admin on the frontend and the /api/admin/* routes below):
.venv/bin/python -m app.cli create-user alice alice@example.com "some-password"
6. (Optional) Set up push notifications
Push works without any setup — VAPID_PUBLIC_KEY/VAPID_PRIVATE_KEY are
unset by default and push delivery is silently skipped. To enable it:
.venv/bin/python -m app.cli generate-vapid-keys
# paste the three printed lines into backend/.env
7. Run the dev server
.venv/bin/uvicorn app.main:app --reload
API docs: http://localhost:8000/docs. WebSocket chat endpoint: ws://localhost:8000/ws/chat.
To try horizontal scaling locally, run a second instance on another port
against the same Postgres + Redis (.venv/bin/uvicorn app.main:app --port 8001)
— a message sent through one instance's WebSocket is delivered to clients
connected to the other, purely via Redis.
8. Run tests
Tests run against a real Postgres database (chatapp_test by default — native
ENUM/UUID types aren't faithfully reproduced by SQLite) and a real Redis
(db 15 by default, kept separate from dev use of db 0), with each test
wrapped in a transaction that's rolled back afterward:
DATABASE_URL=postgresql+asyncpg://chatapp:chatapp@localhost:5432/chatapp_test .venv/bin/pytest
Layout
app/
main.py create_app(), session middleware, router/WS mounting
config.py environment-driven settings (pydantic-settings)
database.py async engine/session, get_db() dependency
dependencies.py get_current_user, require_room_member, require_room_role,
require_site_admin
security.py argon2 password hashing
cli.py `python -m app.cli create-user` / `generate-vapid-keys`
models/ SQLAlchemy models (users, rooms, room_memberships,
messages, room_invites, push_subscriptions,
admin_audit_log)
schemas/ Pydantic request/response models
routers/ auth, rooms, invites, push, admin, health
services/ business logic called by routers
ws/ connection_manager (local sockets), presence +
broadcaster (Redis), /ws/chat handler
alembic/ migrations
tests/ pytest + httpx/TestClient tests
Admin portal (Phase 6)
Every /api/admin/* route (app/routers/admin.py) requires
current_user.is_site_admin (checked via require_site_admin,
app/dependencies.py) and is backed by app/services/admin_service.py:
- Users: list, deactivate/reactivate (
User.is_active), reset password, promote/demoteis_site_admin. An admin can't deactivate or demote their own account (CannotActOnSelfError→ 400) — the one guard against an admin locking themselves out. Deactivation takes effect immediately, even for an already-open session:get_current_userre-checksis_activeon every request since it already loads the user row. - Rooms: list every room including private ones (unlike the
member-facing
GET /api/rooms, which is open-rooms-only), archive/ unarchive (Room.is_archived— archived rooms drop out of the open-room browse list but stay readable for existing members, matching how Mattermost archive works), and force a transfer of ownership to any existing member without needing to already be the owner (the "admin override" of the member-initiated transfer inroom_service.py, which otherwise requires exactly that). - Audit log: every mutating admin action writes one
AdminAuditLogrow (actor, action, target type/id, JSON metadata) in the same transaction as the change, listed newest-first viaGET /api/admin/audit-log.
Two items from the original phase scope are deliberately not here yet:
- Bot/integration management — nothing to manage until Phase 7 builds
the actual bot data model (
api_tokens,webhooks_incoming,event_subscriptionsperARCHITECTURE.md§4); it'll be built alongside that data model instead of as an empty panel now. - System settings — no settings storage or concrete setting exists yet. The frontend has an empty "Settings" tab as a placeholder for when one does.
Cross-instance broadcast (Phase 5)
The WebSocket layer is split into three pieces so that running one app instance and running many behave identically:
app/ws/connection_manager.py— purely local: which sockets on this process are in which room, used only to actuallysend_jsonto them.app/ws/broadcaster.py(RoomBroadcaster) — on a chat message,publish()s it to a Redis channel scoped to the room (room:{id}). Every app instance, including the publisher, runs a single backgroundlisten()task (started inapp/main.py's lifespan) pattern-subscribed toroom:*; each message it receives is handed to its own localConnectionManager.broadcast(). One instance just talks to itself through Redis, so there's no separate single-instance code path.app/ws/presence.py(Presence) — a Redis hash per room (presence:{room_id}, field = user ID, value = connection refcount) is the cross-instance answer to "is this member connected anywhere right now," which is what the Phase 4 offline-push check uses instead of the localConnectionManager. Refcounted so a user connected from two tabs (or two instances) isn't marked offline until every connection closes.
Known limitation: Presence has no heartbeat/TTL, so a hard process crash
(not a clean disconnect) leaks that connection's increment forever — same
category of simplification as the "no server-side session revocation" note
below.
Push notifications (Phase 4)
POST /api/push/subscribe (upserts by endpoint) / DELETE /api/push/subscribe
manage a user's push_subscriptions rows; GET /api/push/vapid-public-key gives
the frontend the key it needs for PushManager.subscribe(). On every chat
message, app/ws/chat.py computes room members - Presence. connected_user_ids(room_id) (who's actually connected to that room right
now, across every app instance — see Phase 5 below) and sends each offline
member a push via pywebpush, awaited inline against the same
request-scoped session rather than fired as a background task — the
broadcast to online members already happened by that point, so nothing
online-facing is delayed, and it sidesteps asyncio.create_task()s outliving
the session/event loop they were created on. An expired/invalid subscription
(pywebpush 404/410) is deleted automatically.
Room roles and invites (Phase 2)
Rooms can be open (anyone can join via POST /api/rooms/{id}/join) or
private (is_private: true at creation — joinable only via invite). Room
roles are owner > admin > member:
- member: post messages, leave the room
- admin: edit room settings, create/list/revoke invites, remove plain members
- owner: everything admin can, plus delete the room, remove admins, change member roles, and transfer ownership
Invite flow: an admin+ calls POST /api/rooms/{id}/invites with an existing
target_username; the invited user sees it via GET /api/invites/mine and
calls POST /api/invites/{id}/accept (or /decline). GET /api/rooms/mine
lists every room (open + private) the current user belongs to, alongside
their role.
Notes / scope decisions
- Invite-only site registration: no
POST /api/auth/register. Accounts are provisioned withpython -m app.cli create-user(see step 4 above). This is separate from room invites above — site accounts vs. room membership. - Room invites are by username only —
room_invites.target_emailexists in the schema (perARCHITECTURE.md) but is unused, since there's no email-delivery mechanism anywhere in the stack yet. - Sessions are signed cookies (Starlette
SessionMiddleware), not a server-side session table — seeARCHITECTURE.md's rationale (simplest way to carry auth through a WebSocket handshake). This means there's currently no way to force- revoke a session server-side; that needs a real session table later. - No CSRF token yet —
SameSite=Laxcookies plus a same-origin frontend dev proxy (see../frontend/vite.config.ts) is the accepted phase-1 mitigation. - Deleting a room explicitly deletes its messages/memberships/invites first
(
room_service.delete_room) rather than relying on DB-level cascades. admin_audit_loghas no admin UI for filtering/searching yet — it's a flat newest-first list withlimit/offsetpagination, no filter by actor/action/target. Fine at current scale; revisit if the log grows.