import datetime as dt import hashlib import uuid from argon2 import PasswordHasher from argon2.exceptions import InvalidHashError, VerificationError, VerifyMismatchError from argon2.low_level import Type from psycopg.errors import UniqueViolation from app import broker, storage, telemetry from app.codec import ( USERNAME, IDEMPOTENCY, canonical_message, decode_cursor, detect_image, encode_cursor, expect_fields, iso, parse_uuid, require_text, utcnow, ) from app.db import connection, jsonb from app.errors import bad_request, conflict, not_found, too_large, unauthorized, unsupported from app.images import validate_image from app.settings import settings BASIC = 'Basic realm="messenger", charset="UTF-8"' BEARER = 'Bearer realm="messenger"' _hasher = PasswordHasher(time_cost=1, memory_cost=8192, parallelism=1, hash_len=32, salt_len=16, type=Type.ID) _dummy = None def init_hasher(): global _dummy _dummy = _hasher.hash("not-a-real-password") def _user_json(row): return { "id": str(row["id"]), "username": row["username"], "display_name": row["display_name"], "created_at": iso(row["created_at"]), } def _token_hash(token): return hashlib.sha256(token.encode("utf-8")).digest() def _password(value): if not isinstance(value, str) or "\x00" in value or not (12 <= len(value) <= 128): raise bad_request() return value def as_uuid(value): if isinstance(value, uuid.UUID): return value return uuid.UUID(str(value)) def _username(value): if not isinstance(value, str) or USERNAME.fullmatch(value) is None: raise bad_request() return value def register(data): expect_fields(data, ["username", "display_name", "password"], ["username", "display_name", "password"]) username = _username(data["username"]) display_name = require_text(data["display_name"], 1, 100) password = _password(data["password"]) password_hash = _hasher.hash(password) user_id = uuid.uuid4() created_at = utcnow() try: with connection() as conn: conn.execute( """ INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES (%s, %s, %s, %s, %s) """, (user_id, username, display_name, password_hash, created_at), ) row = conn.execute( "SELECT id, username, display_name, created_at FROM users WHERE id = %s", (user_id,), ).fetchone() except UniqueViolation as exc: raise conflict("Имя пользователя занято") from exc return 201, _user_json(row) def login(username, password): if not isinstance(username, str) or not isinstance(password, str) or "\x00" in username or "\x00" in password: raise unauthorized(BASIC) with connection() as conn: row = conn.execute( "SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s", (username,), ).fetchone() stored = row["password_hash"] if row else _dummy try: _hasher.verify(stored, password) valid = row is not None except (VerifyMismatchError, VerificationError, InvalidHashError): valid = False if not valid: raise unauthorized(BASIC) token = secrets_token() expires_at = utcnow() + dt.timedelta(seconds=settings.session_ttl) with connection() as conn: conn.execute( "INSERT INTO sessions (token_hash, user_id, expires_at) VALUES (%s, %s, %s)", (_token_hash(token), row["id"], expires_at), ) return 201, { "token": token, "token_type": "Bearer", "expires_at": iso(expires_at), "user": _user_json(row), } def secrets_token(): import secrets return secrets.token_urlsafe(32) def current_user(token): if not token or len(token) < 32: raise unauthorized(BEARER) with connection() as conn: row = conn.execute( """ SELECT u.id, u.username, u.display_name, u.created_at FROM sessions s JOIN users u ON u.id = s.user_id WHERE s.token_hash = %s AND s.revoked_at IS NULL AND s.expires_at > NOW() """, (_token_hash(token),), ).fetchone() if not row: raise unauthorized(BEARER) return row def ready_check(): with connection() as conn: conn.execute("SELECT 1") if not storage.ready() or not broker.ready(): raise unavailable() def revoke(token): user = current_user(token) with connection() as conn: conn.execute( "UPDATE sessions SET revoked_at = NOW() WHERE token_hash = %s AND revoked_at IS NULL", (_token_hash(token),), ) return user def create_chat(user, data): expect_fields(data, ["title", "member_ids"], ["title", "member_ids"]) title = require_text(data["title"], 1, 100) raw_members = data["member_ids"] if not isinstance(raw_members, list) or not (1 <= len(raw_members) <= 100): raise bad_request() members = [parse_uuid(item) for item in raw_members] if len(set(members)) != len(members): raise bad_request() if str(user["id"]) not in members: raise bad_request() chat_id = uuid.uuid4() created_at = utcnow() with connection() as conn: found = conn.execute("SELECT id FROM users WHERE id = ANY(%s)", ([as_uuid(item) for item in members],)).fetchall() if len(found) != len(members): raise bad_request() conn.execute( "INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)", (chat_id, title, created_at), ) for position, member_id in enumerate(members): conn.execute( "INSERT INTO chat_members (chat_id, user_id, position) VALUES (%s, %s, %s)", (chat_id, as_uuid(member_id), position), ) return 201, { "id": str(chat_id), "title": title, "member_ids": members, "created_at": iso(created_at), } def _chat_json(conn, row): members = conn.execute( "SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY position", (row["id"],), ).fetchall() return { "id": str(row["id"]), "title": row["title"], "member_ids": [str(item["user_id"]) for item in members], "created_at": iso(row["created_at"]), } def list_chats(user, limit, cursor): created_at = None row_id = None if cursor is not None: created_at, row_id = decode_cursor(cursor, "chats", str(user["id"])) with connection() as conn: if created_at is None: rows = conn.execute( """ SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id = c.id WHERE m.user_id = %s ORDER BY c.created_at, c.id LIMIT %s """, (user["id"], limit + 1), ).fetchall() else: rows = conn.execute( """ SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id = c.id WHERE m.user_id = %s AND (c.created_at, c.id) > (%s, %s) ORDER BY c.created_at, c.id LIMIT %s """, (user["id"], created_at, as_uuid(row_id), limit + 1), ).fetchall() extra = len(rows) > limit rows = rows[:limit] items = [_chat_json(conn, row) for row in rows] next_cursor = None if extra and rows: last = rows[-1] next_cursor = encode_cursor("chats", str(user["id"]), last["created_at"], str(last["id"])) return {"items": items, "next_cursor": next_cursor} def get_chat(user, chat_id): with connection() as conn: row = _visible_chat(conn, user["id"], chat_id) return _chat_json(conn, row) def _visible_chat(conn, user_id, chat_id): row = conn.execute( """ SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id = c.id WHERE c.id = %s AND m.user_id = %s """, (as_uuid(chat_id), as_uuid(user_id)), ).fetchone() if not row: raise not_found() return row def _message_json(row, attachment_ids): return { "id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), "text": row["text"], "created_at": iso(row["created_at"]), "attachment_ids": attachment_ids, } def _attachment_ids(conn, message_id): rows = conn.execute( "SELECT attachment_id FROM message_attachments WHERE message_id = %s ORDER BY position", (message_id,), ).fetchall() return [str(row["attachment_id"]) for row in rows] def create_message(user, chat_id, key, data, path): chat_uuid = as_uuid(chat_id) if IDEMPOTENCY.fullmatch(key or "") is None: raise bad_request() expect_fields(data, ["text", "attachment_ids"], ["text"]) if not isinstance(data["text"], str) or "\x00" in data["text"] or len(data["text"]) > 4000: raise bad_request() text = data["text"] if "attachment_ids" in data: raw_ids = data["attachment_ids"] if not isinstance(raw_ids, list) or len(raw_ids) > 10: raise bad_request() attachment_ids = [parse_uuid(item) for item in raw_ids] if len(set(attachment_ids)) != len(attachment_ids): raise bad_request() else: attachment_ids = [] if not text.strip() and not attachment_ids: raise bad_request() body_hash = canonical_message(text, attachment_ids) lock = int.from_bytes(hashlib.sha256(f"{user['id']}|POST|{path}|{key}".encode("utf-8")).digest()[:8], "big", signed=True) with connection() as conn: _visible_chat(conn, user["id"], chat_id) conn.execute("SELECT pg_advisory_xact_lock(%s)", (lock,)) existing = conn.execute( """ SELECT body_hash, response_json, created_at FROM idempotency WHERE user_id = %s AND method = 'POST' AND path = %s AND idem_key = %s """, (user["id"], path, key), ).fetchone() if existing and existing["created_at"] >= utcnow() - dt.timedelta(seconds=settings.idempotency_ttl): if bytes(existing["body_hash"]) != body_hash: raise conflict("Ключ идемпотентности уже использован с другим телом") stored = existing["response_json"] if isinstance(stored, str): import json stored = json.loads(stored) return 201, stored if existing: conn.execute( "DELETE FROM idempotency WHERE user_id = %s AND method = 'POST' AND path = %s AND idem_key = %s", (user["id"], path, key), ) if attachment_ids: rows = conn.execute( "SELECT id, chat_id, status FROM attachments WHERE id = ANY(%s)", ([as_uuid(item) for item in attachment_ids],), ).fetchall() found = {str(row["id"]): row for row in rows} if len(found) != len(attachment_ids): raise not_found() for attachment_id in attachment_ids: row = found[attachment_id] if str(row["chat_id"]) != chat_id: raise not_found() if row["status"] == "failed": raise conflict("Нельзя прикрепить неуспешное вложение") message_id = uuid.uuid4() created_at = utcnow() conn.execute( "INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES (%s, %s, %s, %s, %s)", (message_id, chat_uuid, user["id"], text, created_at), ) for position, attachment_id in enumerate(attachment_ids): conn.execute( "INSERT INTO message_attachments (message_id, attachment_id, position) VALUES (%s, %s, %s)", (message_id, as_uuid(attachment_id), position), ) stored = conn.execute( "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE id = %s", (message_id,), ).fetchone() payload = _message_json(stored, attachment_ids) conn.execute( """ INSERT INTO idempotency (user_id, method, path, idem_key, body_hash, status_code, response_json, created_at) VALUES (%s, 'POST', %s, %s, %s, 201, %s, %s) """, (user["id"], path, key, body_hash, jsonb(payload), created_at), ) return 201, payload def list_messages(user, chat_id, limit, cursor): chat_uuid = as_uuid(chat_id) created_at = None row_id = None if cursor is not None: created_at, row_id = decode_cursor(cursor, "messages", chat_id) with connection() as conn: _visible_chat(conn, user["id"], chat_uuid) if created_at is None: rows = conn.execute( """ SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = %s ORDER BY created_at, id LIMIT %s """, (chat_uuid, limit + 1), ).fetchall() else: rows = conn.execute( """ SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = %s AND (created_at, id) > (%s, %s) ORDER BY created_at, id LIMIT %s """, (chat_uuid, created_at, as_uuid(row_id), limit + 1), ).fetchall() extra = len(rows) > limit rows = rows[:limit] items = [_message_json(row, _attachment_ids(conn, row["id"])) for row in rows] next_cursor = None if extra and rows: last = rows[-1] next_cursor = encode_cursor("messages", chat_id, last["created_at"], str(last["id"])) return {"items": items, "next_cursor": next_cursor} def _variants(conn, attachment_id): rows = conn.execute( """ SELECT name, content_type, size_bytes, width, height, sha256 FROM attachment_variants WHERE attachment_id = %s AND pipeline_version = 1 ORDER BY name """, (attachment_id,), ).fetchall() return [ { "name": row["name"], "content_type": row["content_type"], "size_bytes": row["size_bytes"], "width": row["width"], "height": row["height"], "sha256": row["sha256"], } for row in rows ] def _attachment_json(row, variants): return { "id": str(row["id"]), "chat_id": str(row["chat_id"]), "owner_id": str(row["owner_id"]), "filename": row["filename"], "content_type": row["content_type"], "size_bytes": row["size_bytes"], "sha256": row["sha256"], "status": row["status"], "variants": variants, "error_code": row["error_code"], "created_at": iso(row["created_at"]), "updated_at": iso(row["updated_at"]), } def upload(user, chat_id, filename, content_type, data): import hashlib as hl if not data: raise unsupported() if len(data) > settings.max_upload: raise too_large() detected = detect_image(data, content_type) if not detected: raise unsupported() validate_image(data) with connection() as conn: _visible_chat(conn, user["id"], chat_id) attachment_id = uuid.uuid4() created_at = utcnow() digest = hl.sha256(data).hexdigest() staging_key = f"staging/{attachment_id}" storage.put(staging_key, data, detected, "staging") carrier = telemetry.inject_carrier() job_id = uuid.uuid4() payload = { "schema_version": 1, "job_id": str(job_id), "type": "attachment.process.v1", "attachment_id": str(attachment_id), "pipeline_version": 1, "attempt": 1, "created_at": iso(created_at), } if carrier.get("traceparent"): payload["traceparent"] = carrier["traceparent"] if carrier.get("tracestate"): payload["tracestate"] = carrier["tracestate"] row = { "id": attachment_id, "chat_id": chat_id, "owner_id": user["id"], "filename": filename, "content_type": detected, "size_bytes": len(data), "sha256": digest, "status": "queued", "error_code": None, "created_at": created_at, "updated_at": created_at, } with connection() as conn: _visible_chat(conn, user["id"], chat_id) conn.execute( """ INSERT INTO attachments ( id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, error_code, staging_key, pipeline_version, attempt, created_at, updated_at ) VALUES (%s, %s, %s, %s, %s, %s, %s, 'queued', NULL, %s, 1, 0, %s, %s) """, ( attachment_id, as_uuid(chat_id), user["id"], filename, detected, len(data), digest, staging_key, created_at, created_at, ), ) broker.insert_outbox(conn, payload, created_at) try: with connection() as conn: broker.publish_pending(conn, job_id) except Exception: telemetry.log_event("job.publish_deferred", severity="WARNING", job_id=str(job_id)) telemetry.log_event("attachment.accepted", attachment_id=str(attachment_id), size_bytes=len(data), content_type=detected) return 202, _attachment_json(row, []) def get_attachment(user, attachment_id): attachment_uuid = as_uuid(attachment_id) with connection() as conn: row = _visible_attachment(conn, user["id"], attachment_uuid) return _attachment_json(row, _variants(conn, attachment_uuid)) def _visible_attachment(conn, user_id, attachment_id): row = conn.execute("SELECT * FROM attachments WHERE id = %s", (as_uuid(attachment_id),)).fetchone() if not row: raise not_found() member = conn.execute( "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", (row["chat_id"], user_id), ).fetchone() if not member: raise not_found() return row def download(user, attachment_id, variant): attachment_uuid = as_uuid(attachment_id) if variant not in {"original", "thumbnail", "crop", "optimized"}: raise bad_request() with connection() as conn: row = _visible_attachment(conn, user["id"], attachment_uuid) if row["status"] != "ready": raise conflict("Вложение ещё не готово") if variant == "original": key = row["original_key"] content_type = row["content_type"] else: found = conn.execute( """ SELECT object_key, content_type FROM attachment_variants WHERE attachment_id = %s AND name = %s AND pipeline_version = 1 """, (attachment_uuid, variant), ).fetchone() if not found: raise not_found() key = found["object_key"] content_type = found["content_type"] return storage.get(key, variant), content_type