"""Shared, transactional idempotency for message creation. The unique scope is (user, method, path, key). The idempotency row, the message row and its attachment links are written in one PostgreSQL transaction, so a crash or rollback cannot leave a committed key without its message. Concurrent retries through different API replicas converge on the same primary-key row: the loser of the INSERT ... ON CONFLICT waits for the winner to commit and then reads the original message. Only the canonical body is hashed, so JSON key order and an omitted ``attachment_ids`` equal to ``[]`` are the same request. """ from __future__ import annotations import hashlib import json import secrets import uuid from datetime import datetime from . import db from .config import settings from .errors import conflict, not_found def fingerprint(text: str, attachment_ids: list[str]) -> str: payload = { "text": text, "attachment_ids": sorted(attachment_ids), } canonical = json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":")) return hashlib.sha256(canonical.encode("utf-8")).hexdigest() def _cleanup_expired(conn) -> None: """Best-effort maintenance; never on the critical path of every request.""" if secrets.randbelow(100) != 0: return conn.execute( """ DELETE FROM idempotency_keys WHERE ctid IN ( SELECT ctid FROM idempotency_keys WHERE expires_at <= clock_timestamp() LIMIT 1000 ) """ ) def _validate_attachments(conn, chat_id: uuid.UUID, attachment_ids: list[str]) -> None: if not attachment_ids: return parsed = [uuid.UUID(item) for item in attachment_ids] rows = conn.execute( """ SELECT id, chat_id, status FROM attachments WHERE id = ANY(%s) FOR SHARE """, (parsed,), ).fetchall() if len(rows) != len(parsed): raise not_found("Вложение не найдено") by_id = {str(row["id"]): row for row in rows} for attachment_id in attachment_ids: row = by_id.get(attachment_id) if row is None or row["chat_id"] != chat_id: raise not_found("Вложение не найдено в этом чате") if row["status"] != "ready": raise conflict("Вложение ещё не готово") def _message_with_attachments(conn, message_id) -> dict: row = conn.execute( """ SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, ARRAY( SELECT ma.attachment_id FROM message_attachments ma WHERE ma.message_id = m.id ORDER BY ma.position ) AS attachment_ids FROM messages m WHERE m.id = %s """, (message_id,), ).fetchone() if row is None: raise conflict("Результат ещё не готов, повторите запрос") return row def create_message( *, chat_id: uuid.UUID, actor_id: uuid.UUID, path: str, key: str, text: str, attachment_ids: list[str], ) -> dict: """Return the original or newly created message row, atomically.""" request_hash = fingerprint(text, attachment_ids) message_id = uuid.uuid4() with db.pool().connection() as conn: with conn.transaction(): _validate_attachments(conn, chat_id, attachment_ids) # An expired key may be reused: remove it inside the same scope. conn.execute( """ DELETE FROM idempotency_keys WHERE user_id = %s AND method = 'POST' AND path = %s AND key = %s AND expires_at <= clock_timestamp() """, (actor_id, path, key), ) _cleanup_expired(conn) inserted = conn.execute( """ INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, message_id, expires_at) VALUES (%s, 'POST', %s, %s, %s, %s, clock_timestamp() + (%s * interval '1 second')) ON CONFLICT (user_id, method, path, key) DO NOTHING RETURNING message_id """, ( actor_id, path, key, request_hash, message_id, settings.idempotency_ttl_seconds, ), ).fetchone() if inserted is not None: conn.execute( """ INSERT INTO messages (id, chat_id, sender_id, text) VALUES (%s, %s, %s, %s) """, (inserted["message_id"], chat_id, actor_id, text), ) if attachment_ids: with conn.cursor() as cursor: cursor.executemany( """ INSERT INTO message_attachments (message_id, attachment_id, position) VALUES (%s, %s, %s) """, [ (inserted["message_id"], uuid.UUID(item), position) for position, item in enumerate(attachment_ids) ], ) return _message_with_attachments(conn, inserted["message_id"]) existing = conn.execute( """ SELECT request_hash, message_id, expires_at FROM idempotency_keys WHERE user_id = %s AND method = 'POST' AND path = %s AND key = %s """, (actor_id, path, key), ).fetchone() if existing is None: # The row disappeared between the conflict and this read (only # possible with a concurrent expiry cleanup). 409 asks the # client to retry rather than creating an ambiguous duplicate. raise conflict("Повторите запрос: ключ идемпотентности истёк") if existing["request_hash"] != request_hash: raise conflict("Idempotency-Key уже использован с другим сообщением") return _message_with_attachments(conn, existing["message_id"])