Skip to content

Commit

Permalink
whitelisted ckeys, probably stampella
Browse files Browse the repository at this point in the history
  • Loading branch information
Furrior committed Feb 1, 2025
1 parent 4084d62 commit ae9b0bf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
7 changes: 6 additions & 1 deletion app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


class Player(SQLModel, table=True):
__tablename__ = "player"
id: int = Field(default=None, primary_key=True)
# Actually is a pretty big int. Is way **too** big for a lot of software to handle
discord_id: str = Field(max_length=32, unique=True, index=True)
Expand All @@ -19,6 +20,7 @@ class Player(SQLModel, table=True):


class CkeyLinkToken(SQLModel, table=True):
__tablename__ = "ckey_link_token"
id: int = Field(default=None, primary_key=True)
ckey: str = Field(max_length=32, unique=True, index=True)
token: str = Field(
Expand All @@ -39,21 +41,24 @@ class WhitelistBase(SQLModel):


class Whitelist(WhitelistBase, table=True):
pass
__tablename__ = "whitelist"


class WhitelistBan(WhitelistBase, table=True):
__tablename__ = "whitelist_ban"
expiration_time: datetime = Field(
default_factory=lambda: datetime.now() + DEFAULT_WHITELIST_BAN_EXPIRATION_TIME)
reason: str | None = Field(max_length=1024)


class Auth(SQLModel, table=True):
__tablename__ = "auth"
id: int = Field(default=None, primary_key=True)
token_hash: str = Field(max_length=64, unique=True, index=True)


class Donation(SQLModel, table=True):
__tablename__ = "donation"
id: int = Field(default=None, primary_key=True)
player_id: int = Field(foreign_key="player.id", index=True)
amount: int = Field()
Expand Down
75 changes: 56 additions & 19 deletions app/routes/v1/wl/whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from app.database.models import Player, Whitelist, WhitelistBan
from app.deps import BEARER_DEP_RESPONSES, SessionDep, verify_bearer
from app.schemas.generic import PaginatedResponse
from app.schemas.generic import T, PaginatedResponse
from app.schemas.whitelist import (NewWhitelistBanBase, NewWhitelistBanCkey,
NewWhitelistBanDiscord, NewWhitelistBanInternal, NewWhitelistBase,
NewWhitelistCkey, NewWhitelistDiscord,
Expand Down Expand Up @@ -65,24 +65,11 @@ async def create_whitelist_helper(
logger.info("Whitelist created: %s", wl.model_dump_json())
return wl


@router.get("s/", # /whitelists
status_code=status.HTTP_200_OK,
responses={
status.HTTP_200_OK: {"description": "List of matching whitelists"},
status.HTTP_400_BAD_REQUEST: {"description": "Invalid filter combination"},
})
async def get_whitelists(session: SessionDep,
request: Request,
ckey: str | None = None,
discord_id: str | None = None,
wl_type: str | None = None,
active_only: bool = True,
page: int = 1,
page_size: int = 50) -> PaginatedResponse[Whitelist]:
selection = select(Whitelist).join(
Player, Player.id == Whitelist.player_id) # type: ignore

def filter_whitelists(selection: SelectOfScalar[Whitelist],
ckey: str | None = None,
discord_id: str | None = None,
wl_type: str | None = None,
active_only: bool = True) -> SelectOfScalar[Whitelist]:
if active_only:
selection = select_only_active_whitelists(selection)
if ckey is not None:
Expand All @@ -91,7 +78,13 @@ async def get_whitelists(session: SessionDep,
selection = selection.where(Player.discord_id == discord_id)
if wl_type is not None:
selection = selection.where(Whitelist.wl_type == wl_type)
return selection

def paginate_selection(session: SessionDep,
selection: SelectOfScalar[T],
request: Request,
page: int,
page_size: int) -> PaginatedResponse[T]:
total = session.exec(selection.with_only_columns(func.count())).first() # type: ignore # pylint: disable=not-callable
selection = selection.offset((page-1)*page_size).limit(page_size)
items = session.exec(selection).all()
Expand All @@ -104,6 +97,50 @@ async def get_whitelists(session: SessionDep,
current_url=request.url,
)

@router.get("s/", # /whitelists
status_code=status.HTTP_200_OK,
responses={
status.HTTP_200_OK: {"description": "List of matching whitelists"},
status.HTTP_400_BAD_REQUEST: {"description": "Invalid filter combination"},
})
async def get_whitelists(session: SessionDep,
request: Request,
ckey: str | None = None,
discord_id: str | None = None,
wl_type: str | None = None,
active_only: bool = True,
page: int = 1,
page_size: int = 50) -> PaginatedResponse[Whitelist]:
selection = select(Whitelist).join(
Player, Player.id == Whitelist.player_id) # type: ignore

selection = filter_whitelists(selection, ckey, discord_id, wl_type, active_only)

return paginate_selection(session, selection, request, page, page_size)


@router.get("/ckeys/",
status_code=status.HTTP_200_OK,
responses={
status.HTTP_200_OK: {"description": "Whitelistd ckeys"},
status.HTTP_400_BAD_REQUEST: {"description": "Invalid filter combination"},
})
async def get_whitelisted_ckeys(session: SessionDep,
request: Request,
wl_type: str | None = None,
active_only: bool = True,
page: int = 1,
page_size: int = 50) -> PaginatedResponse[str]:
selection = select(Player.ckey).join(
Whitelist, Player.id == Whitelist.player_id).distinct() # type: ignore

selection = filter_whitelists(selection,
wl_type=wl_type,
active_only=active_only)

return paginate_selection(session, selection, request, page, page_size)


WHITELIST_POST_RESPONSES = {
**BEARER_DEP_RESPONSES,
status.HTTP_201_CREATED: {"description": "Whitelist created"},
Expand Down

0 comments on commit ae9b0bf

Please sign in to comment.