forked from mtusi2026/mtusi2026lab3
фикс из пр
This commit is contained in:
+40
-31
@@ -8,7 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from src.models import Chat, ChatPage, CreateChat, CreateUser, User, CreateMessage, Message, MessagePage
|
||||
from src.security import hash_password
|
||||
from src.errors import APIError
|
||||
from src.cursor import decode_cursor, encode_cursor
|
||||
from src.cursor import decode_cursor, encode_cursor, decode_chat_cursor, encode_chat_cursor
|
||||
|
||||
|
||||
async def create_user(
|
||||
@@ -269,33 +269,48 @@ async def list_chats(
|
||||
session: AsyncSession,
|
||||
current_user_id: UUID,
|
||||
limit: int,
|
||||
cursor: str | None = None,
|
||||
) -> ChatPage:
|
||||
chats_result = await session.execute(
|
||||
cursor_created_at = None
|
||||
cursor_chat_id = None
|
||||
if cursor is not None:
|
||||
try:
|
||||
cursor_user_id, cursor_created_at, cursor_chat_id = decode_chat_cursor(cursor)
|
||||
except ValueError as exc:
|
||||
raise APIError(status_code=400, code="invalid_request", message="Invalid cursor") from exc
|
||||
if cursor_user_id != current_user_id:
|
||||
raise APIError(status_code=400, code="invalid_request", message="Invalid cursor")
|
||||
|
||||
result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
SELECT
|
||||
c.id,
|
||||
c.title,
|
||||
c.created_at
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats AS c
|
||||
JOIN chat_members AS cm
|
||||
ON cm.chat_id = c.id
|
||||
JOIN chat_members AS cm ON cm.chat_id = c.id
|
||||
WHERE cm.user_id = :current_user_id
|
||||
AND (
|
||||
CAST(:cursor_created_at AS timestamptz) IS NULL
|
||||
OR (c.created_at, c.id) > (
|
||||
CAST(:cursor_created_at AS timestamptz),
|
||||
CAST(:cursor_chat_id AS uuid)
|
||||
)
|
||||
)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT :limit
|
||||
LIMIT :fetch_limit
|
||||
"""
|
||||
),
|
||||
{
|
||||
"current_user_id": current_user_id,
|
||||
"limit": limit,
|
||||
"cursor_created_at": cursor_created_at,
|
||||
"cursor_chat_id": cursor_chat_id,
|
||||
"fetch_limit": limit + 1,
|
||||
},
|
||||
)
|
||||
|
||||
chat_rows = chats_result.all()
|
||||
|
||||
rows = result.all()
|
||||
has_more = len(rows) > limit
|
||||
page_rows = rows[:limit]
|
||||
items = []
|
||||
|
||||
for chat_row in chat_rows:
|
||||
for chat_row in page_rows:
|
||||
members_result = await session.execute(
|
||||
text(
|
||||
"""
|
||||
@@ -305,29 +320,23 @@ async def list_chats(
|
||||
ORDER BY user_id
|
||||
"""
|
||||
),
|
||||
{
|
||||
"chat_id": chat_row.id,
|
||||
},
|
||||
{"chat_id": chat_row.id},
|
||||
)
|
||||
|
||||
member_ids = [
|
||||
UUID(str(row.user_id))
|
||||
for row in members_result
|
||||
]
|
||||
|
||||
items.append(
|
||||
Chat(
|
||||
id=UUID(str(chat_row.id)),
|
||||
title=chat_row.title,
|
||||
member_ids=member_ids,
|
||||
member_ids=[UUID(str(row.user_id)) for row in members_result],
|
||||
created_at=chat_row.created_at,
|
||||
)
|
||||
)
|
||||
|
||||
return ChatPage(
|
||||
items=items,
|
||||
next_cursor=None,
|
||||
)
|
||||
next_cursor = None
|
||||
if has_more:
|
||||
last = page_rows[-1]
|
||||
next_cursor = encode_chat_cursor(
|
||||
current_user_id, last.created_at, UUID(str(last.id))
|
||||
)
|
||||
return ChatPage(items=items, next_cursor=next_cursor)
|
||||
|
||||
|
||||
async def create_message(
|
||||
@@ -643,4 +652,4 @@ async def get_messages(
|
||||
return MessagePage(
|
||||
items=items,
|
||||
next_cursor=next_cursor,
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user