Багфикс
This commit is contained in:
parent
32f1d70b1d
commit
6f6ffddc12
2 changed files with 41 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
|||
import asyncio
|
||||
|
||||
import websockets
|
||||
from fastapi import WebSocket, WebSocketDisconnect, Depends, HTTPException, status
|
||||
|
||||
|
@ -151,3 +153,40 @@ async def chat_ws(
|
|||
url = f"ws://localhost:8000/api/chat/ws/{chat_id}"
|
||||
async with websockets.connect(url, extra_headers={"Authorization": f"Bearer {token}"}) as websocket:
|
||||
print(await websocket.recv())
|
||||
|
||||
|
||||
class LongPollingManager:
|
||||
def __init__(self):
|
||||
self.waiters: list[asyncio.Future] = []
|
||||
self.messages: list[str] = []
|
||||
|
||||
|
||||
long_polling_manager = LongPollingManager()
|
||||
|
||||
|
||||
@router.get(
|
||||
"/poll",
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
async def poll():
|
||||
future = asyncio.Future()
|
||||
long_polling_manager.waiters.append(future)
|
||||
try:
|
||||
await future
|
||||
return {"message": long_polling_manager.messages.pop(0)}
|
||||
except asyncio.CancelledError:
|
||||
long_polling_manager.waiters.remove(future)
|
||||
raise HTTPException(status_code=500, detail="Client disconnected")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/send",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=None,
|
||||
)
|
||||
async def send(message: str):
|
||||
long_polling_manager.messages.append(message)
|
||||
while long_polling_manager.waiters:
|
||||
waiter = long_polling_manager.waiters.pop(0)
|
||||
if not waiter.done():
|
||||
waiter.set_result(None)
|
||||
|
|
|
@ -6,7 +6,6 @@ from passlib.context import CryptContext
|
|||
|
||||
from app.config import settings
|
||||
from app.exceptions import UserNotFoundException, UserMustConfirmEmailException
|
||||
from app.services.chat_service import ChatService
|
||||
from app.services.user_service import UserService
|
||||
from app.users.exceptions import IncorrectAuthDataException
|
||||
from app.chat.exceptions import UserDontHavePermissionException, ChatNotFoundException
|
||||
|
@ -78,7 +77,8 @@ class AuthService:
|
|||
@classmethod
|
||||
async def validate_user_access_to_chat(cls, uow: UnitOfWork, user_id: int, chat_id: int) -> None:
|
||||
try:
|
||||
await ChatService.find_chat(uow=uow, chat_id=chat_id, user_id=user_id)
|
||||
async with uow:
|
||||
await uow.chat.find_one(chat_id=chat_id, user_id=user_id)
|
||||
except ChatNotFoundException:
|
||||
raise UserDontHavePermissionException
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue