feat(chat): 聊天单向删除

This commit is contained in:
xingc
2025-05-15 18:24:05 +08:00
parent 99f15cff84
commit cc097a587f
8 changed files with 147 additions and 43 deletions

View File

@@ -11,7 +11,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased, selectinload
from core.base.exceptions import WebsocketTooManyRequests
from core.db.models import User, Chat, ReadStatus, Message, chat_participant
from core.db.models import User, Chat, ReadStatus, Message, ChatParticipant
from core.routers.auth.schema import LoginUser
from core.utils.helper import format_date
@@ -38,12 +38,14 @@ async def get_user_active_direct_chats(
direct_chats_dict[str(direct_chat.guid)] = direct_chat.id
return direct_chats_dict
return None
async def get_unread_messages_per_chat(
db_session: AsyncSession, chats: list[Chat], login_user: LoginUser
) -> list[dict]:
"""获取聊天未读消息数"""
unread_messages_info_per_chat = {chat.id: {'message_count': 0} for chat in chats}
unread_messages_info_per_chat: dict[int, dict] = {chat.id: {'message_count': 0} for chat in chats}
# 查询消息情况
read_status_alias = aliased(ReadStatus)
@@ -85,6 +87,7 @@ async def get_unread_messages_per_chat(
updated_at=format_date(chat.updated_at),
recipient_user=users[0],
last_message=messages_info.get('message'),
is_delete=chat.is_deleted,
unread_messages_count=messages_info.get('message_count'),
))
@@ -93,8 +96,8 @@ async def get_unread_messages_per_chat(
async def get_chat(chat_guid, login_user: User, db_session: AsyncSession) -> Chat | None:
subquery = (
select(chat_participant.c.chat_id)
.where(chat_participant.c.user_id == login_user.user_id)
select(ChatParticipant.chat_id)
.where(ChatParticipant.user_id == login_user.user_id)
).scalar_subquery()
query = (