forked from mtusi2026/mtusi2026lab5
feat: крролик, по тщ 5 лаба
This commit is contained in:
+36
-12
@@ -47,14 +47,19 @@ class FakeChatRepository(ChatRepository):
|
||||
def __init__(self) -> None:
|
||||
self.foreign: set[UUID] = set()
|
||||
|
||||
async def create(self, title: str, created_by: UUID, member_ids: list[UUID]) -> ChatSchema:
|
||||
async def create(
|
||||
self, title: str, created_by: UUID, member_ids: list[UUID]
|
||||
) -> ChatSchema:
|
||||
raise NotImplementedError
|
||||
|
||||
async def get_for_member(self, chat_id: UUID, member_id: UUID) -> ChatSchema | None:
|
||||
if chat_id in self.foreign:
|
||||
return None
|
||||
return ChatSchema(
|
||||
id=chat_id, title="chat", member_ids=[member_id], created_at=datetime.now(UTC)
|
||||
id=chat_id,
|
||||
title="chat",
|
||||
member_ids=[member_id],
|
||||
created_at=datetime.now(UTC),
|
||||
)
|
||||
|
||||
async def list_for_member(
|
||||
@@ -69,7 +74,9 @@ class FakeAttachmentRepository(FaultInjection, AttachmentRepository):
|
||||
self.rows: dict[UUID, dict] = {}
|
||||
self.ready_returns_none = False
|
||||
|
||||
def add_row(self, object_key: str, status: AttachmentStatus, age_seconds: float) -> UUID:
|
||||
def add_row(
|
||||
self, object_key: str, status: AttachmentStatus, age_seconds: float
|
||||
) -> UUID:
|
||||
attachment_id = uuid4()
|
||||
self.rows[attachment_id] = {
|
||||
"status": status,
|
||||
@@ -81,21 +88,30 @@ class FakeAttachmentRepository(FaultInjection, AttachmentRepository):
|
||||
|
||||
async def create_pending(self, attachment: NewAttachmentSchema) -> UUID:
|
||||
await self._enter("create_pending")
|
||||
attachment_id = self.add_row(attachment.object_key, AttachmentStatus.PENDING, 0)
|
||||
attachment_id = self.add_row(
|
||||
"staging/" + attachment.object_key, AttachmentStatus.PENDING, 0
|
||||
)
|
||||
self.rows[attachment_id]["new"] = attachment
|
||||
return attachment_id
|
||||
|
||||
async def mark_ready(self, attachment_id: UUID, grace_seconds: int) -> AttachmentSchema | None:
|
||||
await self._enter("mark_ready")
|
||||
async def mark_queued(
|
||||
self, attachment_id: UUID, grace_seconds: int
|
||||
) -> AttachmentSchema | None:
|
||||
await self._enter("mark_queued")
|
||||
row = self.rows.get(attachment_id)
|
||||
if self.ready_returns_none or row is None or row["status"] != AttachmentStatus.PENDING:
|
||||
if (
|
||||
self.ready_returns_none
|
||||
or row is None
|
||||
or row["status"] != AttachmentStatus.PENDING
|
||||
):
|
||||
return None
|
||||
if row["created_at"] <= datetime.now(UTC) - timedelta(seconds=grace_seconds):
|
||||
return None
|
||||
row["status"] = AttachmentStatus.READY
|
||||
row["status"] = AttachmentStatus.QUEUED
|
||||
new: NewAttachmentSchema = row["new"]
|
||||
return AttachmentSchema(
|
||||
id=attachment_id,
|
||||
status="queued",
|
||||
chat_id=new.chat_id,
|
||||
owner_id=new.owner_id,
|
||||
filename=new.filename,
|
||||
@@ -120,10 +136,15 @@ class FakeAttachmentRepository(FaultInjection, AttachmentRepository):
|
||||
attachment_id
|
||||
for attachment_id, row in self.rows.items()
|
||||
if row["status"] == AttachmentStatus.DELETING
|
||||
or (row["status"] == AttachmentStatus.PENDING and row["created_at"] < threshold)
|
||||
or (
|
||||
row["status"] == AttachmentStatus.PENDING
|
||||
and row["created_at"] < threshold
|
||||
)
|
||||
]
|
||||
|
||||
async def claim_unfinished(self, grace_seconds: int, limit: int) -> list[UnfinishedUploadSchema]:
|
||||
async def claim_unfinished(
|
||||
self, grace_seconds: int, limit: int
|
||||
) -> list[UnfinishedUploadSchema]:
|
||||
await self._enter("claim_unfinished")
|
||||
# Повторный вызов в том же проходе не должен возвращать те же строки
|
||||
claimed = [
|
||||
@@ -135,10 +156,13 @@ class FakeAttachmentRepository(FaultInjection, AttachmentRepository):
|
||||
self.rows[attachment_id]["status"] = AttachmentStatus.DELETING
|
||||
self.rows[attachment_id]["claimed_now"] = True
|
||||
return [
|
||||
UnfinishedUploadSchema(id=i, object_key=self.rows[i]["object_key"]) for i in claimed
|
||||
UnfinishedUploadSchema(id=i, object_key=self.rows[i]["object_key"])
|
||||
for i in claimed
|
||||
]
|
||||
|
||||
async def list_unfinished(self, grace_seconds: int, limit: int) -> list[UnfinishedUploadSchema]:
|
||||
async def list_unfinished(
|
||||
self, grace_seconds: int, limit: int
|
||||
) -> list[UnfinishedUploadSchema]:
|
||||
await self._enter("list_unfinished")
|
||||
return [
|
||||
UnfinishedUploadSchema(id=i, object_key=self.rows[i]["object_key"])
|
||||
|
||||
Reference in New Issue
Block a user