Compare commits
2 Commits
642580ccd5
...
970a4b200b
Author | SHA1 | Date | |
---|---|---|---|
|
970a4b200b | ||
|
2119973dbb |
@ -1,6 +1,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from fastapi import Depends, HTTPException, status, Query
|
from fastapi import Depends, HTTPException, status, Query
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -16,7 +17,6 @@ from database.room.models import Anonymous, Challenge, Challenges, CorrigedGener
|
|||||||
TmpCorrection, Waiter, MemberRead, CorrigedData, CorrectionData, Challenger
|
TmpCorrection, Waiter, MemberRead, CorrigedData, CorrectionData, Challenger
|
||||||
from services.auth import get_current_user_optional
|
from services.auth import get_current_user_optional
|
||||||
from services.database import generate_unique_code
|
from services.database import generate_unique_code
|
||||||
from uuid import UUID, uuid4
|
|
||||||
|
|
||||||
|
|
||||||
def create_room_db(*, room: RoomCreate, user: User | None = None, username: str | None = None, db: Session):
|
def create_room_db(*, room: RoomCreate, user: User | None = None, username: str | None = None, db: Session):
|
||||||
@ -172,14 +172,21 @@ def validate_username(username: str, room: Room, db: Session = Depends(get_sessi
|
|||||||
username_anonymous = db.exec(anonymous).first()
|
username_anonymous = db.exec(anonymous).first()
|
||||||
return None if username_anonymous is not None else username
|
return None if username_anonymous is not None else username
|
||||||
|
|
||||||
|
def create_anonymous(username: str, room: Room, db: Session):
|
||||||
def create_anonymous_member(username: str, room: Room, db: Session):
|
|
||||||
username = validate_username(username, room, db)
|
username = validate_username(username, room, db)
|
||||||
if username is None:
|
if username is None:
|
||||||
return None
|
return None
|
||||||
reconnect_code = generate_unique_code(
|
reconnect_code = generate_unique_code(
|
||||||
Anonymous, s=db, field_name="reconnect_code")
|
Anonymous, s=db, field_name="reconnect_code")
|
||||||
anonymous = Anonymous(username=username, reconnect_code=reconnect_code, clientId=uuid4())
|
anonymous = Anonymous(username=username, reconnect_code=reconnect_code, clientId=uuid4())
|
||||||
|
db.add(anonymous)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(anonymous)
|
||||||
|
return anonymous
|
||||||
|
|
||||||
|
|
||||||
|
def create_anonymous_member(username: str, room: Room, db: Session):
|
||||||
|
anonymous = create_anonymous(username, room, db)
|
||||||
member_id = generate_unique_code(Member, s=db)
|
member_id = generate_unique_code(Member, s=db)
|
||||||
member = Member(room=room, anonymous=anonymous, id_code=member_id)
|
member = Member(room=room, anonymous=anonymous, id_code=member_id)
|
||||||
db.add(member)
|
db.add(member)
|
||||||
@ -188,18 +195,6 @@ def create_anonymous_member(username: str, room: Room, db: Session):
|
|||||||
return member
|
return member
|
||||||
|
|
||||||
|
|
||||||
def create_anonymous(username: str, room: Room, db: Session):
|
|
||||||
username = validate_username(username, room, db)
|
|
||||||
if username is None:
|
|
||||||
return None
|
|
||||||
reconnect_code = generate_unique_code(
|
|
||||||
Anonymous, s=db, field_name="reconnect_code")
|
|
||||||
anonymous = Anonymous(username=username, reconnect_code=reconnect_code)
|
|
||||||
db.add(anonymous)
|
|
||||||
db.commit()
|
|
||||||
db.refresh(anonymous)
|
|
||||||
return anonymous
|
|
||||||
|
|
||||||
|
|
||||||
def check_user_in_room(user_id: int, room_id: int, db: Session):
|
def check_user_in_room(user_id: int, room_id: int, db: Session):
|
||||||
user = db.exec(select(Member).where(Member.user_id ==
|
user = db.exec(select(Member).where(Member.user_id ==
|
||||||
|
@ -88,7 +88,7 @@ class RoomConsumer(Consumer):
|
|||||||
await self.send_to_admin(type="disconnect_waiter", payload={"waiter": serialize_member(self.member)})
|
await self.send_to_admin(type="disconnect_waiter", payload={"waiter": serialize_member(self.member)})
|
||||||
|
|
||||||
async def loginMember(self, member: Member):
|
async def loginMember(self, member: Member):
|
||||||
if member.room_id == self.room.id and member.waiting == False:
|
if member.room_id == self.room.id and member.waiting is False:
|
||||||
self.member = member
|
self.member = member
|
||||||
await self.connect_self()
|
await self.connect_self()
|
||||||
self.add_to_group()
|
self.add_to_group()
|
||||||
@ -147,6 +147,8 @@ class RoomConsumer(Consumer):
|
|||||||
|
|
||||||
@Consumer.event('join')
|
@Consumer.event('join')
|
||||||
async def join(self, token: str | None = None, username: str | None = None):
|
async def join(self, token: str | None = None, username: str | None = None):
|
||||||
|
waiter = None
|
||||||
|
|
||||||
if token is not None:
|
if token is not None:
|
||||||
user = get_user_from_token(token, self.db)
|
user = get_user_from_token(token, self.db)
|
||||||
|
|
||||||
@ -168,8 +170,9 @@ class RoomConsumer(Consumer):
|
|||||||
|
|
||||||
elif username is not None:
|
elif username is not None:
|
||||||
if len(username) < 4 or len(username) > 15:
|
if len(username) < 4 or len(username) > 15:
|
||||||
await self.send_error("Nom d'utilisateur invalide ou indisponible")
|
await self.send_error("Nom invalide (4-15 caractères)")
|
||||||
return
|
return
|
||||||
|
|
||||||
anonymous = create_anonymous(username, self.room, self.db)
|
anonymous = create_anonymous(username, self.room, self.db)
|
||||||
if anonymous is None:
|
if anonymous is None:
|
||||||
await self.send_error("Nom d'utilisateur invalide ou indisponible")
|
await self.send_error("Nom d'utilisateur invalide ou indisponible")
|
||||||
@ -178,6 +181,10 @@ class RoomConsumer(Consumer):
|
|||||||
waiter = create_member(
|
waiter = create_member(
|
||||||
anonymous=anonymous, room=self.room, waiting=self.room.public is False, db=self.db)
|
anonymous=anonymous, room=self.room, waiting=self.room.public is False, db=self.db)
|
||||||
|
|
||||||
|
if member is None:
|
||||||
|
await self.send_error("Une erreur est survenue")
|
||||||
|
return
|
||||||
|
|
||||||
self.member = waiter
|
self.member = waiter
|
||||||
self.add_to_group()
|
self.add_to_group()
|
||||||
|
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
from fastapi import APIRouter, Depends
|
|
||||||
from services.auth import get_current_user_optional
|
|
||||||
from database.rooms.crud import create_room_with_user_db, create_room_anonymous_db, get_room_db
|
|
||||||
from schemas.rooms import AnonymousIn_schema, RoomIn_schema, Room_schema
|
|
||||||
from database.auth.models import User
|
|
||||||
router = APIRouter()
|
|
||||||
|
|
||||||
|
|
||||||
@router.post('/rooms', response_model=Room_schema)
|
|
||||||
async def create_room(roomData: RoomIn_schema, anonymous: AnonymousIn_schema = None, user: User = Depends(get_current_user_optional)):
|
|
||||||
if user is not None:
|
|
||||||
room = await create_room_with_user_db(room=roomData, user=user)
|
|
||||||
return await Room_schema.from_tortoise_orm(room)
|
|
||||||
else:
|
|
||||||
room = await create_room_anonymous_db(room=roomData, anonymous=anonymous)
|
|
||||||
return await Room_schema.from_tortoise_orm(room)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/room/{room_id}')
|
|
||||||
async def get_room(room_id: str):
|
|
||||||
room = await get_room_db(room_id)
|
|
||||||
if room is None:
|
|
||||||
return None
|
|
||||||
return await Room_schema.from_tortoise_orm(room)
|
|
Loading…
Reference in New Issue
Block a user