forked from mtusi2026/mtusi2026lab4
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:
@@ -1,24 +1,40 @@
|
||||
from typing import Self
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, StrictStr, model_validator
|
||||
|
||||
from src.api.schemas.common import NonBlankStr, UtcDatetime
|
||||
from src.api.schemas.common import UtcDatetime, UuidIn
|
||||
from src.utils.constants import (
|
||||
MESSAGE_ATTACHMENTS_MAX,
|
||||
MESSAGE_TEXT_MAX_LENGTH,
|
||||
MESSAGE_TEXT_MIN_LENGTH,
|
||||
)
|
||||
|
||||
|
||||
class CreateMessage(BaseModel):
|
||||
"""Отправитель берётся из идентификации; поле sender_id — 400."""
|
||||
"""Отправитель берётся из идентификации; поле sender_id — 400.
|
||||
|
||||
Пустой текст — `""` или из одних пробельных символов — допустим
|
||||
только с хотя бы одним вложением (согласовано с преподавателем
|
||||
2026-10-20, AGENTS.md). Текст не обрезается и не нормализуется.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
text: NonBlankStr = Field(
|
||||
min_length=MESSAGE_TEXT_MIN_LENGTH,
|
||||
max_length=MESSAGE_TEXT_MAX_LENGTH,
|
||||
text: StrictStr = Field(max_length=MESSAGE_TEXT_MAX_LENGTH)
|
||||
# элементы — запись UUID контракта; повтор (и в другом регистре) — 400
|
||||
attachment_ids: list[UuidIn] = Field(
|
||||
default_factory=list,
|
||||
max_length=MESSAGE_ATTACHMENTS_MAX,
|
||||
)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _check_text_and_attachments(self) -> Self:
|
||||
if len(set(self.attachment_ids)) != len(self.attachment_ids):
|
||||
raise ValueError("attachment_ids must be unique")
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError("text must not be blank without attachments")
|
||||
return self
|
||||
|
||||
|
||||
class Message(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
@@ -28,6 +44,7 @@ class Message(BaseModel):
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: UtcDatetime
|
||||
attachment_ids: list[UUID]
|
||||
|
||||
|
||||
class MessagePage(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user