This commit is contained in:
Lilian 2023-02-28 11:55:44 +01:00 committed by Kilton937342
parent 642580ccd5
commit 2119973dbb
2 changed files with 19 additions and 17 deletions

View File

@ -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 ==

View File

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