fix(chat): 修复新建聊天失败 调整查询已存在sql

This commit is contained in:
xingc
2025-05-14 16:24:34 +08:00
parent 69788bb39e
commit ee78186508

View File

@@ -5,19 +5,18 @@
# @Author : xingc # @Author : xingc
# @Desc : # @Desc :
import logging import logging
from typing import List, Dict from typing import List
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from fastapi import APIRouter, Query, Depends, WebSocket, WebSocketDisconnect, HTTPException, status from fastapi import APIRouter, Query, Depends, WebSocket, WebSocketDisconnect, HTTPException, status
from fastapi_limiter.depends import WebSocketRateLimiter from fastapi_limiter.depends import WebSocketRateLimiter
import redis.asyncio as aioredis from sqlalchemy import select, func, and_, exists, distinct
from sqlalchemy import select, func, and_, exists
from sqlalchemy.ext.asyncio import AsyncSession 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.base import schema as base_schema
from core.db.crud import CRUD 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.db.models import Message, Chat, User, ReadStatus, chat_participant
from core.routers.auth.schema import LoginUser from core.routers.auth.schema import LoginUser
from core.routers.auth.services import get_current_user_form_http, get_current_user_form_ws 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 = ( query = (
select(Chat) select(Chat)
.where( .where(
and_(
exists().where( exists().where(
(chat_participant.c.chat_id == Chat.id) & (chat_participant.c.chat_id == Chat.id) &
(chat_participant.c.user_id == login_user.user_id) (chat_participant.c.user_id == login_user.user_id)
) & ),
exists().where( exists().where(
(chat_participant.c.chat_id == Chat.id) & (chat_participant.c.chat_id == Chat.id) &
(chat_participant.c.user_id == recipient_user.user_id) (chat_participant.c.user_id == recipient_user.user_id)
) & ),
Chat.is_deleted == False Chat.is_deleted == False
)
).order_by(Chat.created_at.desc()) ).order_by(Chat.created_at.desc())
) )
result = await db_session.execute(query) result = await db_session.execute(query)