rumine-api-wrapper/app/handlers.py

66 lines
2.3 KiB
Python
Raw Normal View History

2024-03-07 14:06:53 +01:00
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_uuid(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"}
async def get_profile(schema: RequestSchema) -> UserSchema:
uuid = await database.get_uuid_by_username(schema)
if not uuid:
raise HTTPException(status_code=404, detail="User not found")
profile = UserSchema.model_construct(
username=schema.username,
uuid=uuid,
expiry=schema.validated_expiry
)
return profile