aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--backend/discord.py38
-rw-r--r--backend/models/discord_role.py40
-rw-r--r--backend/models/discord_user.py25
-rw-r--r--backend/routes/roles.py36
4 files changed, 130 insertions, 9 deletions
diff --git a/backend/discord.py b/backend/discord.py
index e5c7f8f..cf80cf3 100644
--- a/backend/discord.py
+++ b/backend/discord.py
@@ -1,16 +1,15 @@
"""Various utilities for working with the Discord API."""
import httpx
-from backend.constants import (
- DISCORD_API_BASE_URL, OAUTH2_CLIENT_ID, OAUTH2_CLIENT_SECRET
-)
+from backend import constants
+from backend.models import discord_role, discord_user
async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict:
async with httpx.AsyncClient() as client:
data = {
- "client_id": OAUTH2_CLIENT_ID,
- "client_secret": OAUTH2_CLIENT_SECRET,
+ "client_id": constants.OAUTH2_CLIENT_ID,
+ "client_secret": constants.OAUTH2_CLIENT_SECRET,
"redirect_uri": f"{redirect}/callback"
}
@@ -21,7 +20,7 @@ async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict
data["grant_type"] = "authorization_code"
data["code"] = code
- r = await client.post(f"{DISCORD_API_BASE_URL}/oauth2/token", headers={
+ r = await client.post(f"{constants.DISCORD_API_BASE_URL}/oauth2/token", headers={
"Content-Type": "application/x-www-form-urlencoded"
}, data=data)
@@ -32,10 +31,35 @@ async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict
async def fetch_user_details(bearer_token: str) -> dict:
async with httpx.AsyncClient() as client:
- r = await client.get(f"{DISCORD_API_BASE_URL}/users/@me", headers={
+ r = await client.get(f"{constants.DISCORD_API_BASE_URL}/users/@me", headers={
"Authorization": f"Bearer {bearer_token}"
})
r.raise_for_status()
return r.json()
+
+
+async def get_role_info() -> list[discord_role.DiscordRole]:
+ """Get information about the roles in the configured guild."""
+ async with httpx.AsyncClient() as client:
+ r = await client.get(
+ f"{constants.DISCORD_API_BASE_URL}/guilds/{constants.DISCORD_GUILD}/roles",
+ headers={"Authorization": f"Bot {constants.DISCORD_BOT_TOKEN}"}
+ )
+
+ r.raise_for_status()
+ return [discord_role.DiscordRole(**role) for role in r.json()]
+
+
+async def get_member(member_id: str) -> discord_user.DiscordMember:
+ """Get a member by ID from the configured guild."""
+ async with httpx.AsyncClient() as client:
+ r = await client.get(
+ f"{constants.DISCORD_API_BASE_URL}/guilds/{constants.DISCORD_GUILD}"
+ f"/members/{member_id}",
+ headers={"Authorization": f"Bot {constants.DISCORD_BOT_TOKEN}"}
+ )
+
+ r.raise_for_status()
+ return discord_user.DiscordMember(**r.json())
diff --git a/backend/models/discord_role.py b/backend/models/discord_role.py
new file mode 100644
index 0000000..9f0b7dd
--- /dev/null
+++ b/backend/models/discord_role.py
@@ -0,0 +1,40 @@
+import typing
+
+from pydantic import BaseModel
+
+
+class RoleTags(BaseModel):
+ """Meta information about a discord role."""
+
+ bot_id: typing.Optional[str]
+ integration_id: typing.Optional[str]
+ premium_subscriber: bool
+
+ def __init__(self, **data: typing.Any):
+ """
+ Handle the terrible discord API.
+
+ Discord only returns the premium_subscriber field if it's true,
+ meaning the typical validation process wouldn't work.
+
+ We manually parse the raw data to determine if the field exists, and give it a useful
+ bool value.
+ """
+ data["premium_subscriber"] = "premium_subscriber" in data.keys()
+ super().__init__(**data)
+
+
+class DiscordRole(BaseModel):
+ """Schema model of Discord guild roles."""
+
+ id: str
+ name: str
+ color: int
+ hoist: bool
+ icon: typing.Optional[str]
+ unicode_emoji: typing.Optional[str]
+ position: int
+ permissions: str
+ managed: bool
+ mentionable: bool
+ tags: typing.Optional[RoleTags]
diff --git a/backend/models/discord_user.py b/backend/models/discord_user.py
index 9f246ba..3f4209d 100644
--- a/backend/models/discord_user.py
+++ b/backend/models/discord_user.py
@@ -1,10 +1,11 @@
+import datetime
import typing as t
from pydantic import BaseModel
-class DiscordUser(BaseModel):
- """Schema model of Discord user for form response."""
+class _User(BaseModel):
+ """Base for discord users and members."""
# Discord default fields.
username: str
@@ -20,5 +21,25 @@ class DiscordUser(BaseModel):
premium_type: t.Optional[int]
public_flags: t.Optional[int]
+
+class DiscordUser(_User):
+ """Schema model of Discord user for form response."""
+
# Custom fields
admin: bool
+
+
+class DiscordMember(BaseModel):
+ """A discord guild member."""
+
+ user: _User
+ nick: t.Optional[str]
+ avatar: t.Optional[str]
+ roles: list[str]
+ joined_at: datetime.datetime
+ premium_since: t.Optional[datetime.datetime]
+ deaf: bool
+ mute: bool
+ pending: t.Optional[bool]
+ permissions: t.Optional[str]
+ communication_disabled_until: t.Optional[datetime.datetime]
diff --git a/backend/routes/roles.py b/backend/routes/roles.py
new file mode 100644
index 0000000..b18a04b
--- /dev/null
+++ b/backend/routes/roles.py
@@ -0,0 +1,36 @@
+import starlette.background
+from pymongo.database import Database
+from spectree import Response
+from starlette.authentication import requires
+from starlette.responses import JSONResponse
+from starlette.routing import Request
+
+from backend import discord, route
+from backend.validation import OkayResponse, api
+
+
+async def refresh_roles(database: Database) -> None:
+ """Connect to the discord API and refresh the roles database."""
+ roles = await discord.get_role_info()
+ roles_collection = database.get_collection("roles")
+ roles_collection.drop()
+ roles_collection.insert_many([role.dict() for role in roles])
+
+
+class RolesRoute(route.Route):
+ """Refreshes the roles database."""
+
+ name = "roles"
+ path = "/roles"
+
+ @requires(["authenticated", "admin"])
+ @api.validate(
+ resp=Response(HTTP_200=OkayResponse),
+ tags=["roles"]
+ )
+ async def patch(self, request: Request) -> JSONResponse:
+ """Refresh the roles database."""
+ return JSONResponse(
+ {"status": "ok"},
+ background=starlette.background.BackgroundTask(refresh_roles, request.state.db)
+ )