feat: add image attachments to messages

- upload, metadata and download endpoints for PNG/JPEG attachments:
  limits before reading and decompression, type by content,
  structural integrity check without decoding pixels, safe filename
- write order: pending row with object key -> PUT to S3 -> ready;
  per-step DB sessions so no pool connection is held during transfers
- attachment_ids in messages (validation, idempotency hash, history);
  whitespace-only text allowed with an attachment (teacher, 2026-10-20)
- migration: attachments and message_attachments tables, text length
  CHECK relaxed with NOT VALID
- student tests, fault checks and driver commands for attachments;
  rollout profile starts from e1161f7 and checks the commit first
- sync attachments deltas into main specs; rules, AGENTS.md, PLAN.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 01:08:56 +03:00
co-authored by Claude Opus 5.5
parent 73779f7acd
commit 116d8d3bf0
72 changed files with 5546 additions and 247 deletions
+94 -3
View File
@@ -2,6 +2,7 @@
import base64
import dataclasses
import hashlib
import secrets
import uuid
from collections.abc import Callable, Sequence
@@ -231,15 +232,105 @@ class Api:
text: str,
*,
key: str | None = None,
attachment_ids: Sequence[str] | None = None,
) -> dict[str, Any]:
"""Успешная отправка: 201 и `Message`; без `key` — новый ключ."""
"""Успешная отправка: 201 и `Message`; без `key` — новый ключ.
`attachment_ids=None` — поля в теле нет.
"""
body: dict[str, Any] = {"text": text}
if attachment_ids is not None:
body["attachment_ids"] = list(attachment_ids)
resp = self.post_message(
actor,
chat_id,
key=new_key() if key is None else key,
json={"text": text},
json=body,
)
message = assert_ok(resp, 201, "Message")
if attachment_ids is not None:
assert message["attachment_ids"] == [
item.lower() for item in attachment_ids
], "attachment_ids differ from sent"
return message
def post_attachment(
self,
actor: Actor | None,
chat_id: str,
data: Any,
*,
content_type: str | None = "image/png",
filename: str | None = "image.png",
headers: dict[str, str] | None = None,
) -> httpx.Response:
"""Загрузка как есть: сырые байты (или итератор — частями, без
`Content-Length`); `None` — без заголовка или параметра."""
merged = dict(headers or {})
if content_type is not None:
merged["Content-Type"] = content_type
params = {} if filename is None else {"filename": filename}
return self.request(
"POST",
f"{API}/chats/{chat_id}/attachments",
actor=actor,
headers=merged,
params=params,
content=data,
)
def upload(
self,
actor: Actor,
chat_id: str,
data: bytes,
*,
content_type: str = "image/png",
filename: str = "image.png",
) -> dict[str, Any]:
"""Успешная загрузка: 201 и `Attachment` с размером и SHA-256."""
resp = self.post_attachment(
actor,
chat_id,
data,
content_type=content_type,
filename=filename,
)
attachment = assert_ok(resp, 201, "Attachment")
assert attachment["size_bytes"] == len(data), "size_bytes differs"
assert attachment["sha256"] == hashlib.sha256(data).hexdigest(), (
"sha256 differs"
)
assert attachment["chat_id"] == chat_id.lower(), "chat_id differs"
assert attachment["owner_id"] == actor.id, "owner_id differs"
assert attachment["filename"] == filename, "filename changed"
return attachment
def attachment_meta(
self,
actor: Actor | None,
attachment_id: str,
) -> httpx.Response:
return self.request(
"GET",
f"{API}/attachments/{attachment_id}",
actor=actor,
)
def attachment_content(
self,
actor: Actor | None,
attachment_id: str,
*,
variant: str | None = None,
) -> httpx.Response:
params = {} if variant is None else {"variant": variant}
return self.request(
"GET",
f"{API}/attachments/{attachment_id}/content",
actor=actor,
params=params,
)
return assert_ok(resp, 201, "Message")
def messages_page(
self,