forked from mtusi2026/mtusi2026lab2
внес правки
This commit is contained in:
+11
-3
@@ -10,7 +10,6 @@ from app.models.chat import Chat, ChatMember
|
||||
from app.models.user import User
|
||||
from app.security import get_current_user
|
||||
from app.dependencies import get_session
|
||||
from app.pagination import paginate
|
||||
|
||||
router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["messages"])
|
||||
|
||||
@@ -78,13 +77,22 @@ async def list_messages(
|
||||
|
||||
await check_chat_access(chat_id, user.id, session)
|
||||
|
||||
|
||||
offset = int(cursor) if cursor else 0
|
||||
|
||||
result = await session.execute(
|
||||
select(Message)
|
||||
.where(Message.chat_id == chat_id)
|
||||
.order_by(Message.created_at, Message.id)
|
||||
.limit(limit + 1)
|
||||
.offset(offset)
|
||||
)
|
||||
msgs = result.scalars().all()
|
||||
|
||||
has_next = len(msgs) > limit
|
||||
if has_next:
|
||||
msgs = msgs[:limit]
|
||||
|
||||
my_messages = [
|
||||
{
|
||||
"id": m.id,
|
||||
@@ -95,5 +103,5 @@ async def list_messages(
|
||||
} for m in msgs
|
||||
]
|
||||
|
||||
page, next_cursor = paginate(my_messages, limit, cursor)
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
next_cursor = str(offset + limit) if has_next else None
|
||||
return {"items": my_messages, "next_cursor": next_cursor}
|
||||
Reference in New Issue
Block a user