Перенести реализацию мессенджера из ЛР 6

Файлы скопированы без изменений из solution/BPI2301-Vozzhin
репозитория mtusi2026lab6. Доработки ЛР 7 идут отдельными коммитами.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-12-03 19:48:41 +05:00
co-authored by Claude Opus 5.5
parent 20afc2c790
commit 31d441184b
92 changed files with 6153 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
from __future__ import annotations
from datetime import datetime, timezone
from typing import Optional, Sequence
from uuid import UUID, uuid4
from ..domain.errors import Invalid, NotFound
from ..domain.models import Chat, Keyset, Page, User
from ..repositories import ChatRepository
class ChatService:
def __init__(self, repository: ChatRepository) -> None:
self._repository = repository
async def create(self, actor: User, title: str, member_ids: Sequence[UUID]) -> Chat:
if actor.id not in member_ids:
raise Invalid('вызывающий должен входить в member_ids')
return await self._repository.create(
chat_id=uuid4(),
title=title,
member_ids=member_ids,
created_at=datetime.now(timezone.utc),
)
async def get(self, actor: User, chat_id: UUID) -> Chat:
chat = await self._repository.get_for_member(chat_id, actor.id)
if chat is None:
raise NotFound('чат не найден')
return chat
async def page(self, actor: User, after: Optional[Keyset], limit: int) -> Page:
rows = await self._repository.page_for_member(actor.id, after, limit + 1)
items = tuple(rows[:limit])
return Page(items=items, next=Keyset.from_item(items[-1]) if len(rows) > limit else None)