93 lines
2.7 KiB
Python
93 lines
2.7 KiB
Python
"""Messenger HTTP routes."""
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
|
|
|
from .auth import basic_credentials, bearer_token
|
|
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():
|
|
return store.ready()
|
|
|
|
|
|
@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 page_limit(raw: str) -> int:
|
|
digits = raw.lstrip("0")
|
|
if not raw.isdecimal() or not digits or len(digits) > 3:
|
|
raise HTTPException(400)
|
|
limit = int(digits)
|
|
if not 1 <= limit <= 100:
|
|
raise HTTPException(400)
|
|
return limit
|
|
|
|
|
|
@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"], page_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}/messages", status_code=201)
|
|
def create_message(chat_id: UUID, body: CreateMessage, actor: dict = Depends(current_user)):
|
|
return store.create_message(actor["id"], str(chat_id), body.text)
|
|
|
|
|
|
@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), page_limit(limit), cursor)
|