clientid works
This commit is contained in:
@ -63,8 +63,15 @@ async def register(
|
||||
return user
|
||||
|
||||
|
||||
@router.get("/me", response_model=schemas.User)
|
||||
@router.get("/me")
|
||||
async def read_users_me(
|
||||
current_user: Annotated[schemas.User, Depends(services.get_current_active_user)],
|
||||
):
|
||||
) -> schemas.User:
|
||||
return current_user
|
||||
|
||||
|
||||
@router.get("/anon")
|
||||
async def get_qnon_user(
|
||||
anon_user: Annotated[schemas.AnonUser, Depends(services.get_anon_user)]
|
||||
) -> schemas.AnonUser:
|
||||
return anon_user
|
||||
|
||||
@ -27,3 +27,11 @@ class Token(BaseModel):
|
||||
|
||||
class TokenData(BaseModel):
|
||||
username: Union[str, None] = None
|
||||
|
||||
|
||||
class AnonUser(BaseModel):
|
||||
id: UUID
|
||||
name: Union[str, None] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from fastapi import status, HTTPException, Depends
|
||||
from fastapi import status, HTTPException, Depends, Header
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from sqlalchemy.orm import Session
|
||||
from jose import JWTError, jwt
|
||||
@ -65,7 +65,7 @@ def create_user(db: Session, user_data: schemas.UserRegister) -> schemas.UserInD
|
||||
return schemas.UserInDB.model_validate(user)
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
def get_current_user(
|
||||
token: Annotated[str, Depends(oauth2_scheme)],
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> schemas.UserInDB:
|
||||
@ -90,7 +90,7 @@ async def get_current_user(
|
||||
return user
|
||||
|
||||
|
||||
async def get_current_user_or_none(
|
||||
def get_current_user_or_none(
|
||||
token: Annotated[str, Depends(oauth2_scheme)],
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> Union[schemas.UserInDB, None]:
|
||||
@ -108,9 +108,36 @@ async def get_current_user_or_none(
|
||||
return user
|
||||
|
||||
|
||||
async def get_current_active_user(
|
||||
def get_current_active_user(
|
||||
current_user: Annotated[schemas.User, Depends(get_current_user)],
|
||||
):
|
||||
if not current_user.is_active:
|
||||
raise HTTPException(status_code=400, detail="Inactive user")
|
||||
return current_user
|
||||
|
||||
|
||||
def create_anon_user(db: Annotated[Session, Depends(get_db)]) -> schemas.AnonUser:
|
||||
u = models.AnonymousUser()
|
||||
db.add(u)
|
||||
db.commit()
|
||||
# return schemas.AnonUser.model_validate(u)
|
||||
return u
|
||||
|
||||
|
||||
def get_anon_user(
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
x_client_id: Annotated[Union[str, None], Header()] = None,
|
||||
) -> schemas.AnonUser:
|
||||
if x_client_id:
|
||||
anon = (
|
||||
db.query(models.AnonymousUser)
|
||||
.filter(models.AnonymousUser.id == x_client_id)
|
||||
.first()
|
||||
)
|
||||
if anon:
|
||||
return anon
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
detail={"message": "tf dude? trying to spoof your client id?"},
|
||||
)
|
||||
return create_anon_user(db)
|
||||
|
||||
@ -46,3 +46,10 @@ async def create_queue(
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> schemas.QueueInDb:
|
||||
return services.create_queue(new_queue=new_queue, current_user=current_user, db=db)
|
||||
|
||||
|
||||
@router.post("/{queue_id}/join")
|
||||
async def join_queue(
|
||||
queue_user: Annotated[schemas.QueueUser, Depends(services.join_queue)]
|
||||
) -> schemas.QueueUser:
|
||||
return queue_user
|
||||
|
||||
@ -30,3 +30,12 @@ class QueueInDb(Queue):
|
||||
class QueueDetail(Queue):
|
||||
id: UUID
|
||||
participants: ParticipantInfo
|
||||
|
||||
|
||||
class QueueUser(BaseModel):
|
||||
id: UUID
|
||||
position: int
|
||||
passed: bool
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@ -12,6 +12,11 @@ from ..auth import schemas as auth_schemas
|
||||
from . import schemas
|
||||
|
||||
|
||||
def get_queue_by_id(queue_id: UUID, db: Session) -> models.Queue:
|
||||
q = db.query(models.Queue).filter(models.Queue.id == queue_id).first()
|
||||
return q
|
||||
|
||||
|
||||
def get_user_queues(
|
||||
current_user: Annotated[auth_schemas.User, Depends(auth_services.get_current_user)]
|
||||
) -> list[schemas.QueueInDb]:
|
||||
@ -51,3 +56,29 @@ def get_detailed_queue(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Not Found",
|
||||
)
|
||||
|
||||
|
||||
def join_queue(
|
||||
queue_id: UUID,
|
||||
client: Annotated[auth_schemas.AnonUser, Depends(auth_services.get_anon_user)],
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> schemas.QueueUser:
|
||||
q = get_queue_by_id(queue_id, db)
|
||||
if q:
|
||||
if not q.users.filter(models.QueueUser.user_id == client.id).first():
|
||||
last_qu = q.users.order_by(models.QueueUser.position.desc()).first()
|
||||
position = last_qu.position + 1 if last_qu else 0
|
||||
new_qu = models.QueueUser(
|
||||
user_id=client.id, queue_id=q.id, position=position
|
||||
)
|
||||
db.add(new_qu)
|
||||
db.commit()
|
||||
return new_qu
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="Already joined",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Not Found",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user