forked from mtusi2026/mtusi2026lab2
Исправления после проверки: кэш, cursor, обработка ошибок
This commit is contained in:
+20
-13
@@ -1,10 +1,11 @@
|
||||
import asyncio
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
from app.cache import Cache
|
||||
|
||||
import asyncpg
|
||||
import asyncio
|
||||
|
||||
from app.cache import Cache
|
||||
|
||||
|
||||
def _now_iso(value: datetime) -> str:
|
||||
@@ -118,9 +119,11 @@ class Storage:
|
||||
where = ""
|
||||
if cursor:
|
||||
try:
|
||||
cur_created, cur_id = cursor.split("|", 1)
|
||||
cur_actor, cur_created, cur_id = cursor.split("|", 2)
|
||||
except ValueError:
|
||||
raise ValueError("bad_cursor")
|
||||
if cur_actor != actor_id:
|
||||
raise ValueError("bad_cursor")
|
||||
where = "AND (c.created_at, c.id) > ($2, $3)"
|
||||
params += [
|
||||
datetime.fromisoformat(cur_created.replace("Z", "+00:00")),
|
||||
@@ -152,7 +155,7 @@ class Storage:
|
||||
next_cursor = None
|
||||
if len(rows) > limit:
|
||||
last = rows[limit - 1]
|
||||
next_cursor = f"{_now_iso(last['created_at'])}|{last['id']}"
|
||||
next_cursor = f"{actor_id}|{_now_iso(last['created_at'])}|{last['id']}"
|
||||
return items, next_cursor
|
||||
|
||||
async def add_message(
|
||||
@@ -180,8 +183,15 @@ class Storage:
|
||||
async def list_messages(
|
||||
self, chat_id: str, actor_id: str, limit: int, cursor: Optional[str],
|
||||
) -> tuple[list[dict], Optional[str]]:
|
||||
cache_key = f"messages:{chat_id}:{limit}:{cursor or ''}"
|
||||
async with self._pool.acquire() as conn:
|
||||
is_member = await conn.fetchval(
|
||||
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||
chat_id, actor_id,
|
||||
)
|
||||
if is_member is None:
|
||||
raise ValueError("not_found")
|
||||
|
||||
cache_key = f"messages:{chat_id}:{limit}:{cursor or ''}"
|
||||
cached = await self._cache.get_json(cache_key)
|
||||
if cached is not None:
|
||||
return cached["items"], cached["next_cursor"]
|
||||
@@ -201,19 +211,15 @@ class Storage:
|
||||
|
||||
try:
|
||||
async with self._pool.acquire() as conn:
|
||||
is_member = await conn.fetchval(
|
||||
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||
chat_id, actor_id,
|
||||
)
|
||||
if is_member is None:
|
||||
raise ValueError("not_found")
|
||||
params: list = [chat_id]
|
||||
where = ""
|
||||
if cursor:
|
||||
try:
|
||||
cur_created, cur_id = cursor.split("|", 1)
|
||||
cur_chat, cur_created, cur_id = cursor.split("|", 2)
|
||||
except ValueError:
|
||||
raise ValueError("bad_cursor")
|
||||
if cur_chat != chat_id:
|
||||
raise ValueError("bad_cursor")
|
||||
where = "AND (created_at, id) > ($2, $3)"
|
||||
params += [
|
||||
datetime.fromisoformat(cur_created.replace("Z", "+00:00")),
|
||||
@@ -233,7 +239,7 @@ class Storage:
|
||||
next_cursor = None
|
||||
if len(rows) > limit:
|
||||
last = rows[limit - 1]
|
||||
next_cursor = f"{_now_iso(last['created_at'])}|{last['id']}"
|
||||
next_cursor = f"{chat_id}|{_now_iso(last['created_at'])}|{last['id']}"
|
||||
|
||||
if got_lock:
|
||||
await self._cache.set_json(
|
||||
@@ -244,6 +250,7 @@ class Storage:
|
||||
finally:
|
||||
if got_lock:
|
||||
await self._cache.unlock(lock_key)
|
||||
|
||||
@staticmethod
|
||||
def _user_row(row) -> dict:
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user