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
# @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)