Carry forward the lab4 API and storage baseline, stage verified originals in MinIO, and atomically commit attachment, job, and outbox records before 202. Publish identifier-only jobs with broker confirms and reclaimable outbox leases. Clean abandoned and completed staging objects after database checks.
118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
"""Messenger HTTP routes."""
|
|
|
|
import re
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
|
|
|
from .auth import basic_credentials, bearer_token
|
|
from .attachments import storage_ready, upload
|
|
from .attachment_read import download, get_view
|
|
from .models import CreateChat, CreateMessage, CreateUser
|
|
from .store import store
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/health/live")
|
|
def live():
|
|
return {"status": "ok"}
|
|
|
|
|
|
@router.get("/health/ready")
|
|
def ready():
|
|
result = store.ready()
|
|
storage_ready()
|
|
return result
|
|
|
|
|
|
@router.post("/api/v1/users", status_code=201)
|
|
def register_user(body: CreateUser):
|
|
return store.register(body.username, body.display_name, body.password)
|
|
|
|
|
|
@router.post("/api/v1/auth/sessions", status_code=201)
|
|
async def create_session(request: Request, response: Response):
|
|
if await request.body():
|
|
raise HTTPException(400)
|
|
username, password = basic_credentials(request.headers.get("authorization"))
|
|
response.headers["Cache-Control"] = "no-store"
|
|
return store.create_session(username, password)
|
|
|
|
|
|
def current_user(request: Request) -> dict:
|
|
return store.authenticated(bearer_token(request.headers.get("authorization")))
|
|
|
|
|
|
def parse_limit(value: str) -> int:
|
|
try:
|
|
number = int(value) if value.isdecimal() else 0
|
|
except ValueError:
|
|
raise HTTPException(400) from None
|
|
if not 1 <= number <= 100:
|
|
raise HTTPException(400)
|
|
return number
|
|
|
|
|
|
@router.delete("/api/v1/auth/sessions/current", status_code=204)
|
|
def revoke_session(request: Request):
|
|
store.revoke_session(bearer_token(request.headers.get("authorization")))
|
|
return Response(status_code=204)
|
|
|
|
|
|
@router.get("/api/v1/users/me")
|
|
def user_me(actor: dict = Depends(current_user)):
|
|
return actor
|
|
|
|
|
|
@router.post("/api/v1/chats", status_code=201)
|
|
def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
|
return store.create_chat(actor["id"], body.title, body.member_ids)
|
|
|
|
|
|
@router.get("/api/v1/chats")
|
|
def list_chats(
|
|
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
|
):
|
|
return store.list_chats(actor["id"], parse_limit(limit), cursor)
|
|
|
|
|
|
@router.get("/api/v1/chats/{chat_id}")
|
|
def get_chat(chat_id: UUID, actor: dict = Depends(current_user)):
|
|
return store.chat(actor["id"], str(chat_id))
|
|
|
|
|
|
@router.post("/api/v1/chats/{chat_id}/attachments", status_code=202)
|
|
async def upload_attachment(chat_id: UUID, request: Request, filename: str = Query(...), actor: dict = Depends(current_user)):
|
|
return await upload(request, str(chat_id), actor["id"], filename)
|
|
|
|
|
|
@router.get("/api/v1/attachments/{attachment_id}")
|
|
def get_attachment(attachment_id: UUID, actor: dict = Depends(current_user)):
|
|
return get_view(actor["id"], attachment_id)
|
|
|
|
|
|
@router.get("/api/v1/attachments/{attachment_id}/content")
|
|
def get_attachment_content(
|
|
attachment_id: UUID, variant: str = Query("original"), actor: dict = Depends(current_user)
|
|
):
|
|
return download(actor["id"], attachment_id, variant)
|
|
|
|
|
|
@router.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
|
def create_message(chat_id: UUID, body: CreateMessage, request: Request, actor: dict = Depends(current_user)):
|
|
key = request.headers.get("Idempotency-Key")
|
|
if key is None or not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key):
|
|
raise HTTPException(400)
|
|
return store.create_message(actor["id"], str(chat_id), body.text, key, body.attachment_ids)
|
|
|
|
|
|
@router.get("/api/v1/chats/{chat_id}/messages")
|
|
def list_messages(
|
|
chat_id: UUID,
|
|
limit: str = Query("50"),
|
|
cursor: str | None = Query(None, min_length=1),
|
|
actor: dict = Depends(current_user),
|
|
):
|
|
return store.list_messages(actor["id"], str(chat_id), parse_limit(limit), cursor)
|