2023-08-01 02:38:55 +02:00
|
|
|
"""User management."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import fastapi
|
|
|
|
|
2023-08-03 01:46:49 +02:00
|
|
|
from db import users
|
2023-08-01 02:38:55 +02:00
|
|
|
|
2023-08-04 03:30:56 +02:00
|
|
|
from dhooks import Webhook, Embed
|
2023-08-01 02:38:55 +02:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
router = fastapi.APIRouter(tags=['core'])
|
|
|
|
|
|
|
|
async def check_core_auth(request):
|
|
|
|
received_auth = request.headers.get('Authorization')
|
|
|
|
|
|
|
|
if received_auth != os.getenv('CORE_API_KEY'):
|
|
|
|
return fastapi.Response(status_code=403, content='Invalid or no API key given.')
|
|
|
|
|
|
|
|
@router.get('/users')
|
|
|
|
async def get_users(discord_id: int, incoming_request: fastapi.Request):
|
|
|
|
auth_error = await check_core_auth(incoming_request)
|
|
|
|
|
|
|
|
if auth_error:
|
|
|
|
return auth_error
|
|
|
|
|
2023-08-03 01:46:49 +02:00
|
|
|
user = await users.by_discord_id(discord_id)
|
2023-08-01 02:38:55 +02:00
|
|
|
|
|
|
|
if not user:
|
|
|
|
return fastapi.Response(status_code=404, content='User not found.')
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-01 02:38:55 +02:00
|
|
|
return user
|
|
|
|
|
2023-08-06 21:42:07 +02:00
|
|
|
async def new_user_webhook(user: dict) -> None:
|
2023-08-04 03:30:56 +02:00
|
|
|
dhook = Webhook(os.getenv('DISCORD_WEBHOOK__USER_CREATED'))
|
|
|
|
|
|
|
|
embed = Embed(
|
|
|
|
description='New User',
|
|
|
|
color=0x90ee90,
|
2023-08-06 00:43:36 +02:00
|
|
|
)
|
2023-08-04 03:30:56 +02:00
|
|
|
|
2023-08-06 21:42:07 +02:00
|
|
|
embed.add_field(name='ID', value=str(user['_id']), inline=False)
|
2023-08-06 23:20:14 +02:00
|
|
|
embed.add_field(name='Discord', value=user['auth']['discord'] or '-')
|
|
|
|
embed.add_field(name='Github', value=user['auth']['github'] or '-')
|
2023-08-04 03:30:56 +02:00
|
|
|
|
|
|
|
dhook.send(embed=embed)
|
|
|
|
|
2023-08-01 02:38:55 +02:00
|
|
|
@router.post('/users')
|
|
|
|
async def create_user(incoming_request: fastapi.Request):
|
|
|
|
auth_error = await check_core_auth(incoming_request)
|
|
|
|
|
|
|
|
if auth_error:
|
2023-08-12 18:01:55 +02:00
|
|
|
return auth_error
|
2023-08-01 02:38:55 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
payload = await incoming_request.json()
|
|
|
|
discord_id = payload.get('discord_id')
|
|
|
|
except (json.decoder.JSONDecodeError, AttributeError):
|
|
|
|
return fastapi.Response(status_code=400, content='Invalid or no payload received.')
|
2023-08-04 03:30:56 +02:00
|
|
|
|
2023-08-03 01:46:49 +02:00
|
|
|
user = await users.create(discord_id)
|
2023-08-06 21:42:07 +02:00
|
|
|
await new_user_webhook(user)
|
2023-08-04 03:30:56 +02:00
|
|
|
|
2023-08-01 02:38:55 +02:00
|
|
|
return user
|
2023-08-12 18:01:55 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# new_user_webhook({
|
|
|
|
# '_id': 'JUST_A_TEST_IGNORE_ME',
|
|
|
|
# 'auth': {
|
|
|
|
# 'discord': 123,
|
|
|
|
# 'github': 'abc'
|
|
|
|
# }
|
|
|
|
# })
|
|
|
|
|
|
|
|
pass
|