diff --git a/pyproject.toml b/pyproject.toml index c81856fe..5c1adfea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [tool.poetry] name = "royalnet" - version = "5.6" + version = "5.6.1" description = "A multipurpose bot and web framework" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/royalnet/backpack/stars/__init__.py b/royalnet/backpack/stars/__init__.py index 81d5a6c6..385cf9d7 100644 --- a/royalnet/backpack/stars/__init__.py +++ b/royalnet/backpack/stars/__init__.py @@ -4,6 +4,10 @@ from .api_login_royalnet import ApiLoginRoyalnetStar from .api_token_info import ApiTokenInfoStar from .api_token_passwd import ApiTokenPasswdStar from .api_token_create import ApiTokenCreateStar +from .api_user_get import ApiUserGetStar +from .api_user_list import ApiUserListStar +from .api_user_find import ApiUserFindStar +from .api_alias_list import ApiAliasListStar from .docs import DocsStar # Enter the PageStars of your Pack here! @@ -13,6 +17,10 @@ available_page_stars = [ ApiTokenInfoStar, ApiTokenPasswdStar, ApiTokenCreateStar, + ApiUserGetStar, + ApiUserListStar, + ApiUserFindStar, + ApiAliasListStar, DocsStar, ] diff --git a/royalnet/backpack/stars/api_alias_list.py b/royalnet/backpack/stars/api_alias_list.py new file mode 100644 index 00000000..66f73c18 --- /dev/null +++ b/royalnet/backpack/stars/api_alias_list.py @@ -0,0 +1,25 @@ +from starlette.responses import * +from royalnet.utils import * +from royalnet.backpack.tables import * +from royalnet.constellation.api import * + + +class ApiAliasListStar(ApiStar): + path = "/api/alias/list/v1" + + summary = "Get all aliases of the specified user." + + tags = ["alias"] + + parameters = { + "user_id": "The id of the user to get the aliases of." + } + + async def api(self, data: ApiData) -> JSON: + aliases: typing.List[Alias] = await asyncify( + data.session + .query(self.alchemy.get(Alias)) + .filter_by(user_id=data["user_id"]) + .all + ) + return [alias.alias for alias in aliases] diff --git a/royalnet/backpack/stars/api_login_royalnet.py b/royalnet/backpack/stars/api_login_royalnet.py index 23744346..86147465 100644 --- a/royalnet/backpack/stars/api_login_royalnet.py +++ b/royalnet/backpack/stars/api_login_royalnet.py @@ -18,7 +18,7 @@ class ApiLoginRoyalnetStar(ApiStar): "password": "The password of the user you are logging in as." } - tags = ["royalnet"] + tags = ["login"] async def api(self, data: ApiData) -> ru.JSON: TokenT = self.alchemy.get(Token) diff --git a/royalnet/backpack/stars/api_token_create.py b/royalnet/backpack/stars/api_token_create.py index cfae4133..831f3d7a 100644 --- a/royalnet/backpack/stars/api_token_create.py +++ b/royalnet/backpack/stars/api_token_create.py @@ -16,7 +16,7 @@ class ApiTokenCreateStar(ApiStar): "duration": "The duration in seconds of the new token." } - tags = ["royalnet"] + tags = ["token"] async def api(self, data: ApiData) -> ru.JSON: user = await data.user() diff --git a/royalnet/backpack/stars/api_token_info.py b/royalnet/backpack/stars/api_token_info.py index 606c6858..3986e6dd 100644 --- a/royalnet/backpack/stars/api_token_info.py +++ b/royalnet/backpack/stars/api_token_info.py @@ -7,7 +7,7 @@ class ApiTokenInfoStar(ApiStar): summary = "Get info the current login token." - tags = ["royalnet"] + tags = ["token"] async def api(self, data: ApiData) -> ru.JSON: token = await data.token() diff --git a/royalnet/backpack/stars/api_token_passwd.py b/royalnet/backpack/stars/api_token_passwd.py index d26ffe8f..29a28d4b 100644 --- a/royalnet/backpack/stars/api_token_passwd.py +++ b/royalnet/backpack/stars/api_token_passwd.py @@ -17,7 +17,7 @@ class ApiTokenPasswdStar(ApiStar): "new_password": "The password you want to set." } - tags = ["royalnet"] + tags = ["token"] async def api(self, data: ApiData) -> ru.JSON: TokenT = self.alchemy.get(Token) diff --git a/royalnet/backpack/stars/api_user_find.py b/royalnet/backpack/stars/api_user_find.py new file mode 100644 index 00000000..c61b3fc3 --- /dev/null +++ b/royalnet/backpack/stars/api_user_find.py @@ -0,0 +1,21 @@ +from royalnet.utils import * +from royalnet.backpack.tables import * +from royalnet.constellation.api import * + + +class ApiUserFindStar(ApiStar): + path = "/api/user/find/v1" + + summary = "Find a Royalnet user by one of their aliases." + + parameters = { + "alias": "One of the aliases of the user to get." + } + + tags = ["user"] + + async def api(self, data: ApiData) -> dict: + user = await Alias.find_user(self.alchemy, data.session, data["alias"]) + if user is None: + raise NotFoundError("No such user.") + return user.json() diff --git a/royalnet/backpack/stars/api_user_get.py b/royalnet/backpack/stars/api_user_get.py new file mode 100644 index 00000000..935da2de --- /dev/null +++ b/royalnet/backpack/stars/api_user_get.py @@ -0,0 +1,26 @@ +from royalnet.utils import * +from royalnet.backpack.tables import * +from royalnet.constellation.api import * + + +class ApiUserGetStar(ApiStar): + path = "/api/user/get/v1" + + summary = "Get a Royalnet user by its id." + + parameters = { + "id": "The id of the user to get." + } + + tags = ["user"] + + async def api(self, data: ApiData) -> dict: + user_id_str = data["id"] + try: + user_id = int(user_id_str) + except (ValueError, TypeError): + raise InvalidParameterError("'id' is not a valid int.") + user: User = await asyncify(data.session.query(self.alchemy.get(User)).get, user_id) + if user is None: + raise NotFoundError("No such user.") + return user.json() diff --git a/royalnet/backpack/stars/api_user_list.py b/royalnet/backpack/stars/api_user_list.py new file mode 100644 index 00000000..fd9053ed --- /dev/null +++ b/royalnet/backpack/stars/api_user_list.py @@ -0,0 +1,16 @@ +from starlette.responses import * +from royalnet.utils import * +from royalnet.backpack.tables import * +from royalnet.constellation.api import * + + +class ApiUserListStar(ApiStar): + path = "/api/user/list/v1" + + summary = "Get a list of all registered users." + + tags = ["user"] + + async def api(self, data: ApiData) -> dict: + users: typing.List[User] = await asyncify(data.session.query(self.alchemy.get(User)).all) + return [user.json() for user in users] diff --git a/royalnet/backpack/tables/aliases.py b/royalnet/backpack/tables/aliases.py index d153698a..8c2bd672 100644 --- a/royalnet/backpack/tables/aliases.py +++ b/royalnet/backpack/tables/aliases.py @@ -1,3 +1,4 @@ +from typing import * from sqlalchemy import Column, \ Integer, \ String, \ @@ -23,7 +24,7 @@ class Alias: return relationship("User", backref="aliases") @classmethod - async def find_user(cls, alchemy, session, alias: str): + async def find_user(cls, alchemy, session, alias: Union[str, int]): result = await ru.asyncify(session.query(alchemy.get(cls)).filter_by(alias=alias.lower()).one_or_none) if result is not None: result = result.user diff --git a/royalnet/version.py b/royalnet/version.py index 4705c69e..214a6363 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.6" +semantic = "5.6.1"