from app import app, database from db import RequestSchema, UserSchema from fastapi import HTTPException @app.get('/') async def root(): return { "answer": None } @app.post("/get_uuid/") async def get_uuid(request: RequestSchema): profile = await get_profile(schema=request) return { "username": profile.username, "uuid": profile.uuid } @app.post("/check_subscription/") async def check_subscription(request: RequestSchema): profile = await get_profile(schema=request) subscription_status = await database.check_subscription_by_nickname(user=profile) return {"has_subscription": subscription_status} @app.post("/check_ban_status/") async def check_ban_status(request: RequestSchema): profile = await get_profile(schema=request) ban_status = await database.check_ban_status_by_uuid(user=profile) return {"is_banned": not ban_status} @app.post("/unban/") async def unban_user(request: RequestSchema): profile = await get_profile(schema=request) status = (await check_ban_status(request=request))['is_banned'] if status: await database.unban_by_uuid(user=profile) return {"message": "User unbanned successfully"} return {"message": "User not banned"} @app.post("/grant_permissions/") async def grant_permissions(request: RequestSchema): profile = await get_profile(schema=request) status = (await check_subscription(request=request))['has_subscription'] if not status: await database.grant_permissions_by_uuid(user=profile) await grant_tab(request=request) return {"message": "Permissions granted successfully"} return {"message": "Permissions already granted"} @app.post("/grant_prefix/") async def grant_tab(request: RequestSchema): profile = await get_profile(schema=request) await database.grant_tab_by_username(user=profile, tab=request.tab) return {"message": "Tab granted successfully"} @app.post("/grant_subscribe/") async def grant_tab(request: RequestSchema): profile = await get_profile(schema=request) await database.update_subscription(user=profile) async def get_profile(schema: RequestSchema) -> UserSchema: await database.delete_expired_tab_users() uuid = await database.get_uuid_by_username(schema) if not uuid: raise HTTPException(status_code=404, detail="User not found") discord_id = await database.get_discord(schema) if not discord_id: raise HTTPException(status_code=404, detail="User not found in whitelist") has_sub = await database.check_subscription_by_nickname(schema) profile = UserSchema.model_construct( username=schema.username, uuid=uuid, discord_id=discord_id, expiry=schema.validated_expiry, has_sub=has_sub, tab=schema.tab ) print(profile) return profile