diff --git a/mipac/actions/users/list.py b/mipac/actions/users/list.py index 89c31922..96fd9beb 100644 --- a/mipac/actions/users/list.py +++ b/mipac/actions/users/list.py @@ -13,7 +13,71 @@ from mipac.client import ClientManager -class ClientListActions(AbstractAction): +class ClientPartialUserListActions(AbstractAction): + """ユーザー向けのリストのアクションを提供します。""" + def __init__(self, user_id: str | None = None, *, session: HTTPClient, client: ClientManager): + self.__user_id: str | None = user_id + self._session: HTTPClient = session + self._client: ClientManager = client + + async def get_list(self, *, user_id: str | None = None) -> list[UserList]: + """Get the user lists of a user + + Endpoint `/api/users/lists/list` + + Parameters + ---------- + user_id : str + The id of the user to get the lists of + + Returns + ------- + list[UserList] + The user lists the user has + """ + + user_id = user_id or self.__user_id + + if user_id is None: + raise ParameterError("required parameter user_id is missing") + + raw_user_lists: list[IUserList] = await self._session.request( + Route("POST", "/api/users/lists/list"), json={"userId": user_id}, auth=True + ) + + return [UserList(raw_user_list, client=self._client) for raw_user_list in raw_user_lists] + + async def pull(self, list_id: str, *, user_id: str | None = None) -> bool: + """Pull a user from a user list + + Endpoint `/api/users/lists/pull` + + Parameters + ---------- + list_id : str + The id of the user list to pull from + user_id : str, optional + The id of the user to pull, by default None + + Returns + ------- + bool + True if the user was pulled, False otherwise + """ + user_id = user_id or self.__user_id + + if user_id is None: + raise ParameterError("required parameter user_id is missing") + + res: bool = await self._session.request( + Route("POST", "/api/users/lists/pull"), + json={"listId": list_id, "userId": user_id}, + auth=True, + ) + return res + + +class ClientUserListActions(ClientPartialUserListActions): def __init__( self, list_id: str | None = None, @@ -22,10 +86,8 @@ def __init__( session: HTTPClient, client: ClientManager, ): + super().__init__(user_id=user_id, session=session, client=client) self.__list_id: str | None = list_id - self.__user_id: str | None = user_id - self._session: HTTPClient = session - self._client: ClientManager = client async def delete(self, *, list_id: str | None = None) -> bool: """Delete a user list @@ -49,35 +111,21 @@ async def delete(self, *, list_id: str | None = None) -> bool: ) return res - async def get_list(self, *, user_id: str | None = None) -> list[UserList]: - """Get the user lists of a user - - Endpoint `/api/users/lists/list` - - Parameters - ---------- - user_id : str - The id of the user to get the lists of - - Returns - ------- - list[UserList] - The user lists the user has - """ - - user_id = user_id or self.__user_id + @override + async def get_list(self, user_id: str) -> list[UserList]: + return await super().get_list(user_id=user_id) - if user_id is None: - raise ParameterError("required parameter user_id is missing") + @override + async def pull(self, user_id: str, *, list_id: str | None = None) -> bool: + list_id = list_id or self.__list_id - raw_user_lists: list[IUserList] = await self._session.request( - Route("POST", "/api/users/lists/list"), json={"userId": user_id}, auth=True - ) + if list_id is None: + raise ParameterError("required parameter list_id is missing") - return [UserList(raw_user_list, client=self._client) for raw_user_list in raw_user_lists] + return await super().pull(list_id=list_id, user_id=user_id) -class UserListActions(ClientListActions): +class UserListActions(ClientUserListActions): def __init__(self, *, session: HTTPClient, client: ClientManager): super().__init__(session=session, client=client) @@ -106,5 +154,5 @@ async def delete(self, list_id: str) -> bool: return await super().delete(list_id=list_id) @override - async def get_list(self, user_id: str) -> list[UserList]: - return await super().get_list(user_id=user_id) + async def pull(self, list_id: str, user_id: str) -> bool: + return await super().pull(list_id=list_id, user_id=user_id) diff --git a/mipac/manager/user.py b/mipac/manager/user.py index a82808aa..d20827a6 100644 --- a/mipac/manager/user.py +++ b/mipac/manager/user.py @@ -8,7 +8,11 @@ from mipac.manager.blocking import BlockingManager from mipac.manager.follow import FollowManager from mipac.manager.mute import MuteManager -from mipac.manager.users.list import ClientUserListManager, UserListManager +from mipac.manager.users.list import ( + ClientPartialUserListManager, + ClientUserListManager, + UserListManager, +) if TYPE_CHECKING: from mipac.manager.client import ClientManager @@ -28,7 +32,7 @@ def __init__(self, user: PartialUser, *, session: HTTPClient, client: ClientMana # self.follow: FollowManager = FollowManager(session=session, client=client) TODO: Client版のFollowManagerを作る # self.mute: MuteManager = MuteManager(session=session, client=client) TODO: Client版のMuteManagerを作る # self.block = BlockingManager(session=session, client=client) TODO: Client版のBlockingManagerを作る - self.list = ClientUserListManager(user_id=user.id, session=session, client=client) + self.list = ClientPartialUserListManager(user_id=user.id, session=session, client=client) @property def action(self) -> ClientUserActions: @@ -49,9 +53,5 @@ def __init__(self, *, session: HTTPClient, client: ClientManager): def action(self) -> UserActions: return self.__actions - def _create_client_user_list_manager( - self, list_id: str, user_id: str | None = None - ) -> ClientUserListManager: - return ClientUserListManager( - list_id=list_id, user_id=user_id, session=self.__session, client=self.__client - ) + def _create_client_user_list_manager(self, list_id: str) -> ClientUserListManager: + return ClientUserListManager(list_id=list_id, session=self.__session, client=self.__client) diff --git a/mipac/manager/users/list.py b/mipac/manager/users/list.py index b5831196..c9091ff6 100644 --- a/mipac/manager/users/list.py +++ b/mipac/manager/users/list.py @@ -3,30 +3,47 @@ from mipac.abstract.manager import AbstractManager from mipac.http import HTTPClient -from mipac.actions.users.list import ClientListActions, UserListActions +from mipac.actions.users.list import ( + ClientPartialUserListActions, + ClientUserListActions, + UserListActions, +) if TYPE_CHECKING: from mipac.manager.client import ClientManager +class ClientPartialUserListManager(AbstractManager): + def __init__(self, user_id: str, *, session: HTTPClient, client: ClientManager): + self.__user_id: str = user_id + self.__session: HTTPClient = session + self.__client: ClientManager = client + self.__action: ClientPartialUserListActions = ClientPartialUserListActions( + user_id=self.__user_id, session=self.__session, client=self.__client + ) + + @property + def action(self) -> ClientPartialUserListActions: + return self.__action + + class ClientUserListManager(AbstractManager): def __init__( self, - list_id: str | None = None, - user_id: str | None = None, + list_id: str, *, session: HTTPClient, client: ClientManager, ): self.__session: HTTPClient = session self.__client: ClientManager = client - self.__action: ClientListActions = ClientListActions( - list_id=list_id, user_id=user_id, session=self.__session, client=self.__client + self.__action: ClientUserListActions = ClientUserListActions( + list_id=list_id, session=self.__session, client=self.__client ) @property - def action(self) -> ClientListActions: + def action(self) -> ClientUserListActions: return self.__action