2022-09-18 22:43:04 +02:00
|
|
|
from fastapi import Depends
|
2022-09-25 22:32:19 +02:00
|
|
|
from sqlmodel import Session, select, col
|
2022-09-18 22:43:04 +02:00
|
|
|
from database.db import get_session
|
2022-09-26 10:04:02 +02:00
|
|
|
from database.room.models import Anonymous, Member, Room, RoomCreate, Waiter, MemberRead
|
2022-09-16 21:50:55 +02:00
|
|
|
from database.auth.models import User
|
2022-09-18 22:43:04 +02:00
|
|
|
from services.database import generate_unique_code
|
2022-09-25 22:32:19 +02:00
|
|
|
from database.auth.crud import get_user_from_token
|
|
|
|
|
|
|
|
|
|
|
|
def check_room(room_id: str, db: Session = Depends(get_session)):
|
2022-09-26 10:04:02 +02:00
|
|
|
room = db.exec(select(Room).where(Room.id_code == room_id)).first()
|
|
|
|
return room
|
2022-09-25 22:32:19 +02:00
|
|
|
|
2022-09-16 21:50:55 +02:00
|
|
|
|
2022-09-26 10:04:02 +02:00
|
|
|
def create_room_db(*, room: RoomCreate, user: User | None = None, username: str | None = None, db: Session):
|
|
|
|
id_code = generate_unique_code(Room, s=db)
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
room_obj = Room(**room.dict(exclude_unset=True), id_code=id_code)
|
|
|
|
if user is not None:
|
|
|
|
member = Member(user_id=user.id, room=room_obj,
|
|
|
|
is_admin=True, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
if username is not None:
|
|
|
|
reconnect_code = generate_unique_code(
|
|
|
|
Anonymous, s=db, field_name='reconnect_code')
|
|
|
|
anonymous = Anonymous(username=username, reconnect_code=reconnect_code)
|
|
|
|
member = Member(anonymous=anonymous, room=room_obj,
|
|
|
|
is_admin=True, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
if username is None and user is None:
|
|
|
|
raise ValueError('Username or user required')
|
|
|
|
|
|
|
|
return {"room": room_obj, "member": member}
|
2022-09-18 22:43:04 +02:00
|
|
|
|
|
|
|
|
2022-09-25 22:32:19 +02:00
|
|
|
def get_member_from_user(user_id: int, room_id: int, db: Session):
|
|
|
|
member = db.exec(select(Member).where(Member.room_id ==
|
|
|
|
room_id, Member.user_id == user_id)).first()
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_member_from_token(token: str, room_id: int, db: Session):
|
|
|
|
user = get_user_from_token(token, db)
|
|
|
|
if user is False:
|
|
|
|
return False
|
|
|
|
if user is None:
|
|
|
|
return None
|
|
|
|
member = get_member_from_user(user.id, room_id, db)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_member_from_anonymous(anonymous_id: int, room_id: int, db: Session):
|
|
|
|
member = db.exec(select(Member).where(Member.room_id ==
|
|
|
|
room_id, Member.anonymous_id == anonymous_id)).first()
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_member_from_reconnect_code(reconnect_code: str, room_id: int, db: Session):
|
|
|
|
anonymous = get_anonymous_from_code(reconnect_code, db)
|
|
|
|
if anonymous is None:
|
|
|
|
return None
|
|
|
|
member = get_member_from_anonymous(anonymous.id, room_id, db)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_anonymous_from_code(reconnect_code: str, db: Session):
|
|
|
|
anonymous = db.exec(select(Anonymous).where(
|
|
|
|
Anonymous.reconnect_code == reconnect_code)).first()
|
|
|
|
return anonymous
|
|
|
|
|
|
|
|
|
2022-09-26 10:04:02 +02:00
|
|
|
def create_member(*, room: Room, user: User | None = None, anonymous: Anonymous | None = None, waiting: bool = False, db: Session):
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, user=user, anonymous=anonymous, waiting=waiting,
|
|
|
|
id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_or_create_member(*, room: Room, user: User | None = None, anonymous: Anonymous | None = None, waiting: bool = False, db: Session):
|
|
|
|
member = user is not None and get_member_from_user(user.id, room.id, db)
|
|
|
|
if member is not None and member is not False:
|
|
|
|
return member
|
|
|
|
member= create_member(room=room, user=user, anonymous=anonymous, waiting=waiting, db=db)
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-25 22:32:19 +02:00
|
|
|
def connect_member(member: Member, db: Session):
|
|
|
|
member.online = True
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def disconnect_member(member: Member, db: Session):
|
|
|
|
if member.waiting == False:
|
|
|
|
member.online = False
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
else:
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def validate_username(username: str, room: Room, db: Session = Depends(get_session)):
|
|
|
|
if len(username) > 20:
|
|
|
|
return None
|
|
|
|
members = select(Member.anonymous_id).where(
|
|
|
|
Member.room_id == room.id, Member.anonymous_id != None)
|
|
|
|
anonymous = select(Anonymous).where(
|
|
|
|
col(Anonymous.id).in_(members), Anonymous.username == username)
|
|
|
|
username_anonymous = db.exec(anonymous).first()
|
|
|
|
return None if username_anonymous is not None else username
|
|
|
|
|
|
|
|
|
|
|
|
def create_anonymous_member(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)
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, anonymous=anonymous, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
2022-09-26 10:04:02 +02:00
|
|
|
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
|
2022-09-25 22:32:19 +02:00
|
|
|
|
2022-09-26 10:04:02 +02:00
|
|
|
def check_user_in_room(user_id: int, room_id: int, db: Session):
|
|
|
|
user = db.exec(select(Member).where(Member.user_id==user_id, Member.room_id == room_id)).first()
|
|
|
|
return user
|
2022-09-25 22:32:19 +02:00
|
|
|
|
|
|
|
def create_user_member(user: User, room: Room, db: Session):
|
|
|
|
member = get_member_from_user(user.id, room.id, db)
|
|
|
|
if member is not None:
|
|
|
|
return None
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, user=user, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def create_anonymous_waiter(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)
|
|
|
|
|
|
|
|
member_id = generate_unique_code(Member, s=db)
|
|
|
|
member = Member(room=room, anonymous=anonymous,
|
|
|
|
waiting=True, id_code=member_id)
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
2022-09-26 10:04:02 +02:00
|
|
|
|
|
|
|
|
2022-09-25 22:32:19 +02:00
|
|
|
def create_user_waiter(user: User, room: Room, db: Session):
|
|
|
|
member = get_member_from_user(user.id, room.id, db)
|
|
|
|
if member is not None:
|
|
|
|
return member
|
2022-09-26 10:04:02 +02:00
|
|
|
|
|
|
|
member = create_member(room=room, user=user, waiting=True,
|
|
|
|
db=db)
|
2022-09-25 22:32:19 +02:00
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def get_waiter(waiter_code: str, db: Session):
|
2022-09-26 10:04:02 +02:00
|
|
|
return db.exec(select(Member).where(Member.id_code == waiter_code, Member.waiting == True)).first()
|
|
|
|
|
|
|
|
|
|
|
|
def get_member(id_code: str, room_id: str, db: Session):
|
|
|
|
return db.exec(select(Member).where(Member.id_code == id_code, Member.room_id == room_id)).first()
|
2022-09-25 22:32:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def delete_member(member: Member, db: Session):
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return None
|
|
|
|
|
2022-09-26 10:04:02 +02:00
|
|
|
|
2022-09-25 22:32:19 +02:00
|
|
|
def accept_waiter(member: Member, db: Session):
|
|
|
|
member.waiting = False
|
|
|
|
member.waiter_code = None
|
|
|
|
db.add(member)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(member)
|
|
|
|
return member
|
|
|
|
|
|
|
|
|
|
|
|
def refuse_waiter(member: Member, db: Session):
|
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return None
|
|
|
|
|
2022-09-26 10:04:02 +02:00
|
|
|
|
2022-09-25 22:32:19 +02:00
|
|
|
def leave_room(member: Member, db: Session):
|
2022-09-26 10:04:02 +02:00
|
|
|
db.delete(member)
|
|
|
|
db.commit()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def serialize_member(member: Member) -> MemberRead | Waiter:
|
|
|
|
member_obj = member.user or member.anonymous
|
|
|
|
if member.waiting == False:
|
|
|
|
return MemberRead(username=member_obj.username, reconnect_code=getattr(member_obj, "reconnect_code", ""), isUser=member.user_id != None, isAdmin=member.is_admin, id_code=member.id_code).dict()
|
|
|
|
if member.waiting == True:
|
|
|
|
return Waiter(username=member_obj.username, waiter_id=member.id_code).dict()
|