from __future__ import annotations import hashlib import json from datetime import datetime, timezone from typing import List, Optional, Tuple from uuid import UUID, uuid4 import asyncpg from app import cache, config, db from app.errors import AppError def format_ts(dt: datetime) -> str: if dt.tzinfo is None: dt = dt.replace(tzinfo=timezone.utc) return dt.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z") def user_dict(row) -> dict: return { "id": str(row["id"]), "username": row["username"], "display_name": row["display_name"], "created_at": format_ts(row["created_at"]), } def chat_dict(row, member_ids: List[str]) -> dict: return { "id": str(row["id"]), "title": row["title"], "member_ids": member_ids, "created_at": format_ts(row["created_at"]), } def message_dict(row) -> dict: raw_ids = row["attachment_ids"] if "attachment_ids" in row.keys() else [] if raw_ids is None: raw_ids = [] return { "id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), "text": row["text"], "created_at": format_ts(row["created_at"]), "attachment_ids": [str(item) for item in raw_ids], } async def _run(coro): try: return await coro except AppError: raise except db.INTEGRITY: raise except Exception: raise AppError(503, "service unavailable") from None async def create_user(username: str, display_name: str, password_hash: str) -> Tuple[Optional[dict], Optional[str]]: user_id = uuid4() created = datetime.now(timezone.utc) try: async with db.require().acquire() as conn: await conn.execute( """ INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, $2, $3, $4, $5) """, user_id, username, display_name, password_hash, created, ) except asyncpg.UniqueViolationError: return None, "conflict" except Exception: raise AppError(503, "service unavailable") from None return user_dict({"id": user_id, "username": username, "display_name": display_name, "created_at": created}), None async def get_user(user_id: str) -> Optional[dict]: row = await _run( _fetchrow("SELECT id, username, display_name, created_at FROM users WHERE id = $1", UUID(user_id)) ) return user_dict(row) if row else None async def get_user_by_username(username: str): return await _run( _fetchrow( "SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1", username, ) ) async def _fetchrow(sql: str, *args): async with db.require().acquire() as conn: return await conn.fetchrow(sql, *args) async def _fetch(sql: str, *args): async with db.require().acquire() as conn: return await conn.fetch(sql, *args) async def member_ids_of(chat_id: UUID) -> List[str]: rows = await _run(_fetch("SELECT user_id FROM chat_members WHERE chat_id = $1 ORDER BY user_id", chat_id)) return [str(r["user_id"]) for r in rows] async def create_chat(title: str, member_ids: List[str], caller_id: str) -> Tuple[Optional[dict], Optional[str]]: if caller_id not in member_ids: return None, "caller not in members" if len(set(member_ids)) != len(member_ids): return None, "duplicate members" ids = [UUID(x) for x in member_ids] chat_id = uuid4() created = datetime.now(timezone.utc) try: async with db.require().acquire() as conn: async with conn.transaction(): existing = await conn.fetch("SELECT id FROM users WHERE id = ANY($1::uuid[])", ids) if len(existing) != len(ids): return None, "unknown member" await conn.execute( "INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)", chat_id, title, created, ) await conn.executemany( "INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)", [(chat_id, mid) for mid in ids], ) except AppError: raise except Exception: raise AppError(503, "service unavailable") from None return chat_dict({"id": chat_id, "title": title, "created_at": created}, member_ids), None async def get_chat(chat_id: str, user_id: str) -> Optional[dict]: row = await load_chat_for_member(chat_id, user_id) if row is None: return None return chat_dict(row, await member_ids_of(UUID(chat_id))) async def load_chat_for_member(chat_id: str, user_id: str): return await _run( _fetchrow( """ SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id = c.id AND m.user_id = $2 WHERE c.id = $1 """, UUID(chat_id), UUID(user_id), ) ) async def list_chats(user_id: str, limit: int, after: Optional[Tuple[str, str]] = None): args: list = [UUID(user_id)] clause = "" if after is not None: clause = "AND (c.created_at, c.id) > ($2, $3)" args.extend([datetime.fromisoformat(after[0].replace("Z", "+00:00")), UUID(after[1])]) rows = await _run( _fetch( f""" SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $1 WHERE 1=1 {clause} ORDER BY c.created_at, c.id LIMIT {int(limit) + 1} """, *args, ) ) page = rows[:limit] items = [chat_dict(row, await member_ids_of(row["id"])) for row in page] next_key = None if len(rows) > limit: last = page[-1] next_key = (format_ts(last["created_at"]), str(last["id"])) return items, next_key def _canonical_message(text: str, attachment_ids: List[str]) -> str: # Missing attachment_ids and [] are the same message. Key order does not matter. return json.dumps( {"attachment_ids": attachment_ids, "text": text}, sort_keys=True, ensure_ascii=False, separators=(",", ":"), ) async def add_message( chat_id: str, sender_id: str, text: str, idem_key: str, path: str, attachment_ids: Optional[List[str]] = None, ) -> Tuple[Optional[dict], Optional[str]]: """Insert one message or replay the stored 201. The row and the result commit together. Same user+method+path+key is serialized with a Postgres advisory lock so two replicas cannot insert two messages. A failed transaction stores nothing. """ attachment_ids = list(attachment_ids or []) digest = hashlib.sha256(_canonical_message(text, attachment_ids).encode("utf-8")).hexdigest() lock_key = f"{sender_id}|POST|{path}|{idem_key}" payload: Optional[dict] = None created_new = False try: async with db.require().acquire() as conn: async with conn.transaction(): await conn.execute("SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0))", lock_key) member = await conn.fetchrow( """ SELECT c.id FROM chats c JOIN chat_members m ON m.chat_id = c.id AND m.user_id = $2 WHERE c.id = $1 """, UUID(chat_id), UUID(sender_id), ) if member is None: return None, "not_found" row = await conn.fetchrow( """ SELECT request_hash, response_json, created_at FROM idempotency_keys WHERE user_id = $1 AND http_method = 'POST' AND path = $2 AND idem_key = $3 """, UUID(sender_id), path, idem_key, ) if row is not None: created_at = row["created_at"] if created_at.tzinfo is None: created_at = created_at.replace(tzinfo=timezone.utc) age = (datetime.now(timezone.utc) - created_at).total_seconds() if age <= config.IDEMPOTENCY_TTL_SECONDS: if row["request_hash"] != digest: return None, "conflict" return json.loads(row["response_json"]), None await conn.execute( """ DELETE FROM idempotency_keys WHERE user_id = $1 AND http_method = 'POST' AND path = $2 AND idem_key = $3 """, UUID(sender_id), path, idem_key, ) resolved: List[str] = [] if attachment_ids: rows = await conn.fetch( """ SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[]) """, [UUID(item) for item in attachment_ids], ) found = {str(row["id"]): row for row in rows} if len(found) != len(attachment_ids) or any( str(found[item]["chat_id"]) != chat_id for item in attachment_ids ): return None, "not_found" if any(found[item]["status"] == "failed" for item in attachment_ids): return None, "not_ready" resolved = attachment_ids message_id = uuid4() created = datetime.now(timezone.utc) await conn.execute( """ INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5) """, message_id, UUID(chat_id), UUID(sender_id), text, created, ) if resolved: await conn.executemany( """ INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1, $2, $3) """, [(message_id, UUID(item), index) for index, item in enumerate(resolved)], ) payload = message_dict( { "id": message_id, "chat_id": UUID(chat_id), "sender_id": UUID(sender_id), "text": text, "created_at": created, "attachment_ids": resolved, } ) await conn.execute( """ INSERT INTO idempotency_keys (user_id, http_method, path, idem_key, request_hash, response_json, created_at) VALUES ($1, 'POST', $2, $3, $4, $5, $6) """, UUID(sender_id), path, idem_key, digest, json.dumps(payload, ensure_ascii=False, separators=(",", ":")), created, ) created_new = True except AppError: raise except Exception: raise AppError(503, "service unavailable") from None if created_new: await cache.invalidate_history(chat_id) return payload, None async def list_messages(chat_id: str, user_id: str, limit: int, after: Optional[Tuple[str, str]] = None): if await load_chat_for_member(chat_id, user_id) is None: return None, None, "not_found" cursor = "" after_ts = after_id = None if after is not None: cursor = after[0] + ":" + after[1] after_ts = datetime.fromisoformat(after[0].replace("Z", "+00:00")) after_id = UUID(after[1]) async def loader() -> dict: await cache.incr_history_sql() if after_ts is None: rows = await _run( _fetch( """ SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, COALESCE( array_agg(ma.attachment_id ORDER BY ma.position) FILTER (WHERE ma.attachment_id IS NOT NULL), ARRAY[]::uuid[] ) AS attachment_ids FROM messages m LEFT JOIN message_attachments ma ON ma.message_id = m.id WHERE m.chat_id = $1 GROUP BY m.id ORDER BY m.created_at, m.id LIMIT $2 """, UUID(chat_id), limit + 1, ) ) else: rows = await _run( _fetch( """ SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, COALESCE( array_agg(ma.attachment_id ORDER BY ma.position) FILTER (WHERE ma.attachment_id IS NOT NULL), ARRAY[]::uuid[] ) AS attachment_ids FROM messages m LEFT JOIN message_attachments ma ON ma.message_id = m.id WHERE m.chat_id = $1 AND (m.created_at, m.id) > ($2, $3) GROUP BY m.id ORDER BY m.created_at, m.id LIMIT $4 """, UUID(chat_id), after_ts, after_id, limit + 1, ) ) page = rows[:limit] items = [message_dict(r) for r in page] next_key = None if len(rows) > limit: last = page[-1] next_key = (format_ts(last["created_at"]), str(last["id"])) return {"items": items, "next_key": next_key} page = await cache.get_history_page(chat_id, cursor, limit, loader) return page["items"], page.get("next_key"), None