Skip to content

Commit

Permalink
refactor: ClientPartialUserListActionsを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Dec 8, 2023
1 parent ad0720b commit de8b8a2
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 44 deletions.
108 changes: 78 additions & 30 deletions mipac/actions/users/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
16 changes: 8 additions & 8 deletions mipac/manager/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
29 changes: 23 additions & 6 deletions mipac/manager/users/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit de8b8a2

Please sign in to comment.