From ee78186508d2ccd38fd5edb9eca4518488e4b256 Mon Sep 17 00:00:00 2001 From: xingc Date: Wed, 14 May 2025 16:24:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(chat):=20=E4=BF=AE=E5=A4=8D=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E8=81=8A=E5=A4=A9=E5=A4=B1=E8=B4=A5=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=9F=A5=E8=AF=A2=E5=B7=B2=E5=AD=98=E5=9C=A8sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/routers/chat/routes.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/core/routers/chat/routes.py b/core/routers/chat/routes.py index 036cc37..40a7c54 100644 --- a/core/routers/chat/routes.py +++ b/core/routers/chat/routes.py @@ -5,19 +5,18 @@ # @Author : xingc # @Desc : import logging -from typing import List, Dict +from typing import List from json.decoder import JSONDecodeError from fastapi import APIRouter, Query, Depends, WebSocket, WebSocketDisconnect, HTTPException, status from fastapi_limiter.depends import WebSocketRateLimiter -import redis.asyncio as aioredis -from sqlalchemy import select, func, and_, exists +from sqlalchemy import select, func, and_, exists, distinct from sqlalchemy.ext.asyncio import AsyncSession -from sqlalchemy.orm import selectinload +from sqlalchemy.orm import selectinload, aliased from core.base import schema as base_schema from core.db.crud import CRUD -from core.db.engine import get_async_session, get_cache +from core.db.engine import get_async_session from core.db.models import Message, Chat, User, ReadStatus, chat_participant from core.routers.auth.schema import LoginUser from core.routers.auth.services import get_current_user_form_http, get_current_user_form_ws @@ -162,15 +161,17 @@ async def create_new_chat( query = ( select(Chat) .where( - exists().where( - (chat_participant.c.chat_id == Chat.id) & - (chat_participant.c.user_id == login_user.user_id) - ) & - exists().where( - (chat_participant.c.chat_id == Chat.id) & - (chat_participant.c.user_id == recipient_user.user_id) - ) & - Chat.is_deleted == False + and_( + exists().where( + (chat_participant.c.chat_id == Chat.id) & + (chat_participant.c.user_id == login_user.user_id) + ), + exists().where( + (chat_participant.c.chat_id == Chat.id) & + (chat_participant.c.user_id == recipient_user.user_id) + ), + Chat.is_deleted == False + ) ).order_by(Chat.created_at.desc()) ) result = await db_session.execute(query)