forked from mtusi2026/mtusi2026lab5
1273 lines
33 KiB
Python
1273 lines
33 KiB
Python
|
|
import base64
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import os
|
|
import re
|
|
import secrets
|
|
import uuid
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
from psycopg.errors import UniqueViolation
|
|
|
|
from argon2 import PasswordHasher
|
|
from argon2.exceptions import (
|
|
VerifyMismatchError,
|
|
VerificationError,
|
|
InvalidHashError,
|
|
)
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import JSONResponse, Response
|
|
from fastapi.exceptions import RequestValidationError
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import BotoCoreError, ClientError
|
|
from PIL import Image, UnidentifiedImageError
|
|
|
|
app = FastAPI(title="MTUSI Messenger Lab5")
|
|
|
|
INSTANCE_ID = str(uuid.uuid4())
|
|
|
|
DATABASE_URL = os.environ["DATABASE_URL"]
|
|
SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
|
|
|
IMAGE_MAX_ATTEMPTS = int(os.getenv("IMAGE_MAX_ATTEMPTS", "5"))
|
|
S3_BUCKET = os.environ["S3_BUCKET"]
|
|
MAX_IMAGE_BYTES = 10 * 1024 * 1024
|
|
MAX_IMAGE_PIXELS = 40_000_000
|
|
|
|
s3 = boto3.client(
|
|
"s3",
|
|
endpoint_url=os.environ["S3_ENDPOINT"],
|
|
aws_access_key_id=os.environ["S3_ACCESS_KEY"],
|
|
aws_secret_access_key=os.environ["S3_SECRET_KEY"],
|
|
region_name="us-east-1",
|
|
config=Config(
|
|
signature_version="s3v4",
|
|
s3={"addressing_style": "path"},
|
|
connect_timeout=3,
|
|
read_timeout=10,
|
|
retries={"max_attempts": 1},
|
|
),
|
|
)
|
|
|
|
password_hasher = PasswordHasher()
|
|
|
|
|
|
# ---------- Helpers ----------
|
|
|
|
class ApiError(Exception):
|
|
def __init__(self, status, code, message, headers=None):
|
|
self.status = status
|
|
self.code = code
|
|
self.message = message
|
|
self.headers = headers or {}
|
|
|
|
|
|
def fail(status, code, message, headers=None):
|
|
raise ApiError(status, code, message, headers)
|
|
|
|
|
|
def db():
|
|
return psycopg.connect(DATABASE_URL, row_factory=dict_row)
|
|
|
|
|
|
def utc_string(value):
|
|
return value.astimezone(timezone.utc).isoformat(
|
|
timespec="microseconds"
|
|
).replace("+00:00", "Z")
|
|
|
|
|
|
def user_json(row):
|
|
return {
|
|
"id": str(row["id"]),
|
|
"username": row["username"],
|
|
"display_name": row["display_name"],
|
|
"created_at": utc_string(row["created_at"]),
|
|
}
|
|
|
|
|
|
|
|
def message_json(row, conn=None):
|
|
attachment_ids = []
|
|
|
|
if conn is not None:
|
|
attachments = conn.execute(
|
|
"""
|
|
SELECT attachment_id
|
|
FROM message_attachments
|
|
WHERE message_id = %s
|
|
ORDER BY attachment_id
|
|
""",
|
|
(row["id"],),
|
|
).fetchall()
|
|
|
|
attachment_ids = [
|
|
str(item["attachment_id"])
|
|
for item in attachments
|
|
]
|
|
|
|
return {
|
|
"id": str(row["id"]),
|
|
"chat_id": str(row["chat_id"]),
|
|
"sender_id": str(row["sender_id"]),
|
|
"text": row["text"],
|
|
"attachment_ids": attachment_ids,
|
|
"created_at": utc_string(row["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(m["user_id"]) for m in members],
|
|
"created_at": utc_string(row["created_at"]),
|
|
}
|
|
|
|
|
|
def valid_uuid(value):
|
|
if not isinstance(value, str):
|
|
return False
|
|
try:
|
|
return str(uuid.UUID(value)) == value
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def valid_text(value, maximum):
|
|
return (
|
|
isinstance(value, str)
|
|
and 1 <= len(value) <= maximum
|
|
and bool(value.strip())
|
|
)
|
|
|
|
|
|
def validate_limit(request):
|
|
value = request.query_params.get("limit", "50")
|
|
|
|
if not re.fullmatch(r"[0-9]+", value):
|
|
fail(400, "invalid_request", "Invalid limit")
|
|
|
|
number = int(value)
|
|
|
|
if not 1 <= number <= 100:
|
|
fail(400, "invalid_request", "Invalid limit")
|
|
|
|
return number
|
|
|
|
|
|
def make_cursor(resource, row):
|
|
payload = {
|
|
"resource": resource,
|
|
"created_at": utc_string(row["created_at"]),
|
|
"id": str(row["id"]),
|
|
}
|
|
|
|
raw = json.dumps(payload).encode("utf-8")
|
|
|
|
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
|
|
|
|
|
|
def read_cursor(cursor, resource):
|
|
if not isinstance(cursor, str) or not cursor:
|
|
fail(400, "invalid_request", "Invalid cursor")
|
|
|
|
try:
|
|
raw = base64.urlsafe_b64decode(
|
|
cursor + "=" * (-len(cursor) % 4)
|
|
)
|
|
value = json.loads(raw)
|
|
|
|
if value["resource"] != resource:
|
|
raise ValueError()
|
|
|
|
if not valid_uuid(value["id"]):
|
|
raise ValueError()
|
|
|
|
timestamp = datetime.fromisoformat(
|
|
value["created_at"].replace("Z", "+00:00")
|
|
)
|
|
|
|
if timestamp.tzinfo is None:
|
|
raise ValueError()
|
|
|
|
return timestamp, value["id"]
|
|
|
|
except (ValueError, KeyError, TypeError, UnicodeDecodeError):
|
|
fail(400, "invalid_request", "Invalid cursor")
|
|
|
|
|
|
async def json_body(request, required, allowed):
|
|
content_type = request.headers.get("content-type", "")
|
|
media_type = content_type.split(";")[0].strip().lower()
|
|
|
|
if media_type != "application/json":
|
|
fail(415, "unsupported_media_type", "Expected JSON")
|
|
|
|
try:
|
|
data = await request.json()
|
|
except Exception:
|
|
fail(400, "invalid_request", "Invalid JSON")
|
|
|
|
if not isinstance(data, dict):
|
|
fail(400, "invalid_request", "Expected JSON object")
|
|
|
|
if not set(required).issubset(data):
|
|
fail(400, "invalid_request", "Missing fields")
|
|
|
|
if not set(data).issubset(allowed):
|
|
fail(400, "invalid_request", "Unexpected fields")
|
|
|
|
return data
|
|
|
|
|
|
def unauthorized(kind="Bearer"):
|
|
if kind == "Basic":
|
|
header = 'Basic realm="messenger", charset="UTF-8"'
|
|
else:
|
|
header = 'Bearer realm="messenger"'
|
|
|
|
fail(
|
|
401,
|
|
"unauthorized",
|
|
"Authentication required",
|
|
{"WWW-Authenticate": header},
|
|
)
|
|
|
|
|
|
def current_user(request):
|
|
authorization = request.headers.get("Authorization", "")
|
|
|
|
if not authorization.startswith("Bearer "):
|
|
unauthorized()
|
|
|
|
token = authorization[7:]
|
|
|
|
if not token:
|
|
unauthorized()
|
|
|
|
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
|
|
|
with db() 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 = FALSE
|
|
AND s.expires_at > clock_timestamp()
|
|
""",
|
|
(token_hash,)
|
|
).fetchone()
|
|
|
|
if row is None:
|
|
unauthorized()
|
|
|
|
return row
|
|
|
|
|
|
def accessible_chat(conn, chat_id, user_id):
|
|
if not valid_uuid(chat_id):
|
|
fail(400, "invalid_request", "Invalid 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
|
|
""",
|
|
(chat_id, user_id)
|
|
).fetchone()
|
|
|
|
if row is None:
|
|
fail(404, "not_found", "Chat not found")
|
|
|
|
return row
|
|
|
|
|
|
# ---------- Request IDs and errors ----------
|
|
|
|
def error_response(request, status, code, message, headers=None):
|
|
request_id = request.state.request_id
|
|
|
|
response_headers = {
|
|
"X-Request-Id": request_id,
|
|
**(headers or {}),
|
|
}
|
|
|
|
return JSONResponse(
|
|
status_code=status,
|
|
content={
|
|
"error": {
|
|
"code": code,
|
|
"message": message,
|
|
"request_id": request_id,
|
|
}
|
|
},
|
|
headers=response_headers,
|
|
)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def add_request_id(request, call_next):
|
|
request.state.request_id = str(uuid.uuid4())
|
|
|
|
try:
|
|
response = await call_next(request)
|
|
except psycopg.OperationalError:
|
|
response = error_response(
|
|
request, 503, "unavailable", "Database unavailable"
|
|
)
|
|
except Exception:
|
|
response = error_response(
|
|
request, 500, "internal_error", "Internal server error"
|
|
)
|
|
|
|
response.headers["X-Request-Id"] = request.state.request_id
|
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
|
return response
|
|
|
|
|
|
@app.exception_handler(ApiError)
|
|
async def api_error_handler(request, exc):
|
|
return error_response(
|
|
request, exc.status, exc.code, exc.message, exc.headers
|
|
)
|
|
|
|
|
|
@app.exception_handler(StarletteHTTPException)
|
|
async def http_error_handler(request, exc):
|
|
return error_response(
|
|
request, exc.status_code,
|
|
"not_found" if exc.status_code == 404 else "invalid_request",
|
|
"Resource not found" if exc.status_code == 404 else "Invalid request",
|
|
)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_error_handler(request, exc):
|
|
return error_response(
|
|
request, 400, "invalid_request", "Invalid request"
|
|
)
|
|
|
|
|
|
# ---------- Health ----------
|
|
|
|
@app.get("/health/live")
|
|
def health_live():
|
|
return {"status": "ok"}
|
|
|
|
|
|
|
|
@app.get("/health/ready")
|
|
def health_ready():
|
|
try:
|
|
with db() as conn:
|
|
conn.execute("SELECT 1")
|
|
|
|
s3.head_bucket(Bucket=S3_BUCKET)
|
|
|
|
return {"status": "ok"}
|
|
|
|
except (
|
|
psycopg.Error,
|
|
BotoCoreError,
|
|
ClientError,
|
|
OSError,
|
|
):
|
|
fail(
|
|
503,
|
|
"unavailable",
|
|
"Storage unavailable",
|
|
)
|
|
|
|
|
|
# ---------- Users ----------
|
|
|
|
@app.post("/api/v1/users", status_code=201)
|
|
async def create_user(request: Request):
|
|
data = await json_body(
|
|
request,
|
|
required=["username", "display_name", "password"],
|
|
allowed=["username", "display_name", "password"],
|
|
)
|
|
|
|
username = data["username"]
|
|
display_name = data["display_name"]
|
|
password = data["password"]
|
|
|
|
if (
|
|
not isinstance(username, str)
|
|
or not re.fullmatch(r"[a-z0-9_]{3,32}", username)
|
|
or not valid_text(display_name, 100)
|
|
or not isinstance(password, str)
|
|
or not 12 <= len(password) <= 128
|
|
):
|
|
fail(400, "invalid_request", "Invalid user fields")
|
|
|
|
password_hash = password_hasher.hash(password)
|
|
user_id = str(uuid.uuid4())
|
|
|
|
try:
|
|
with db() as conn:
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO users
|
|
(id, username, display_name, password_hash)
|
|
VALUES (%s, %s, %s, %s)
|
|
RETURNING id, username, display_name, created_at
|
|
""",
|
|
(user_id, username, display_name, password_hash)
|
|
).fetchone()
|
|
|
|
return user_json(row)
|
|
|
|
except UniqueViolation:
|
|
fail(409, "conflict", "Username already exists")
|
|
|
|
|
|
@app.get("/api/v1/users/me")
|
|
def get_me(request: Request):
|
|
return user_json(current_user(request))
|
|
|
|
|
|
# ---------- Sessions ----------
|
|
|
|
@app.post("/api/v1/auth/sessions", status_code=201)
|
|
async def create_session(request: Request):
|
|
authorization = request.headers.get("Authorization", "")
|
|
|
|
if not authorization.startswith("Basic "):
|
|
unauthorized("Basic")
|
|
|
|
try:
|
|
encoded = authorization[6:]
|
|
decoded = base64.b64decode(encoded, validate=True).decode("utf-8")
|
|
|
|
if ":" not in decoded:
|
|
raise ValueError()
|
|
|
|
username, password = decoded.split(":", 1)
|
|
|
|
except (ValueError, UnicodeDecodeError):
|
|
unauthorized("Basic")
|
|
|
|
if not re.fullmatch(r"[a-z0-9_]{3,32}", username):
|
|
unauthorized("Basic")
|
|
|
|
with db() as conn:
|
|
row = conn.execute(
|
|
"SELECT * FROM users WHERE username = %s",
|
|
(username,)
|
|
).fetchone()
|
|
|
|
if row is None:
|
|
unauthorized("Basic")
|
|
|
|
try:
|
|
password_hasher.verify(row["password_hash"], password)
|
|
except (VerifyMismatchError, VerificationError, InvalidHashError):
|
|
unauthorized("Basic")
|
|
|
|
token = secrets.token_urlsafe(32)
|
|
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
|
|
|
expires_at = datetime.now(timezone.utc) + timedelta(
|
|
seconds=SESSION_TTL_SECONDS
|
|
)
|
|
|
|
with db() as conn:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO sessions (token_hash, user_id, expires_at)
|
|
VALUES (%s, %s, %s)
|
|
""",
|
|
(token_hash, row["id"], expires_at)
|
|
)
|
|
|
|
return {
|
|
"token": token,
|
|
"token_type": "Bearer",
|
|
"expires_at": utc_string(expires_at),
|
|
"user": user_json(row),
|
|
}
|
|
|
|
|
|
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
|
def logout(request: Request):
|
|
current_user(request)
|
|
|
|
token = request.headers["Authorization"][7:]
|
|
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
|
|
|
with db() as conn:
|
|
conn.execute(
|
|
"UPDATE sessions SET revoked = TRUE WHERE token_hash = %s",
|
|
(token_hash,)
|
|
)
|
|
|
|
return Response(status_code=204)
|
|
|
|
|
|
# ---------- Chats ----------
|
|
|
|
@app.post("/api/v1/chats", status_code=201)
|
|
async def create_chat(request: Request):
|
|
actor = current_user(request)
|
|
|
|
data = await json_body(
|
|
request,
|
|
required=["title", "member_ids"],
|
|
allowed=["title", "member_ids"],
|
|
)
|
|
|
|
title = data["title"]
|
|
member_ids = data["member_ids"]
|
|
|
|
if not valid_text(title, 100):
|
|
fail(400, "invalid_request", "Invalid title")
|
|
|
|
if (
|
|
not isinstance(member_ids, list)
|
|
or not 1 <= len(member_ids) <= 100
|
|
or not all(valid_uuid(x) for x in member_ids)
|
|
or len(member_ids) != len(set(member_ids))
|
|
or str(actor["id"]) not in member_ids
|
|
):
|
|
fail(400, "invalid_request", "Invalid members")
|
|
|
|
chat_id = str(uuid.uuid4())
|
|
|
|
with db() as conn:
|
|
# All operations use one transaction.
|
|
existing = conn.execute(
|
|
"""
|
|
SELECT id FROM users
|
|
WHERE id = ANY(%s::uuid[])
|
|
FOR KEY SHARE
|
|
""",
|
|
(member_ids,)
|
|
).fetchall()
|
|
|
|
if len(existing) != len(member_ids):
|
|
fail(400, "invalid_request", "Unknown member")
|
|
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO chats (id, title)
|
|
VALUES (%s, %s)
|
|
RETURNING id, title, created_at
|
|
""",
|
|
(chat_id, title)
|
|
).fetchone()
|
|
|
|
for position, member_id in enumerate(member_ids):
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO chat_members (chat_id, user_id, position)
|
|
VALUES (%s, %s, %s)
|
|
""",
|
|
(chat_id, member_id, position)
|
|
)
|
|
|
|
result = chat_json(conn, row)
|
|
|
|
return result
|
|
|
|
|
|
@app.get("/api/v1/chats")
|
|
def list_chats(request: Request):
|
|
actor = current_user(request)
|
|
limit = validate_limit(request)
|
|
cursor = request.query_params.get("cursor")
|
|
|
|
resource = "chats:" + str(actor["id"])
|
|
|
|
with db() as conn:
|
|
if cursor 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
|
|
""",
|
|
(actor["id"], limit + 1)
|
|
).fetchall()
|
|
else:
|
|
timestamp, last_id = read_cursor(cursor, resource)
|
|
|
|
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
|
|
""",
|
|
(actor["id"], timestamp, last_id, limit + 1)
|
|
).fetchall()
|
|
|
|
has_more = len(rows) > limit
|
|
page = rows[:limit]
|
|
|
|
items = [chat_json(conn, row) for row in page]
|
|
|
|
next_cursor = (
|
|
make_cursor(resource, page[-1])
|
|
if has_more else None
|
|
)
|
|
|
|
return {"items": items, "next_cursor": next_cursor}
|
|
|
|
|
|
@app.get("/api/v1/chats/{chat_id}")
|
|
def get_chat(chat_id: str, request: Request):
|
|
actor = current_user(request)
|
|
|
|
with db() as conn:
|
|
row = accessible_chat(conn, chat_id, actor["id"])
|
|
result = chat_json(conn, row)
|
|
|
|
return result
|
|
|
|
|
|
# ---------- Attachments ----------
|
|
|
|
from src.attachments import (
|
|
MAX_BYTES,
|
|
valid_filename,
|
|
inspect_image,
|
|
attachment_json,
|
|
object_key,
|
|
)
|
|
|
|
|
|
def attachment_for_user(conn, attachment_id, user_id):
|
|
if not valid_uuid(attachment_id):
|
|
fail(400, "invalid_request", "Invalid attachment ID")
|
|
|
|
row = conn.execute(
|
|
"""
|
|
SELECT a.*
|
|
FROM attachments a
|
|
JOIN chat_members cm
|
|
ON cm.chat_id = a.chat_id
|
|
WHERE a.id = %s
|
|
AND cm.user_id = %s
|
|
""",
|
|
(attachment_id, user_id),
|
|
).fetchone()
|
|
|
|
if row is None:
|
|
fail(404, "not_found", "Attachment not found")
|
|
|
|
return row
|
|
|
|
|
|
@app.post(
|
|
"/api/v1/chats/{chat_id}/attachments",
|
|
status_code=202,
|
|
)
|
|
async def upload_attachment(chat_id: str, request: Request):
|
|
actor = current_user(request)
|
|
|
|
with db() as conn:
|
|
accessible_chat(conn, chat_id, actor["id"])
|
|
|
|
filename = request.query_params.get("filename")
|
|
|
|
if not valid_filename(filename):
|
|
fail(400, "invalid_request", "Invalid filename")
|
|
|
|
content_type = request.headers.get(
|
|
"content-type", ""
|
|
).split(";")[0].strip().lower()
|
|
|
|
if content_type not in ("image/png", "image/jpeg"):
|
|
fail(
|
|
415,
|
|
"unsupported_media_type",
|
|
"Expected PNG or JPEG",
|
|
)
|
|
|
|
content_length = request.headers.get("content-length")
|
|
|
|
if content_length is not None:
|
|
try:
|
|
declared_size = int(content_length)
|
|
except ValueError:
|
|
fail(
|
|
400,
|
|
"invalid_request",
|
|
"Invalid Content-Length",
|
|
)
|
|
|
|
if declared_size > MAX_BYTES:
|
|
fail(
|
|
413,
|
|
"payload_too_large",
|
|
"Image exceeds 10 MiB",
|
|
)
|
|
|
|
# Receive the HTTP body with a strict byte limit.
|
|
data = bytearray()
|
|
|
|
async for chunk in request.stream():
|
|
if len(data) + len(chunk) > MAX_BYTES:
|
|
fail(
|
|
413,
|
|
"payload_too_large",
|
|
"Image exceeds 10 MiB",
|
|
)
|
|
|
|
data.extend(chunk)
|
|
|
|
data = bytes(data)
|
|
|
|
try:
|
|
metadata = inspect_image(data, content_type)
|
|
except ValueError as exc:
|
|
if str(exc) in ("too_large", "too_many_pixels"):
|
|
fail(
|
|
413,
|
|
"payload_too_large",
|
|
"Image exceeds limits",
|
|
)
|
|
|
|
fail(
|
|
415,
|
|
"unsupported_media_type",
|
|
"Invalid PNG or JPEG",
|
|
)
|
|
|
|
attachment_id = str(uuid.uuid4())
|
|
job_id = str(uuid.uuid4())
|
|
|
|
# Server-generated keys, independent of filename.
|
|
staging_key = f"staging/{attachment_id}"
|
|
original_key = f"originals/{attachment_id}"
|
|
|
|
# First persist the accepted original in shared S3.
|
|
try:
|
|
s3.put_object(
|
|
Bucket=S3_BUCKET,
|
|
Key=staging_key,
|
|
Body=data,
|
|
ContentType=metadata["content_type"],
|
|
)
|
|
except (BotoCoreError, ClientError, OSError):
|
|
fail(
|
|
503,
|
|
"unavailable",
|
|
"Storage unavailable",
|
|
)
|
|
|
|
try:
|
|
with db() as conn:
|
|
accessible_chat(conn, chat_id, actor["id"])
|
|
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO attachments
|
|
(
|
|
id,
|
|
chat_id,
|
|
uploader_id,
|
|
filename,
|
|
content_type,
|
|
size_bytes,
|
|
sha256,
|
|
object_key,
|
|
staging_key,
|
|
width,
|
|
height,
|
|
status,
|
|
error_code
|
|
)
|
|
VALUES (
|
|
%s, %s, %s, %s, %s,
|
|
%s, %s, %s, %s, %s,
|
|
%s, 'queued', NULL
|
|
)
|
|
RETURNING *
|
|
""",
|
|
(
|
|
attachment_id,
|
|
chat_id,
|
|
actor["id"],
|
|
filename,
|
|
metadata["content_type"],
|
|
metadata["size_bytes"],
|
|
metadata["sha256"],
|
|
original_key,
|
|
staging_key,
|
|
metadata["width"],
|
|
metadata["height"],
|
|
),
|
|
).fetchone()
|
|
|
|
# Durable job record in the same DB transaction.
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO attachment_jobs
|
|
(
|
|
id,
|
|
attachment_id,
|
|
status,
|
|
attempts,
|
|
max_attempts,
|
|
next_attempt_at
|
|
)
|
|
VALUES (
|
|
%s, %s, 'queued',
|
|
0, %s, NOW()
|
|
)
|
|
""",
|
|
(
|
|
job_id,
|
|
attachment_id,
|
|
IMAGE_MAX_ATTEMPTS,
|
|
),
|
|
)
|
|
|
|
except Exception:
|
|
# The database transaction was not confirmed.
|
|
# Try to clean up the unreferenced staging object.
|
|
try:
|
|
s3.delete_object(
|
|
Bucket=S3_BUCKET,
|
|
Key=staging_key,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
raise
|
|
|
|
# Do not perform resizing or require the worker here.
|
|
# The dispatcher will publish this durable DB job.
|
|
return attachment_json(row)
|
|
|
|
|
|
|
|
@app.get("/api/v1/attachments/{attachment_id}")
|
|
def get_attachment(attachment_id: str, request: Request):
|
|
actor = current_user(request)
|
|
|
|
with db() as conn:
|
|
row = attachment_for_user(
|
|
conn, attachment_id, actor["id"]
|
|
)
|
|
|
|
variant_rows = conn.execute(
|
|
"""
|
|
SELECT variant_name, content_type,
|
|
size_bytes, sha256, width, height
|
|
FROM attachment_variants
|
|
WHERE attachment_id = %s
|
|
ORDER BY variant_name
|
|
""",
|
|
(attachment_id,),
|
|
).fetchall()
|
|
|
|
variants = [
|
|
{
|
|
"name": v["variant_name"],
|
|
"content_type": v["content_type"],
|
|
"size_bytes": v["size_bytes"],
|
|
"sha256": v["sha256"],
|
|
"width": v["width"],
|
|
"height": v["height"],
|
|
}
|
|
for v in variant_rows
|
|
]
|
|
|
|
return attachment_json(row, variants)
|
|
|
|
|
|
|
|
|
|
@app.get("/api/v1/attachments/{attachment_id}/content")
|
|
def download_attachment(
|
|
attachment_id: str,
|
|
request: Request,
|
|
):
|
|
actor = current_user(request)
|
|
|
|
variant = request.query_params.get(
|
|
"variant", "original"
|
|
)
|
|
|
|
with db() as conn:
|
|
row = attachment_for_user(
|
|
conn, attachment_id, actor["id"]
|
|
)
|
|
|
|
if row["status"] != "ready":
|
|
fail(
|
|
409,
|
|
"conflict",
|
|
"Attachment is not ready",
|
|
)
|
|
|
|
if variant == "original":
|
|
key = row["object_key"]
|
|
content_type = row["content_type"]
|
|
expected_size = row["size_bytes"]
|
|
expected_sha256 = row["sha256"]
|
|
|
|
elif variant == "thumbnail":
|
|
variant_row = conn.execute(
|
|
"""
|
|
SELECT object_key, content_type,
|
|
size_bytes, sha256
|
|
FROM attachment_variants
|
|
WHERE attachment_id = %s
|
|
AND variant_name = 'thumbnail'
|
|
""",
|
|
(attachment_id,),
|
|
).fetchone()
|
|
|
|
if variant_row is None:
|
|
fail(
|
|
404,
|
|
"not_found",
|
|
"Variant not found",
|
|
)
|
|
|
|
key = variant_row["object_key"]
|
|
content_type = variant_row["content_type"]
|
|
expected_size = variant_row["size_bytes"]
|
|
expected_sha256 = variant_row["sha256"]
|
|
|
|
else:
|
|
fail(
|
|
404,
|
|
"not_found",
|
|
"Variant not found",
|
|
)
|
|
|
|
try:
|
|
result = s3.get_object(
|
|
Bucket=S3_BUCKET,
|
|
Key=key,
|
|
)
|
|
|
|
body_stream = result["Body"]
|
|
|
|
try:
|
|
data = body_stream.read(MAX_BYTES + 1)
|
|
finally:
|
|
body_stream.close()
|
|
|
|
except (BotoCoreError, ClientError, OSError):
|
|
fail(
|
|
503,
|
|
"unavailable",
|
|
"Storage unavailable",
|
|
)
|
|
|
|
if (
|
|
len(data) != expected_size
|
|
or hashlib.sha256(data).hexdigest()
|
|
!= expected_sha256
|
|
):
|
|
fail(
|
|
503,
|
|
"unavailable",
|
|
"Stored object is unavailable",
|
|
)
|
|
|
|
return Response(
|
|
content=data,
|
|
media_type=content_type,
|
|
headers={
|
|
"Cache-Control": "private, no-store",
|
|
"X-Content-Type-Options": "nosniff",
|
|
},
|
|
)
|
|
|
|
def get_idempotency_key(request):
|
|
key = request.headers.get("Idempotency-Key")
|
|
|
|
if (
|
|
key is None
|
|
or not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key)
|
|
):
|
|
fail(
|
|
400,
|
|
"invalid_request",
|
|
"Invalid or missing Idempotency-Key"
|
|
)
|
|
|
|
return key
|
|
|
|
|
|
def request_fingerprint(data):
|
|
normalized = {
|
|
"text": data["text"],
|
|
"attachment_ids": data.get("attachment_ids", []),
|
|
}
|
|
|
|
encoded = json.dumps(
|
|
normalized,
|
|
ensure_ascii=False,
|
|
sort_keys=True,
|
|
separators=(",", ":"),
|
|
).encode("utf-8")
|
|
|
|
return hashlib.sha256(encoded).hexdigest()
|
|
|
|
|
|
|
|
def idempotency_lock_id(user_id, chat_id, key):
|
|
scope = f"{user_id}:{chat_id}:{key}"
|
|
|
|
digest = hashlib.sha256(
|
|
scope.encode("utf-8")
|
|
).digest()
|
|
|
|
return int.from_bytes(
|
|
digest[:8],
|
|
byteorder="big",
|
|
signed=True
|
|
)
|
|
|
|
|
|
# ---------- Messages ----------
|
|
|
|
|
|
|
|
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
|
async def send_message(chat_id: str, request: Request):
|
|
actor = current_user(request)
|
|
|
|
data = await json_body(
|
|
request,
|
|
required=["text"],
|
|
allowed=["text", "attachment_ids"],
|
|
)
|
|
|
|
message_text = data["text"]
|
|
attachment_ids = data.get("attachment_ids", [])
|
|
|
|
if (
|
|
not isinstance(message_text, str)
|
|
or len(message_text) > 4000
|
|
):
|
|
fail(400, "invalid_request", "Invalid message text")
|
|
|
|
if (
|
|
not isinstance(attachment_ids, list)
|
|
or len(attachment_ids) > 10
|
|
or not all(valid_uuid(x) for x in attachment_ids)
|
|
or len(attachment_ids) != len(set(attachment_ids))
|
|
):
|
|
fail(400, "invalid_request", "Invalid attachment_ids")
|
|
|
|
if not message_text.strip() and not attachment_ids:
|
|
fail(400, "invalid_request", "Empty message")
|
|
|
|
key = get_idempotency_key(request)
|
|
fingerprint = request_fingerprint(data)
|
|
user_id = actor["id"]
|
|
|
|
with db() as conn:
|
|
accessible_chat(conn, chat_id, user_id)
|
|
|
|
lock_id = idempotency_lock_id(
|
|
user_id, chat_id, key
|
|
)
|
|
|
|
conn.execute(
|
|
"SELECT pg_advisory_xact_lock(%s)",
|
|
(lock_id,),
|
|
)
|
|
|
|
conn.execute(
|
|
"""
|
|
DELETE FROM message_idempotency
|
|
WHERE user_id = %s
|
|
AND chat_id = %s
|
|
AND idempotency_key = %s
|
|
AND created_at <
|
|
clock_timestamp() - INTERVAL '24 hours'
|
|
""",
|
|
(user_id, chat_id, key),
|
|
)
|
|
|
|
previous = conn.execute(
|
|
"""
|
|
SELECT i.request_hash, m.*
|
|
FROM message_idempotency i
|
|
JOIN messages m ON m.id = i.message_id
|
|
WHERE i.user_id = %s
|
|
AND i.chat_id = %s
|
|
AND i.idempotency_key = %s
|
|
""",
|
|
(user_id, chat_id, key),
|
|
).fetchone()
|
|
|
|
if previous is not None:
|
|
if previous["request_hash"] != fingerprint:
|
|
fail(
|
|
409,
|
|
"conflict",
|
|
"Idempotency-Key already used "
|
|
"with different content",
|
|
)
|
|
|
|
return message_json(previous, conn)
|
|
|
|
for attachment_id in attachment_ids:
|
|
attachment = conn.execute(
|
|
"""
|
|
SELECT id, status
|
|
FROM attachments
|
|
WHERE id = %s
|
|
AND chat_id = %s
|
|
FOR KEY SHARE
|
|
""",
|
|
(attachment_id, chat_id),
|
|
).fetchone()
|
|
|
|
if attachment is None:
|
|
fail(
|
|
404,
|
|
"not_found",
|
|
"Attachment not found",
|
|
)
|
|
|
|
if attachment["status"] == "failed":
|
|
fail(
|
|
409,
|
|
"conflict",
|
|
"Attachment processing failed",
|
|
)
|
|
|
|
if attachment["status"] not in (
|
|
"queued",
|
|
"processing",
|
|
"ready",
|
|
):
|
|
fail(
|
|
409,
|
|
"conflict",
|
|
"Invalid attachment status",
|
|
)
|
|
|
|
message_id = str(uuid.uuid4())
|
|
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO messages
|
|
(id, chat_id, sender_id, text)
|
|
VALUES (%s, %s, %s, %s)
|
|
RETURNING id, chat_id, sender_id,
|
|
text, created_at
|
|
""",
|
|
(message_id, chat_id, user_id, message_text),
|
|
).fetchone()
|
|
|
|
for attachment_id in attachment_ids:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO message_attachments
|
|
(message_id, attachment_id)
|
|
VALUES (%s, %s)
|
|
""",
|
|
(message_id, attachment_id),
|
|
)
|
|
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO message_idempotency
|
|
(
|
|
user_id, chat_id, idempotency_key,
|
|
request_hash, message_id
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
""",
|
|
(
|
|
user_id, chat_id, key,
|
|
fingerprint, message_id,
|
|
),
|
|
)
|
|
|
|
return message_json(row, conn)
|
|
|
|
|
|
|
|
@app.get("/api/v1/chats/{chat_id}/messages")
|
|
def list_messages(chat_id: str, request: Request):
|
|
actor = current_user(request)
|
|
limit = validate_limit(request)
|
|
cursor = request.query_params.get("cursor")
|
|
|
|
resource = "messages:" + chat_id
|
|
|
|
with db() as conn:
|
|
accessible_chat(conn, chat_id, actor["id"])
|
|
|
|
if cursor 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_id, limit + 1)
|
|
).fetchall()
|
|
else:
|
|
timestamp, last_id = read_cursor(cursor, resource)
|
|
|
|
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_id, timestamp, last_id, limit + 1)
|
|
).fetchall()
|
|
|
|
has_more = len(rows) > limit
|
|
page = rows[:limit]
|
|
|
|
next_cursor = (
|
|
make_cursor(resource, page[-1])
|
|
if has_more else None
|
|
)
|
|
|
|
items = [message_json(row, conn) for row in page]
|
|
|
|
return {"items": items, "next_cursor": next_cursor}
|