forked from mtusi2026/mtusi2026lab2
Implement lab2 PostgreSQL authentication and Redis sessions
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import os
|
||||
import uvicorn
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"app.main:app",
|
||||
host=os.getenv("APP_HOST", "0.0.0.0"),
|
||||
port=int(os.getenv("APP_PORT", "8080")),
|
||||
workers=1,
|
||||
access_log=False,
|
||||
)
|
||||
+493
@@ -0,0 +1,493 @@
|
||||
"""Lab 2: stateless HTTP API, PostgreSQL source of truth, durable Redis sessions."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import secrets
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any
|
||||
|
||||
import psycopg
|
||||
import redis
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
from fastapi import FastAPI, Query, Request
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse, Response
|
||||
from psycopg.errors import UniqueViolation
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool, PoolTimeout
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
logging.basicConfig(level=os.environ.get("LOG_LEVEL", "INFO"), format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
||||
logger = logging.getLogger("messenger")
|
||||
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}\Z")
|
||||
ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1) # Argon2id; bounded 19 MiB/KDF.
|
||||
# Dummy KDF verification on unknown users reduces username-enumeration timing leakage.
|
||||
DUMMY_HASH = ph.hash("lab2-dummy-password-not-for-login")
|
||||
|
||||
|
||||
def iso(dt: datetime) -> str:
|
||||
return dt.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
def __init__(self, status: int, code: str, message: str):
|
||||
super().__init__(message)
|
||||
self.status, self.code, self.message = status, code, message
|
||||
|
||||
|
||||
class StrictModel(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
|
||||
class CreateUser(StrictModel):
|
||||
username: str
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
password: str = Field(min_length=12, max_length=128)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def check_username(cls, v: str) -> str:
|
||||
if not USERNAME_RE.fullmatch(v):
|
||||
raise ValueError("invalid username")
|
||||
return v
|
||||
|
||||
@field_validator("display_name", "password")
|
||||
@classmethod
|
||||
def non_blank(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("must not be blank")
|
||||
try:
|
||||
v.encode("utf-8")
|
||||
except UnicodeEncodeError as exc:
|
||||
raise ValueError("must be valid UTF-8") from exc
|
||||
return v
|
||||
|
||||
|
||||
class CreateChat(StrictModel):
|
||||
title: str = Field(min_length=1, max_length=100)
|
||||
member_ids: list[str] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def non_blank(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("blank title")
|
||||
try:
|
||||
v.encode("utf-8")
|
||||
except UnicodeEncodeError as exc:
|
||||
raise ValueError("must be valid UTF-8") from exc
|
||||
return v
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def unique(cls, v: list[str]) -> list[str]:
|
||||
if len(v) != len(set(v)):
|
||||
raise ValueError("duplicate members")
|
||||
return v
|
||||
|
||||
|
||||
class CreateMessage(StrictModel):
|
||||
text: str = Field(min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def non_blank(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("blank message")
|
||||
try:
|
||||
v.encode("utf-8")
|
||||
except UnicodeEncodeError as exc:
|
||||
raise ValueError("must be valid UTF-8") from exc
|
||||
return v
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
url = os.environ.get("DATABASE_URL")
|
||||
if not url:
|
||||
raise RuntimeError("DATABASE_URL must be provided (do not commit credentials)")
|
||||
pool = ConnectionPool(url, min_size=1, max_size=int(os.environ.get("DB_POOL_MAX", "8")),
|
||||
timeout=3, max_lifetime=1800,
|
||||
kwargs={"row_factory": dict_row, "connect_timeout": 5, "options": "-c statement_timeout=5000"}, open=False)
|
||||
pool.open(wait=False)
|
||||
app.state.pool = pool
|
||||
app.state.sessions = redis.Redis.from_url(os.environ.get("REDIS_URL", "redis://redis:6379/0"),
|
||||
socket_connect_timeout=2, socket_timeout=2,
|
||||
health_check_interval=10, decode_responses=True)
|
||||
ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
|
||||
if ttl < 1:
|
||||
raise RuntimeError("SESSION_TTL_SECONDS must be >= 1")
|
||||
app.state.session_ttl = ttl
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
app.state.sessions.close()
|
||||
pool.close()
|
||||
|
||||
|
||||
app = FastAPI(title="MTUCI Messenger Lab 2", docs_url=None, redoc_url=None, openapi_url=None, lifespan=lifespan)
|
||||
|
||||
|
||||
def reqid(request: Request) -> str:
|
||||
return getattr(request.state, "request_id", "unknown")
|
||||
|
||||
|
||||
def error_response(request: Request, status: int, code: str, message: str) -> JSONResponse:
|
||||
result = JSONResponse(status_code=status, content={"error": {"code": code, "message": message, "request_id": reqid(request)}})
|
||||
if status == 401:
|
||||
scheme = 'Basic' if request.url.path == "/api/v1/auth/sessions" else 'Bearer'
|
||||
result.headers["WWW-Authenticate"] = (f'Basic realm="messenger", charset="UTF-8"' if scheme == 'Basic' else 'Bearer realm="messenger"')
|
||||
return result
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_context(request: Request, call_next):
|
||||
request.state.request_id = uuid.uuid4().hex
|
||||
logger.info("request start id=%s method=%s path=%s", reqid(request), request.method, request.url.path)
|
||||
try:
|
||||
path = request.url.path
|
||||
if path == "/api/v1/users/me" or path.startswith("/api/v1/chats") or path == "/api/v1/auth/sessions/current":
|
||||
# Authentication precedes resource and body validation.
|
||||
request.state.actor = await run_in_threadpool(_authenticate, request)
|
||||
if request.method == "POST" and (path in ("/api/v1/users", "/api/v1/chats") or (path.startswith("/api/v1/chats/") and path.endswith("/messages"))):
|
||||
mime = request.headers.get("content-type", "").split(";", 1)[0].strip().lower()
|
||||
if mime != "application/json":
|
||||
raise APIError(415, "unsupported_media_type", "Content-Type must be application/json")
|
||||
response = await call_next(request)
|
||||
except APIError as exc:
|
||||
response = error_response(request, exc.status, exc.code, exc.message)
|
||||
except (psycopg.Error, PoolTimeout, redis.RedisError):
|
||||
logger.warning("dependency unavailable request=%s", reqid(request))
|
||||
response = error_response(request, 503, "unavailable", "Required dependency unavailable")
|
||||
except Exception:
|
||||
logger.exception("unexpected request error id=%s", reqid(request))
|
||||
response = error_response(request, 500, "internal_error", "Internal server error")
|
||||
response.headers["X-Request-Id"] = reqid(request)
|
||||
logger.info("request end id=%s status=%s", reqid(request), response.status_code)
|
||||
return response
|
||||
|
||||
|
||||
@app.exception_handler(APIError)
|
||||
async def api_error_handler(request: Request, exc: APIError):
|
||||
return error_response(request, exc.status, exc.code, exc.message)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_error_handler(request: Request, exc: RequestValidationError):
|
||||
return error_response(request, 400, "invalid_request", "Invalid request")
|
||||
|
||||
|
||||
@app.exception_handler(psycopg.Error)
|
||||
async def db_error_handler(request: Request, exc: psycopg.Error):
|
||||
logger.warning("database error request=%s class=%s", reqid(request), type(exc).__name__)
|
||||
return error_response(request, 503, "unavailable", "Required dependency unavailable")
|
||||
|
||||
|
||||
@app.exception_handler(PoolTimeout)
|
||||
async def pool_error_handler(request: Request, exc: PoolTimeout):
|
||||
return error_response(request, 503, "unavailable", "Required dependency unavailable")
|
||||
|
||||
|
||||
@app.exception_handler(redis.RedisError)
|
||||
async def redis_error_handler(request: Request, exc: redis.RedisError):
|
||||
return error_response(request, 503, "unavailable", "Required dependency unavailable")
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unexpected_handler(request: Request, exc: Exception):
|
||||
logger.exception("unexpected application error request=%s", reqid(request))
|
||||
return error_response(request, 500, "internal_error", "Internal server error")
|
||||
|
||||
|
||||
def valid_uuid(raw: str, name: str = "id") -> uuid.UUID:
|
||||
try:
|
||||
return uuid.UUID(raw)
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
raise APIError(400, "invalid_request", f"Invalid {name}")
|
||||
|
||||
|
||||
def limit_value(raw: str | None) -> int:
|
||||
if raw is None:
|
||||
return 50
|
||||
if not re.fullmatch(r"[0-9]+", raw):
|
||||
raise APIError(400, "invalid_request", "Invalid limit")
|
||||
n = int(raw)
|
||||
if n < 1 or n > 100:
|
||||
raise APIError(400, "invalid_request", "Invalid limit")
|
||||
return n
|
||||
|
||||
|
||||
def from_db_utf8(value: bytes | memoryview) -> str:
|
||||
"""Decode PostgreSQL bytea; unlike text/varchar it can contain U+0000.
|
||||
|
||||
The HTTP contract allows the full UTF-8 string range, including an escaped
|
||||
JSON NUL. Pydantic checks Unicode code-point lengths before encoding;
|
||||
the database enforces a conservative upper bound on byte length.
|
||||
"""
|
||||
return bytes(value).decode("utf-8")
|
||||
|
||||
|
||||
def user_public(row: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"id": str(row["id"]), "username": row["username"], "display_name": from_db_utf8(row["display_name"]), "created_at": iso(row["created_at"])}
|
||||
|
||||
|
||||
def chat_public(row: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"id": str(row["id"]), "title": from_db_utf8(row["title"]), "member_ids": [str(x) for x in row["member_ids"]], "created_at": iso(row["created_at"])}
|
||||
|
||||
|
||||
def message_public(row: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), "text": from_db_utf8(row["text"]), "created_at": iso(row["created_at"])}
|
||||
|
||||
|
||||
def _authenticate(request: Request) -> dict[str, Any]:
|
||||
auth = request.headers.get("authorization", "")
|
||||
parts = auth.split(" ", 1)
|
||||
if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1] or not re.fullmatch(r"[A-Za-z0-9_-]+", parts[1]):
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
token = parts[1]
|
||||
digest = hashlib.sha256(token.encode("ascii")).hexdigest()
|
||||
redis_key = "session:" + digest
|
||||
user_id = request.app.state.sessions.get(redis_key)
|
||||
if user_id is None:
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
try:
|
||||
session_uid = uuid.UUID(user_id)
|
||||
except (ValueError, AttributeError, TypeError):
|
||||
# Corrupted Redis contents are not an authenticated identity. Fail
|
||||
# closed and delete only the broken key, never invent a user or emit 500.
|
||||
request.app.state.sessions.delete(redis_key)
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
with request.app.state.pool.connection() as conn:
|
||||
row = conn.execute("SELECT id, username, display_name, created_at FROM users WHERE id = %s", (session_uid,)).fetchone()
|
||||
if row is None:
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
actor = user_public(row)
|
||||
request.state.session_digest = digest
|
||||
return actor
|
||||
|
||||
|
||||
def actor(request: Request) -> dict[str, Any]:
|
||||
found = getattr(request.state, "actor", None)
|
||||
return found if found is not None else _authenticate(request)
|
||||
|
||||
|
||||
def basic_credentials(request: Request) -> tuple[str, str]:
|
||||
auth = request.headers.get("authorization", "")
|
||||
parts = auth.split(" ", 1)
|
||||
if len(parts) != 2 or parts[0].lower() != "basic":
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
try:
|
||||
raw = base64.b64decode(parts[1], validate=True).decode("utf-8")
|
||||
username, password = raw.split(":", 1)
|
||||
except (ValueError, UnicodeError, binascii.Error):
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
if not USERNAME_RE.fullmatch(username) or not password:
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
return username, password
|
||||
|
||||
|
||||
def cursor_encode(payload: dict[str, str]) -> str:
|
||||
return base64.urlsafe_b64encode(json.dumps(payload, separators=(",", ":")).encode("ascii")).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def cursor_decode(raw: str | None, *, kind: str, scope_key: str, scope_id: str) -> tuple[datetime, uuid.UUID] | None:
|
||||
if raw is None:
|
||||
return None
|
||||
try:
|
||||
if not raw or not re.fullmatch(r"[A-Za-z0-9_-]+", raw) or len(raw) > 1500:
|
||||
raise ValueError("bad cursor")
|
||||
value = json.loads(base64.urlsafe_b64decode(raw + ("=" * (-len(raw) % 4))))
|
||||
if not isinstance(value, dict) or set(value) != {"kind", scope_key, "created_at", "id"}:
|
||||
raise ValueError("bad cursor fields")
|
||||
if value["kind"] != kind or value[scope_key] != scope_id:
|
||||
raise ValueError("wrong cursor context")
|
||||
stamp = value["created_at"]
|
||||
if not isinstance(stamp, str) or not stamp.endswith("Z"):
|
||||
raise ValueError("invalid timestamp")
|
||||
when = datetime.fromisoformat(stamp.replace("Z", "+00:00"))
|
||||
if when.utcoffset() != timedelta(0):
|
||||
raise ValueError("not UTC")
|
||||
return when, uuid.UUID(value["id"])
|
||||
except (ValueError, TypeError, KeyError, UnicodeError, binascii.Error):
|
||||
raise APIError(400, "invalid_request", "Invalid cursor")
|
||||
|
||||
|
||||
def cursor_next(kind: str, scope_key: str, scope_id: str, row: dict[str, Any]) -> str:
|
||||
return cursor_encode({"kind": kind, scope_key: scope_id, "created_at": iso(row["created_at"]), "id": str(row["id"])})
|
||||
|
||||
|
||||
def read_chat(conn: psycopg.Connection, cid: uuid.UUID, uid: uuid.UUID) -> dict[str, Any]:
|
||||
row = conn.execute("""
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT cm2.user_id FROM chat_members cm2 WHERE cm2.chat_id=c.id ORDER BY cm2.position) AS member_ids
|
||||
FROM chats c JOIN chat_members mine ON mine.chat_id=c.id
|
||||
WHERE c.id=%s AND mine.user_id=%s
|
||||
""", (cid, uid)).fetchone()
|
||||
if row is None:
|
||||
raise APIError(404, "not_found", "Object not found")
|
||||
return row
|
||||
|
||||
|
||||
@app.get("/health/live")
|
||||
def live():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
def ready(request: Request):
|
||||
with request.app.state.pool.connection() as conn:
|
||||
conn.execute("SELECT 1")
|
||||
request.app.state.sessions.ping()
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.post("/api/v1/users", status_code=201)
|
||||
def create_user(body: CreateUser, request: Request):
|
||||
new_id = uuid.uuid4()
|
||||
hashed = ph.hash(body.password)
|
||||
try:
|
||||
with request.app.state.pool.connection() 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
|
||||
""", (new_id, body.username, body.display_name.encode("utf-8"), hashed)).fetchone()
|
||||
except UniqueViolation:
|
||||
raise APIError(409, "conflict", "Username already exists")
|
||||
return user_public(row)
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||||
def create_session(request: Request):
|
||||
# Contract: this endpoint uses Basic credentials only, never a JSON body.
|
||||
if request.headers.get("content-length") not in (None, "", "0") or request.headers.get("transfer-encoding"):
|
||||
raise APIError(400, "invalid_request", "Request body is not supported")
|
||||
username, password = basic_credentials(request)
|
||||
with request.app.state.pool.connection() as conn:
|
||||
row = conn.execute("SELECT id, username, display_name, created_at, password_hash FROM users WHERE username=%s", (username,)).fetchone()
|
||||
try:
|
||||
is_valid = ph.verify(row["password_hash"] if row else DUMMY_HASH, password)
|
||||
except (VerificationError, InvalidHashError):
|
||||
is_valid = False
|
||||
if row is None or not is_valid:
|
||||
raise APIError(401, "unauthorized", "Unauthorized")
|
||||
token = secrets.token_urlsafe(32) # 32 cryptographically random bytes (256 bits).
|
||||
digest = hashlib.sha256(token.encode("ascii")).hexdigest()
|
||||
ttl = request.app.state.session_ttl
|
||||
request.app.state.sessions.set("session:" + digest, str(row["id"]), ex=ttl)
|
||||
expiry = datetime.now(timezone.utc) + timedelta(seconds=ttl)
|
||||
return {"token": token, "token_type": "Bearer", "expires_at": iso(expiry), "user": user_public(row)}
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
def logout(request: Request):
|
||||
actor(request)
|
||||
request.app.state.sessions.delete("session:" + request.state.session_digest)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
def users_me(request: Request):
|
||||
return actor(request)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats", status_code=201)
|
||||
def create_chat(request: Request, body: CreateChat):
|
||||
uid = uuid.UUID(actor(request)["id"])
|
||||
members = [valid_uuid(raw, "member_id") for raw in body.member_ids]
|
||||
if len(set(members)) != len(members) or uid not in members:
|
||||
raise APIError(400, "invalid_request", "Invalid chat members")
|
||||
cid = uuid.uuid4()
|
||||
with request.app.state.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
existing = conn.execute("SELECT id FROM users WHERE id=ANY(%s)", (members,)).fetchall()
|
||||
if len(existing) != len(members):
|
||||
raise APIError(400, "invalid_request", "Unknown member")
|
||||
row = conn.execute("INSERT INTO chats(id, title) VALUES (%s,%s) RETURNING id,title,created_at", (cid, body.title.encode("utf-8"))).fetchone()
|
||||
for index, member in enumerate(members):
|
||||
conn.execute("INSERT INTO chat_members(chat_id,user_id,position) VALUES (%s,%s,%s)", (cid, member, index))
|
||||
return chat_public({**row, "member_ids": members})
|
||||
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
def list_chats(request: Request, limit: str | None = Query(default=None), cursor: str | None = Query(default=None)):
|
||||
uid = uuid.UUID(actor(request)["id"])
|
||||
size = limit_value(limit)
|
||||
key = cursor_decode(cursor, kind="chats", scope_key="user_id", scope_id=str(uid))
|
||||
with request.app.state.pool.connection() as conn:
|
||||
if key:
|
||||
match = conn.execute("""
|
||||
SELECT 1 FROM chats c JOIN chat_members cm ON c.id=cm.chat_id
|
||||
WHERE cm.user_id=%s AND c.id=%s AND c.created_at=%s
|
||||
""", (uid, key[1], key[0])).fetchone()
|
||||
if match is None:
|
||||
raise APIError(400, "invalid_request", "Invalid cursor")
|
||||
query = """
|
||||
SELECT c.id,c.title,c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id=c.id ORDER BY m.position) AS member_ids
|
||||
FROM chats c JOIN chat_members cm ON cm.chat_id=c.id
|
||||
WHERE cm.user_id=%s
|
||||
"""
|
||||
params: list[Any] = [uid]
|
||||
if key:
|
||||
query += " AND (c.created_at,c.id)>(%s,%s)"
|
||||
params.extend(key)
|
||||
query += " ORDER BY c.created_at,c.id LIMIT %s"
|
||||
rows = conn.execute(query, [*params, size+1]).fetchall()
|
||||
page = rows[:size]
|
||||
return {"items": [chat_public(r) for r in page], "next_cursor": cursor_next("chats", "user_id", str(uid), page[-1]) if len(rows)>size else None}
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(chat_id: str, request: Request):
|
||||
uid = uuid.UUID(actor(request)["id"])
|
||||
with request.app.state.pool.connection() as conn:
|
||||
row = read_chat(conn, valid_uuid(chat_id, "chat_id"), uid)
|
||||
return chat_public(row)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
def create_message(chat_id: str, request: Request, body: CreateMessage):
|
||||
uid = uuid.UUID(actor(request)["id"])
|
||||
cid = valid_uuid(chat_id, "chat_id")
|
||||
with request.app.state.pool.connection() as conn:
|
||||
read_chat(conn, cid, uid)
|
||||
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
|
||||
""", (uuid.uuid4(), cid, uid, body.text.encode("utf-8"))).fetchone()
|
||||
return message_public(row)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
def list_messages(chat_id: str, request: Request, limit: str | None = Query(default=None), cursor: str | None = Query(default=None)):
|
||||
uid = uuid.UUID(actor(request)["id"])
|
||||
cid = valid_uuid(chat_id, "chat_id")
|
||||
size = limit_value(limit)
|
||||
key = cursor_decode(cursor, kind="messages", scope_key="chat_id", scope_id=str(cid))
|
||||
with request.app.state.pool.connection() as conn:
|
||||
read_chat(conn, cid, uid)
|
||||
if key:
|
||||
found = conn.execute("SELECT 1 FROM messages WHERE chat_id=%s AND id=%s AND created_at=%s", (cid, key[1], key[0])).fetchone()
|
||||
if not found:
|
||||
raise APIError(400, "invalid_request", "Invalid cursor")
|
||||
query = """
|
||||
SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s
|
||||
"""
|
||||
params: list[Any] = [cid]
|
||||
if key:
|
||||
query += " AND (created_at,id)>(%s,%s)"
|
||||
params.extend(key)
|
||||
query += " ORDER BY created_at,id LIMIT %s"
|
||||
rows = conn.execute(query, [*params, size+1]).fetchall()
|
||||
page = rows[:size]
|
||||
return {"items": [message_public(r) for r in page], "next_cursor": cursor_next("messages", "chat_id", str(cid), page[-1]) if len(rows)>size else None}
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Versioned transactional SQL migration runner, safe to rerun."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import psycopg
|
||||
|
||||
|
||||
def main():
|
||||
folder = Path(__file__).resolve().parent.parent / 'db' / 'migrations'
|
||||
files = sorted(folder.glob('*.sql'))
|
||||
if not files:
|
||||
raise RuntimeError('no migrations')
|
||||
with psycopg.connect(os.environ['DATABASE_URL'], connect_timeout=5) as conn:
|
||||
with conn.transaction():
|
||||
# Serialize migration runners and all schema changes in the transaction.
|
||||
conn.execute('SELECT pg_advisory_xact_lock(12803802)')
|
||||
conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())')
|
||||
applied = {r[0] for r in conn.execute('SELECT version FROM schema_migrations')}
|
||||
if any(name not in {f.name for f in files} for name in applied):
|
||||
raise RuntimeError('database contains unknown migration version')
|
||||
for file in files:
|
||||
if file.name in applied:
|
||||
continue
|
||||
conn.execute(file.read_text(encoding='utf-8'))
|
||||
conn.execute('INSERT INTO schema_migrations(version) VALUES (%s)', (file.name,))
|
||||
print('Applied migration', file.name)
|
||||
print('Migrations up to date')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user