forked from mtusi2026/mtusi2026lab7
Перенести реализацию мессенджера из lab6 в skeleton lab7.
Контракт, публичные тесты и README задания оставлены от lab7. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+523
@@ -0,0 +1,523 @@
|
||||
import base64
|
||||
import binascii
|
||||
import re
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse, Response
|
||||
|
||||
from app import attachments, cache, config, db, direct, images, jobs, rediscli, s3, sessions, store
|
||||
from app.cursor_codec import decode_cursor, encode_cursor
|
||||
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
|
||||
from app.passwords import dummy_hash, hash_password, verify_password
|
||||
from app.validation import (
|
||||
parse_json_body,
|
||||
require_exact_keys,
|
||||
parse_attachment_ids,
|
||||
validate_display_name,
|
||||
validate_filename,
|
||||
validate_limit,
|
||||
validate_idempotency_key,
|
||||
validate_message_text,
|
||||
validate_password,
|
||||
validate_title,
|
||||
validate_username,
|
||||
validate_uuid,
|
||||
)
|
||||
|
||||
|
||||
def _json(data: dict, request_id: str, status: int = 200) -> JSONResponse:
|
||||
return JSONResponse(status_code=status, headers={"X-Request-Id": request_id}, content=data)
|
||||
|
||||
|
||||
def _empty(request_id: str) -> Response:
|
||||
return Response(status_code=204, headers={"X-Request-Id": request_id})
|
||||
|
||||
|
||||
async def _bearer_user(request: Request) -> dict:
|
||||
header = request.headers.get("Authorization") or ""
|
||||
scheme, _, rest = header.partition(" ")
|
||||
if scheme.lower() != "bearer" or not rest.strip():
|
||||
raise AppError(401, "authentication required", BEARER_CHALLENGE)
|
||||
user_id = str(await sessions.resolve(rest.strip()))
|
||||
user = await store.get_user(user_id)
|
||||
if user is None:
|
||||
raise AppError(401, "authentication required", BEARER_CHALLENGE)
|
||||
return user
|
||||
|
||||
|
||||
async def health_live(request: Request) -> Response:
|
||||
return _json({"status": "ok"}, request.state.request_id)
|
||||
|
||||
|
||||
async def health_ready(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if request.app.state.shutting_down:
|
||||
return error_response(503, "service is shutting down", rid)
|
||||
await db.ping()
|
||||
await rediscli.ping()
|
||||
await s3.head_bucket()
|
||||
return _json({"status": "ok"}, rid)
|
||||
|
||||
|
||||
async def register_user(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if request.headers.get("content-type", "").split(";")[0].strip().lower() not in ("application/json", ""):
|
||||
if request.headers.get("content-type"):
|
||||
return error_response(415, "unsupported media type", rid)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, rid)
|
||||
assert body is not None
|
||||
if require_exact_keys(body, {"username", "display_name", "password"}, {"username", "display_name", "password"}):
|
||||
return error_response(400, "invalid user payload", rid)
|
||||
if validate_username(body["username"]) or validate_display_name(body["display_name"]) or validate_password(body["password"]):
|
||||
return error_response(400, "invalid user fields", rid)
|
||||
user, conflict = await store.create_user(body["username"], body["display_name"], hash_password(body["password"]))
|
||||
if conflict:
|
||||
return error_response(409, "username already taken", rid)
|
||||
assert user is not None
|
||||
return _json(user, rid, 201)
|
||||
|
||||
|
||||
async def current_user(request: Request) -> Response:
|
||||
user = await _bearer_user(request)
|
||||
return _json(user, request.state.request_id)
|
||||
|
||||
|
||||
async def create_session(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
header = request.headers.get("Authorization") or ""
|
||||
scheme, _, rest = header.partition(" ")
|
||||
if scheme.lower() != "basic" or not rest.strip():
|
||||
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
|
||||
try:
|
||||
decoded = base64.b64decode(rest.strip(), validate=True).decode("utf-8")
|
||||
except (binascii.Error, UnicodeError, ValueError):
|
||||
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
|
||||
username, sep, password = decoded.partition(":")
|
||||
if not sep:
|
||||
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
|
||||
row = await store.get_user_by_username(username)
|
||||
password_hash = row["password_hash"] if row else dummy_hash()
|
||||
if row is None or not verify_password(password_hash, password):
|
||||
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
|
||||
token, expires_at = await sessions.create(row["id"])
|
||||
return _json(
|
||||
{
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": expires_at,
|
||||
"user": store.user_dict(row),
|
||||
},
|
||||
rid,
|
||||
201,
|
||||
)
|
||||
|
||||
|
||||
async def revoke_session(request: Request) -> Response:
|
||||
header = request.headers.get("Authorization") or ""
|
||||
scheme, _, rest = header.partition(" ")
|
||||
if scheme.lower() != "bearer" or not rest.strip():
|
||||
raise AppError(401, "authentication required", BEARER_CHALLENGE)
|
||||
await sessions.revoke(rest.strip())
|
||||
return _empty(request.state.request_id)
|
||||
|
||||
|
||||
async def create_chat(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
ct = request.headers.get("content-type", "").split(";")[0].strip().lower()
|
||||
if ct and ct != "application/json":
|
||||
return error_response(415, "unsupported media type", rid)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, rid)
|
||||
assert body is not None
|
||||
if require_exact_keys(body, {"title", "member_ids"}, {"title", "member_ids"}):
|
||||
return error_response(400, "invalid chat payload", rid)
|
||||
if validate_title(body["title"]):
|
||||
return error_response(400, "invalid title", rid)
|
||||
members = body["member_ids"]
|
||||
if not isinstance(members, list) or not members:
|
||||
return error_response(400, "invalid member_ids", rid)
|
||||
if len(members) > 100:
|
||||
return error_response(400, "too many members", rid)
|
||||
for mid in members:
|
||||
if validate_uuid(mid):
|
||||
return error_response(400, "invalid member id", rid)
|
||||
chat, create_err = await store.create_chat(body["title"], members, user["id"])
|
||||
if create_err == "caller not in members":
|
||||
return error_response(400, "caller must be a member", rid)
|
||||
if create_err in ("duplicate members", "unknown member"):
|
||||
return error_response(400, "invalid members", rid)
|
||||
assert chat is not None
|
||||
return _json(chat, rid, 201)
|
||||
|
||||
|
||||
async def list_chats(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
limit, limit_err = validate_limit(request.query_params.get("limit"))
|
||||
if limit_err:
|
||||
return error_response(400, limit_err, rid)
|
||||
assert limit is not None
|
||||
cursor_raw = request.query_params.get("cursor")
|
||||
after = None
|
||||
if cursor_raw is not None:
|
||||
if not cursor_raw.strip():
|
||||
return error_response(400, "invalid cursor", rid)
|
||||
try:
|
||||
created_at, entity_id = decode_cursor(cursor_raw, user["id"])
|
||||
except ValueError:
|
||||
return error_response(400, "invalid cursor", rid)
|
||||
after = (created_at, entity_id)
|
||||
items, next_key = await store.list_chats(user["id"], limit, after)
|
||||
next_cursor = None
|
||||
if next_key is not None:
|
||||
next_cursor = encode_cursor(user["id"], next_key[0], next_key[1])
|
||||
return _json({"items": items, "next_cursor": next_cursor}, rid)
|
||||
|
||||
|
||||
async def get_chat(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
chat_id = request.path_params["chat_id"]
|
||||
if validate_uuid(chat_id):
|
||||
return error_response(400, "invalid chat id", rid)
|
||||
chat = await store.get_chat(chat_id, user["id"])
|
||||
if chat is None:
|
||||
return error_response(404, "chat not found", rid)
|
||||
return _json(chat, rid)
|
||||
|
||||
|
||||
async def create_message(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
chat_id = request.path_params["chat_id"]
|
||||
if validate_uuid(chat_id):
|
||||
return error_response(400, "invalid chat id", rid)
|
||||
ct = request.headers.get("content-type", "").split(";")[0].strip().lower()
|
||||
if ct and ct != "application/json":
|
||||
return error_response(415, "unsupported media type", rid)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, rid)
|
||||
assert body is not None
|
||||
if require_exact_keys(body, {"text", "attachment_ids"}, {"text"}):
|
||||
return error_response(400, "invalid message payload", rid)
|
||||
attachment_ids, attachment_err = parse_attachment_ids(body.get("attachment_ids"))
|
||||
if attachment_err:
|
||||
return error_response(400, attachment_err, rid)
|
||||
assert attachment_ids is not None
|
||||
if validate_message_text(body["text"], allow_blank=bool(attachment_ids)):
|
||||
return error_response(400, "invalid message text", rid)
|
||||
if validate_idempotency_key(request.headers.get("Idempotency-Key")):
|
||||
return error_response(400, "invalid idempotency key", rid)
|
||||
message, msg_err = await store.add_message(
|
||||
chat_id,
|
||||
user["id"],
|
||||
body["text"],
|
||||
request.headers.get("Idempotency-Key") or "",
|
||||
request.url.path,
|
||||
attachment_ids,
|
||||
)
|
||||
if msg_err == "not_found":
|
||||
return error_response(404, "chat not found", rid)
|
||||
if msg_err == "conflict":
|
||||
return error_response(409, "idempotency key reused with a different body", rid)
|
||||
if msg_err == "not_ready":
|
||||
return error_response(409, "attachment is not ready", rid)
|
||||
assert message is not None
|
||||
return _json(message, rid, 201)
|
||||
|
||||
|
||||
async def list_messages(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
chat_id = request.path_params["chat_id"]
|
||||
if validate_uuid(chat_id):
|
||||
return error_response(400, "invalid chat id", rid)
|
||||
limit, limit_err = validate_limit(request.query_params.get("limit"))
|
||||
if limit_err:
|
||||
return error_response(400, limit_err, rid)
|
||||
assert limit is not None
|
||||
cursor_raw = request.query_params.get("cursor")
|
||||
after = None
|
||||
if cursor_raw is not None:
|
||||
if not cursor_raw.strip():
|
||||
return error_response(400, "invalid cursor", rid)
|
||||
try:
|
||||
created_at, entity_id = decode_cursor(cursor_raw, chat_id)
|
||||
except ValueError:
|
||||
return error_response(400, "invalid cursor", rid)
|
||||
after = (created_at, entity_id)
|
||||
page, next_key, list_err = await store.list_messages(chat_id, user["id"], limit, after)
|
||||
if list_err == "not_found":
|
||||
return error_response(404, "chat not found", rid)
|
||||
assert page is not None
|
||||
next_cursor = None
|
||||
if next_key is not None:
|
||||
next_cursor = encode_cursor(chat_id, next_key[0], next_key[1])
|
||||
return _json({"items": page, "next_cursor": next_cursor}, rid)
|
||||
|
||||
|
||||
def _media_type(value: str) -> str:
|
||||
return value.split(";", 1)[0].strip().lower()
|
||||
|
||||
|
||||
async def _read_limited(request: Request, limit: int) -> bytes:
|
||||
received = bytearray()
|
||||
async for chunk in request.stream():
|
||||
if len(received) + len(chunk) > limit:
|
||||
raise AppError(413, "file exceeds upload limit")
|
||||
received.extend(chunk)
|
||||
return bytes(received)
|
||||
|
||||
|
||||
async def upload_attachment(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
chat_id = request.path_params["chat_id"]
|
||||
if validate_uuid(chat_id):
|
||||
return error_response(400, "invalid chat id", rid)
|
||||
if validate_filename(request.query_params.get("filename")):
|
||||
return error_response(400, "invalid filename", rid)
|
||||
filename = request.query_params.get("filename") or ""
|
||||
declared = _media_type(request.headers.get("content-type", ""))
|
||||
if declared not in ("image/png", "image/jpeg"):
|
||||
return error_response(415, "unsupported media type", rid)
|
||||
content_length = request.headers.get("content-length")
|
||||
if content_length is not None:
|
||||
try:
|
||||
announced = int(content_length)
|
||||
except ValueError:
|
||||
return error_response(400, "invalid content length", rid)
|
||||
if announced > config.MAX_UPLOAD_BYTES:
|
||||
return error_response(413, "file exceeds upload limit", rid)
|
||||
if announced <= 0:
|
||||
return error_response(415, "unsupported or damaged image", rid)
|
||||
if await store.load_chat_for_member(chat_id, user["id"]) is None:
|
||||
return error_response(404, "chat not found", rid)
|
||||
data = await _read_limited(request, config.MAX_UPLOAD_BYTES)
|
||||
if not data:
|
||||
return error_response(415, "unsupported or damaged image", rid)
|
||||
try:
|
||||
content_type = images.inspect_image(data, declared)
|
||||
except images.ImageRejected as exc:
|
||||
return error_response(exc.status, exc.message, rid)
|
||||
created = await attachments.create_attachment(chat_id, user["id"], filename, content_type, data)
|
||||
return _json(created, rid, 202)
|
||||
|
||||
|
||||
async def begin_direct_upload(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
chat_id = request.path_params["chat_id"]
|
||||
if validate_uuid(chat_id):
|
||||
return error_response(400, "invalid chat id", rid)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, rid)
|
||||
assert body is not None
|
||||
if require_exact_keys(body, {"filename", "content_type", "size_bytes"}, {"filename", "content_type", "size_bytes"}):
|
||||
return error_response(400, "invalid upload payload", rid)
|
||||
if validate_filename(body["filename"]):
|
||||
return error_response(400, "invalid filename", rid)
|
||||
content_type = body["content_type"]
|
||||
if content_type not in ("image/png", "image/jpeg"):
|
||||
return error_response(415, "unsupported media type", rid)
|
||||
size = body["size_bytes"]
|
||||
if isinstance(size, bool) or not isinstance(size, int):
|
||||
return error_response(400, "invalid size", rid)
|
||||
if size > config.MAX_UPLOAD_BYTES:
|
||||
return error_response(413, "file exceeds upload limit", rid)
|
||||
if size < 1:
|
||||
return error_response(400, "invalid size", rid)
|
||||
if await store.load_chat_for_member(chat_id, user["id"]) is None:
|
||||
return error_response(404, "chat not found", rid)
|
||||
started = await direct.begin_upload(chat_id, user["id"], body["filename"], content_type, size)
|
||||
return _json(started, rid, 201)
|
||||
|
||||
|
||||
async def complete_direct_upload(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
upload_id = request.path_params["upload_id"]
|
||||
if validate_uuid(upload_id):
|
||||
return error_response(400, "invalid upload id", rid)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, rid)
|
||||
assert body is not None
|
||||
if require_exact_keys(body, {"sha256"}, {"sha256"}):
|
||||
return error_response(400, "invalid upload payload", rid)
|
||||
digest = body["sha256"]
|
||||
if not isinstance(digest, str) or re.fullmatch(r"[a-f0-9]{64}", digest) is None:
|
||||
return error_response(400, "invalid checksum", rid)
|
||||
created = await direct.complete_upload(upload_id, user["id"], digest)
|
||||
return _json(created, rid, 202)
|
||||
|
||||
|
||||
async def get_attachment(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
row = await attachments.get_for_member(attachment_id, user["id"])
|
||||
if row is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(await attachments.present(row), rid)
|
||||
|
||||
|
||||
async def download_attachment(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
user = await _bearer_user(request)
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
variant = request.query_params.get("variant") or "original"
|
||||
if variant not in ("original", "thumbnail", "crop", "optimized"):
|
||||
return error_response(400, "invalid variant", rid)
|
||||
row = await attachments.get_for_member(attachment_id, user["id"])
|
||||
if row is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
if row["status"] != "ready":
|
||||
return error_response(409, "attachment is not ready", rid)
|
||||
if variant == "original":
|
||||
payload = await s3.get_bytes(row["object_key"])
|
||||
media_type = row["content_type"]
|
||||
else:
|
||||
async with db.require().acquire() as conn:
|
||||
found = await conn.fetchrow(
|
||||
"""
|
||||
SELECT content_type, object_key
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id = $1 AND pipeline_version = $2 AND name = $3
|
||||
""",
|
||||
row["id"],
|
||||
int(row["applied_pipeline_version"]),
|
||||
variant,
|
||||
)
|
||||
if found is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
payload = await s3.get_bytes(found["object_key"])
|
||||
media_type = found["content_type"]
|
||||
return Response(payload, status_code=200, media_type=media_type, headers={"X-Request-Id": rid})
|
||||
|
||||
|
||||
async def lab_fault(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", rid)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, rid)
|
||||
assert body is not None
|
||||
mode = body.get("mode", "")
|
||||
remaining = body.get("remaining", 1)
|
||||
if isinstance(remaining, bool) or not isinstance(remaining, int):
|
||||
return error_response(400, "invalid fault", rid)
|
||||
try:
|
||||
armed = await jobs.arm_fault(mode, remaining)
|
||||
except AppError as exc:
|
||||
return error_response(exc.status, exc.message, rid)
|
||||
return _json(armed, rid)
|
||||
|
||||
|
||||
async def lab_jobs(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", rid)
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
listed = await jobs.list_jobs(UUID(attachment_id))
|
||||
if listed["applied_pipeline_version"] is None:
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(listed, rid)
|
||||
|
||||
|
||||
async def lab_bump(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", rid)
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
result = await jobs.bump_pipeline(UUID(attachment_id))
|
||||
if result["result"] == "missing":
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(result, rid, 202)
|
||||
|
||||
|
||||
async def lab_redeliver(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", rid)
|
||||
job_id = request.path_params["job_id"]
|
||||
if validate_uuid(job_id):
|
||||
return error_response(400, "invalid job id", rid)
|
||||
result = await jobs.redeliver(UUID(job_id))
|
||||
if result["result"] == "missing":
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(result, rid, 202)
|
||||
|
||||
|
||||
async def lab_replay(request: Request) -> Response:
|
||||
rid = request.state.request_id
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", rid)
|
||||
attachment_id = request.path_params["attachment_id"]
|
||||
if validate_uuid(attachment_id):
|
||||
return error_response(400, "invalid attachment id", rid)
|
||||
result = await jobs.replay_attachment(attachment_id)
|
||||
if result["result"] == "missing":
|
||||
return error_response(404, "attachment not found", rid)
|
||||
return _json(result, rid)
|
||||
|
||||
|
||||
async def lab_sweep(request: Request) -> Response:
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", request.state.request_id)
|
||||
deleted = await attachments.sweep_orphans()
|
||||
return _json({"deleted": deleted}, request.state.request_id)
|
||||
|
||||
|
||||
async def lab_metrics(request: Request) -> Response:
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", request.state.request_id)
|
||||
return _json(
|
||||
{
|
||||
"singleflight": await cache.singleflight_enabled(),
|
||||
"invalidate": await cache.invalidate_enabled(),
|
||||
"history_sql": await cache.history_sql_count(),
|
||||
},
|
||||
request.state.request_id,
|
||||
)
|
||||
|
||||
|
||||
async def lab_flags(request: Request) -> Response:
|
||||
if not config.lab_mode():
|
||||
return error_response(404, "not found", request.state.request_id)
|
||||
raw = await request.body()
|
||||
body, err = parse_json_body(raw)
|
||||
if err:
|
||||
return error_response(400, err, request.state.request_id)
|
||||
assert body is not None
|
||||
if body.get("reset_sql"):
|
||||
await cache.reset_history_sql_count()
|
||||
flags = await cache.set_flags(
|
||||
singleflight=body.get("singleflight"),
|
||||
invalidate=body.get("invalidate"),
|
||||
delay_ms=body.get("delay_ms"),
|
||||
)
|
||||
return _json(flags, request.state.request_id)
|
||||
Reference in New Issue
Block a user