Files
mtusi2026lab4-safronov/src/api/schemas/message.py
T
ricoromaandClaude Opus 5.5 116d8d3bf0 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>
2026-10-21 01:08:56 +03:00

55 lines
1.8 KiB
Python

from typing import Self
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field, StrictStr, model_validator
from src.api.schemas.common import UtcDatetime, UuidIn
from src.utils.constants import (
MESSAGE_ATTACHMENTS_MAX,
MESSAGE_TEXT_MAX_LENGTH,
)
class CreateMessage(BaseModel):
"""Отправитель берётся из идентификации; поле sender_id — 400.
Пустой текст — `""` или из одних пробельных символов — допустим
только с хотя бы одним вложением (согласовано с преподавателем
2026-10-20, AGENTS.md). Текст не обрезается и не нормализуется.
"""
model_config = ConfigDict(extra="forbid")
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)
id: UUID
chat_id: UUID
sender_id: UUID
text: str
created_at: UtcDatetime
attachment_ids: list[UUID]
class MessagePage(BaseModel):
model_config = ConfigDict(from_attributes=True)
items: list[Message]
next_cursor: str | None