feat: add message history and concurrent writes
This commit is contained in:
+18
-1
@@ -4,7 +4,7 @@ from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
||||
|
||||
from .models import CreateChat, CreateUser
|
||||
from .models import CreateChat, CreateMessage, CreateUser
|
||||
from .store import store
|
||||
|
||||
router = APIRouter()
|
||||
@@ -51,3 +51,20 @@ def list_chats(
|
||||
@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),
|
||||
):
|
||||
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
|
||||
raise HTTPException(400)
|
||||
return store.list_messages(actor["id"], str(chat_id), int(limit), cursor)
|
||||
|
||||
@@ -44,6 +44,7 @@ class Store:
|
||||
self.users: dict[str, dict] = {}
|
||||
self.usernames: dict[str, str] = {}
|
||||
self.chats: dict[str, dict] = {}
|
||||
self.messages: dict[str, list[dict]] = {}
|
||||
|
||||
def register(self, username: str, display_name: str) -> dict:
|
||||
with self.lock:
|
||||
@@ -67,6 +68,7 @@ class Store:
|
||||
raise HTTPException(400)
|
||||
chat = {"id": str(uuid4()), "title": title, "member_ids": member_ids.copy(), "created_at": now()}
|
||||
self.chats[chat["id"]] = chat
|
||||
self.messages[chat["id"]] = []
|
||||
return chat
|
||||
|
||||
def chat(self, actor_id: str, chat_id: str) -> dict:
|
||||
@@ -89,5 +91,30 @@ class Store:
|
||||
next_cursor = encode_cursor(f"chats:{actor_id}", page[-1]) if len(items) > limit else None
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
def create_message(self, actor_id: str, chat_id: str, text: str) -> dict:
|
||||
with self.lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or actor_id not in chat["member_ids"]:
|
||||
raise HTTPException(404)
|
||||
message = {
|
||||
"id": str(uuid4()), "chat_id": chat_id, "sender_id": actor_id,
|
||||
"text": text, "created_at": now(),
|
||||
}
|
||||
self.messages[chat_id].append(message)
|
||||
return message
|
||||
|
||||
def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict:
|
||||
with self.lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or actor_id not in chat["member_ids"]:
|
||||
raise HTTPException(404)
|
||||
after = decode_cursor(cursor, f"messages:{chat_id}")
|
||||
items = sorted(self.messages[chat_id], key=lambda item: (item["created_at"], item["id"]))
|
||||
if after is not None:
|
||||
items = [item for item in items if (item["created_at"], item["id"]) > after]
|
||||
page = items[:limit]
|
||||
next_cursor = encode_cursor(f"messages:{chat_id}", page[-1]) if len(items) > limit else None
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
|
||||
store = Store()
|
||||
|
||||
Reference in New Issue
Block a user