feat(chat): 聊天消息预览

This commit is contained in:
xingc
2025-05-15 22:48:58 +08:00
parent 7b86e482fb
commit 00ef8d8971
3 changed files with 72 additions and 25 deletions

View File

@@ -34,7 +34,7 @@ chat_router = APIRouter(prefix='/chat', tags=['聊天模块'])
@chat_router.get(
'/all/', summary='获取所有聊天',
response_model=base_schema.BaseResponse[
base_schema.Paginated[
base_schema.PaginatedCursor[
List[schema.ChatUnreadOut]
]
]
@@ -72,6 +72,7 @@ async def get_all_chats(
return {
'data': {
'list': unread_messages_chats_count,
'next_page': (page * page_size) < total,
'total': total,
'page': page,
'page_size': page_size,
@@ -82,7 +83,7 @@ async def get_all_chats(
@chat_router.get(
'/{chat_guid}/messages/', summary='获取单个聊天的历史消息',
response_model=base_schema.BaseResponse[
base_schema.Paginated[
base_schema.PaginatedCursor[
List[schema.MessageOut]
]
]
@@ -111,14 +112,15 @@ async def get_single_chat_messages(
conditions
)
)
total = await db_session.execute(total_stmt)
total_result = await db_session.execute(total_stmt)
total = total_result.scalar()
# 分页记录查询
stmt = (
select(Message)
.where(conditions)
.order_by(Message.created_at.desc())
.limit(page_size).offset((page - 1) * page_size)
.options(selectinload(Message.user), selectinload(Message.chat))
.order_by(Message.id.desc()).
limit(page_size).offset((page - 1) * page_size)
)
records = await db_session.execute(stmt)
results = [
@@ -133,7 +135,9 @@ async def get_single_chat_messages(
return {
'data': {
'list': results,
'total': total.scalar(),
'cursor': results[-1].get('id'),
'next_page': (page * page_size) < total,
'total': total,
'page': page,
'page_size': page_size,
},
@@ -250,7 +254,7 @@ async def get_chat_websocket(
websocket.user_id = login_user.user_id
await socket_manager.add_user_socket_connection(user_id, websocket)
ratelimit = WebSocketRateLimiter(times=50, seconds=10, callback=websocket_callback)
ratelimit = WebSocketRateLimiter(times=30, seconds=10, callback=websocket_callback)
current_user = await CRUD(db_session, User).get(filters={'user_id': login_user.user_id})
if chats := await get_user_active_direct_chats(db_session, current_user=current_user):
for chat_guid in chats: