feat(chat): 单向聊天的历史消息获取

This commit is contained in:
xingc
2025-05-15 18:54:08 +08:00
parent 2b54b4493b
commit 329a50bc43
2 changed files with 20 additions and 12 deletions

View File

@@ -94,18 +94,31 @@ async def get_unread_messages_per_chat(
return results
async def get_chat(chat_guid, login_user: User, db_session: AsyncSession) -> Chat | None:
async def get_chat(chat_guid, login_user: User, db_session: AsyncSession,
is_query_messages: bool = False) -> Chat | None:
subquery = (
select(ChatParticipant.chat_id)
.where(ChatParticipant.user_id == login_user.user_id)
.where(
and_(
ChatParticipant.user_id == login_user.user_id,
ChatParticipant.is_deleted == False
)
)
).scalar_subquery()
conditions = [
Chat.id.in_(subquery),
Chat.guid == chat_guid
]
if is_query_messages is False:
conditions.append(
Chat.is_deleted == False
)
query = (
select(Chat)
.where(and_(
Chat.id.in_(subquery),
Chat.guid == chat_guid,
Chat.is_deleted == False
*conditions
))
)
result = await db_session.execute(query)