refactor: userlist周りを再実装 progress #124

pull/109/head
yupix 7 months ago
parent e8ce50d50c
commit 36faacc1bc
No known key found for this signature in database
GPG Key ID: 2FF705F5C56D9C06

@ -15,15 +15,12 @@ if TYPE_CHECKING:
from mipac.client import ClientManager
class ClientPartialUserListActions(AbstractAction):
"""ユーザー向けのリストのアクションを提供します。"""
def __init__(self, user_id: str | None = None, *, session: HTTPClient, client: ClientManager):
self.__user_id: str | None = user_id
class SharedPartialUserListActions(AbstractAction):
def __init__(self, *, session: HTTPClient, client: ClientManager):
self._session: HTTPClient = session
self._client: ClientManager = client
async def get_list(self, *, user_id: str | None = None) -> list[UserList]:
async def get_list(self, *, user_id: str) -> list[UserList]:
"""Get the user lists of a user
Endpoint `/api/users/lists/list`
@ -38,19 +35,13 @@ class ClientPartialUserListActions(AbstractAction):
list[UserList]
The user lists the user has
"""
user_id = user_id or self.__user_id
if user_id is None:
raise ValueError("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:
async def pull(self, *, list_id: str, user_id: str) -> bool:
"""Pull a user from a user list
Endpoint `/api/users/lists/pull`
@ -67,11 +58,6 @@ class ClientPartialUserListActions(AbstractAction):
bool
True if the user was pulled, False otherwise
"""
user_id = user_id or self.__user_id
if user_id is None:
raise ValueError("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},
@ -79,7 +65,7 @@ class ClientPartialUserListActions(AbstractAction):
)
return res
async def push(self, list_id: str, *, user_id: str | None = None) -> bool:
async def push(self, *, list_id: str, user_id: str) -> bool:
"""Push a user to a user list
Endpoint `/api/users/lists/push`
@ -96,11 +82,6 @@ class ClientPartialUserListActions(AbstractAction):
bool
True if the user was pushed, False otherwise
"""
user_id = user_id or self.__user_id
if user_id is None:
raise ValueError("required parameter user_id is missing")
res: bool = await self._session.request(
Route("POST", "/api/users/lists/push"),
json={"listId": list_id, "userId": user_id},
@ -108,14 +89,7 @@ class ClientPartialUserListActions(AbstractAction):
)
return res
async def update_membership(
self, list_id: str, with_replies: bool = MISSING, *, user_id: str | None = None
):
user_id = user_id or self.__user_id
if user_id is None:
raise ValueError("required parameter user_id is missing")
async def update_membership(self, with_replies: bool = MISSING, *, list_id: str, user_id: str):
data = remove_dict_missing(
{"listId": list_id, "userId": user_id, "withReplies": with_replies}
)
@ -128,19 +102,97 @@ class ClientPartialUserListActions(AbstractAction):
return res
class ClientUserListActions(ClientPartialUserListActions):
class ClientPartialUserListActions(SharedPartialUserListActions):
"""ユーザー向けのリストのアクションを提供します。"""
def __init__(self, user_id: str, *, session: HTTPClient, client: ClientManager):
super().__init__(session=session, client=client)
self.__user_id: str = user_id
@override
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
return await super().get_list(user_id=user_id)
@override
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
return await super().pull(list_id=list_id, user_id=user_id)
@override
async def push(self, *, list_id: str, user_id: str | None = None) -> bool:
"""Push a user to a user list
Endpoint `/api/users/lists/push`
Parameters
----------
list_id : str
The id of the user list to push to
user_id : str, optional
The id of the user to push, by default None
Returns
-------
bool
True if the user was pushed, False otherwise
"""
user_id = user_id or self.__user_id
return await super().push(list_id=list_id, user_id=user_id)
@override
async def update_membership(
self, with_replies: bool = MISSING, *, list_id: str, user_id: str | None = None
):
user_id = user_id or self.__user_id
return await super().update_membership(
list_id=list_id, with_replies=with_replies, user_id=user_id
)
class SharedUserListActions(SharedPartialUserListActions):
def __init__(
self,
list_id: str | None = None,
*,
user_id: str | None = None,
session: HTTPClient,
client: ClientManager,
):
super().__init__(user_id=user_id, session=session, client=client)
self.__list_id: str | None = list_id
super().__init__(session=session, client=client)
async def delete(self, *, list_id: str | None = None) -> bool:
async def delete(self, *, list_id: str) -> bool:
"""Delete a user list
Endpoint `/api/users/lists/delete`
@ -155,36 +207,13 @@ class ClientUserListActions(ClientPartialUserListActions):
bool
True if the user list was deleted, False otherwise
"""
list_id = list_id or self.__list_id
res: bool = await self._session.request(
Route("POST", "/api/users/lists/delete"), json={"listId": list_id}, auth=True
)
return res
@override
async def get_list(self, user_id: str) -> list[UserList]:
return await super().get_list(user_id=user_id)
@override
async def pull(self, user_id: str, *, list_id: str | None = None) -> bool:
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
return await super().pull(list_id=list_id, user_id=user_id)
@override
async def push(self, user_id: str, *, list_id: str | None = None) -> bool:
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
return await super().push(list_id=list_id, user_id=user_id)
async def show(self, for_public: bool = False, *, list_id: str | None = None) -> UserList:
async def show(self, for_public: bool = False, *, list_id: str) -> UserList:
"""Show a user list
Endpoint `/api/users/lists/show`
@ -201,11 +230,6 @@ class ClientUserListActions(ClientPartialUserListActions):
UserList
The user list
"""
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
raw_user_list: IUserList = await self._session.request(
Route("POST", "/api/users/lists/show"),
json={"listId": list_id, "forPublic": for_public},
@ -213,7 +237,7 @@ class ClientUserListActions(ClientPartialUserListActions):
)
return UserList(raw_user_list=raw_user_list, client=self._client)
async def favorite(self, *, list_id: str | None = None) -> bool:
async def favorite(self, *, list_id: str) -> bool:
"""Favorite a user list
Endpoint `/api/users/lists/favorite`
@ -228,8 +252,6 @@ class ClientUserListActions(ClientPartialUserListActions):
bool
True if the user list was favorited, False otherwise
"""
list_id = list_id or self.__list_id
res: bool = await self._session.request(
Route("POST", "/api/users/lists/favorite"), json={"listId": list_id}, auth=True
)
@ -250,15 +272,13 @@ class ClientUserListActions(ClientPartialUserListActions):
bool
True if the user list was unfavorited, False otherwise
"""
list_id = list_id or self.__list_id
res: bool = await self._session.request(
Route("POST", "/api/users/lists/unfavorite"), json={"listId": list_id}, auth=True
)
return res
async def update(
self, name: str = MISSING, is_public: bool = MISSING, *, list_id: str | None = None
self, name: str = MISSING, is_public: bool = MISSING, *, list_id: str
) -> UserList:
"""Update a user list
@ -278,11 +298,6 @@ class ClientUserListActions(ClientPartialUserListActions):
UserList
The updated user list
"""
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
data = remove_dict_missing({"listId": list_id, "name": name, "public": is_public})
res: IUserList = await self._session.request(
@ -292,12 +307,7 @@ class ClientUserListActions(ClientPartialUserListActions):
)
return UserList(raw_user_list=res, client=self._client)
async def create_from_public(self, name: str, *, list_id: str | None = None):
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
async def create_from_public(self, name: str, *, list_id: str):
res: IUserList = await self._session.request(
Route("POST", "/api/users/lists/create-from-public"),
json={"listId": list_id, "name": name},
@ -305,18 +315,6 @@ class ClientUserListActions(ClientPartialUserListActions):
)
return UserList(raw_user_list=res, client=self._client)
@override
async def update_membership(
self, user_id: str, with_replies: bool = MISSING, list_id: str | None = None
):
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
return await super().update_membership(
user_id=user_id, with_replies=with_replies, list_id=list_id
)
async def get_memberships(
self,
for_public: bool = False,
@ -324,13 +322,8 @@ class ClientUserListActions(ClientPartialUserListActions):
since_id: str | None = None,
until_id: str | None = None,
*,
list_id: str | None = None,
list_id: str,
) -> list[UserListMembership]:
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
data = {
"listId": list_id,
"forPublic": for_public,
@ -357,13 +350,8 @@ class ClientUserListActions(ClientPartialUserListActions):
since_id: str | None = None,
until_id: str | None = None,
*,
list_id: str | None = None,
list_id: str,
):
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
data = {
"listId": list_id,
"forPublic": for_public,
@ -396,13 +384,8 @@ class ClientUserListActions(ClientPartialUserListActions):
with_renotes: bool = True,
with_files: bool = True,
*,
list_id: str | None = None,
list_id: str,
) -> list[Note]:
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
return await self._client.note.action.get_time_line(
list_id=list_id,
limit=limit,
@ -428,13 +411,8 @@ class ClientUserListActions(ClientPartialUserListActions):
with_renotes: bool = True,
with_files: bool = True,
*,
list_id: str | None = None,
list_id: str,
):
list_id = list_id or self.__list_id
if list_id is None:
raise ValueError("required parameter list_id is missing")
async for i in self._client.note.action.get_all_time_line(
list_id=list_id,
limit=limit,
@ -450,109 +428,177 @@ class ClientUserListActions(ClientPartialUserListActions):
yield i
class UserListActions(ClientUserListActions):
def __init__(self, *, session: HTTPClient, client: ClientManager):
class ClientUserListActions(SharedUserListActions):
def __init__(
self,
list_id: str,
*,
session: HTTPClient,
client: ClientManager,
):
super().__init__(session=session, client=client)
self.__list_id: str = list_id
async def create(self, name: str) -> UserList:
"""Create a new user list
@override
async def delete(self, *, list_id: str | None = None) -> bool:
"""Delete a user list
Endpoint `/api/users/lists/create`
Endpoint `/api/users/lists/delete`
Parameters
----------
name : str
The name of the new user list
list_id : str, optional
The id of the user list to delete, by default None
Returns
-------
UserList
The created user list
bool
True if the user list was deleted, False otherwise
"""
raw_user_list: IUserList = await self._session.request(
Route("POST", "/api/users/lists/create"), json={"name": name}, auth=True
)
return UserList(raw_user_list=raw_user_list, client=self._client)
list_id = list_id or self.__list_id
@override
async def delete(self, list_id: str) -> bool:
return await super().delete(list_id=list_id)
@override
async def pull(self, list_id: str, user_id: str) -> bool:
return await super().pull(list_id=list_id, user_id=user_id)
async def show(self, for_public: bool = False, *, list_id: str | None = None) -> UserList:
"""Show a user list
@override
async def push(self, list_id: str, user_id: str) -> bool:
return await super().push(list_id=list_id, user_id=user_id)
Endpoint `/api/users/lists/show`
@override
async def show(self, list_id: str, for_public: bool = False) -> UserList:
return await super().show(list_id=list_id, for_public=for_public)
Parameters
----------
for_public : bool, optional
Whether to show the user list for the public, by default False
list_id : str, optional
The id of the user list to show, by default None
Returns
-------
UserList
The user list
"""
list_id = list_id or self.__list_id
return await super().show(for_public=for_public, list_id=list_id)
@override
async def favorite(self, list_id: str) -> bool:
async def favorite(self, *, list_id: str | None = None) -> bool:
"""Favorite a user list
Endpoint `/api/users/lists/favorite`
Parameters
----------
list_id : str, optional
The id of the user list to favorite, by default None
Returns
-------
bool
True if the user list was favorited, False otherwise
"""
list_id = list_id or self.__list_id
return await super().favorite(list_id=list_id)
@override
async def unfavorite(self, list_id: str) -> bool:
async def unfavorite(self, *, list_id: str | None = None) -> bool:
"""Unfavorite a user list
Endpoint `/api/users/lists/unfavorite`
Parameters
----------
list_id : str, optional
The id of the user list to unfavorite, by default None
Returns
-------
bool
True if the user list was unfavorited, False otherwise
"""
list_id = list_id or self.__list_id
return await super().unfavorite(list_id=list_id)
@override
async def update(
self, list_id: str, name: str = MISSING, is_public: bool = MISSING
self, name: str = MISSING, is_public: bool = MISSING, *, list_id: str | None = None
) -> UserList:
"""Update a user list
Endpoint `/api/users/lists/update`
Parameters
----------
name : str, optional
The new name of the user list, by default MISSING
is_public : bool, optional
Whether the user list should be public, by default MISSING
list_id : str, optional
The id of the user list to update, by default None
Returns
-------
UserList
The updated user list
"""
list_id = list_id or self.__list_id
return await super().update(name=name, is_public=is_public, list_id=list_id)
@override
async def create_from_public(self, list_id: str, name: str):
return await super().create_from_public(name=name, list_id=list_id)
async def create_from_public(self, name: str, *, list_id: str | None = None):
list_id = list_id or self.__list_id
@override
async def update_membership(self, list_id: str, user_id: str, with_replies: bool = MISSING):
return await super().update_membership(
list_id=list_id, user_id=user_id, with_replies=with_replies
)
return await super().create_from_public(name=name, list_id=list_id)
@override
async def get_memberships(
self,
list_id: str,
for_public: bool = False,
limit: int = 30,
since_id: str | None = None,
until_id: str | None = None,
*,
list_id: str | None = None,
) -> list[UserListMembership]:
list_id = list_id or self.__list_id
return await super().get_memberships(
list_id=list_id,
for_public=for_public,
limit=limit,
since_id=since_id,
until_id=until_id,
list_id=list_id,
)
@override
async def get_all_memberships(
self,
list_id: str,
for_public: bool = False,
limit: int = 30,
since_id: str | None = None,
until_id: str | None = None,
*,
list_id: str | None = None,
):
list_id = list_id or self.__list_id
async for i in super().get_all_memberships(
list_id=list_id,
for_public=for_public,
limit=limit,
since_id=since_id,
until_id=until_id,
list_id=list_id,
):
yield i
# ここからはusers/lists系じゃないが、ここにあってほしい物
@override
async def get_time_line(
self,
list_id: str,
limit: int = 10,
since_id: str | None = None,
until_id: str | None = None,
@ -562,9 +608,12 @@ class UserListActions(ClientUserListActions):
include_local_renotes: bool = True,
with_renotes: bool = True,
with_files: bool = True,
*,
list_id: str | None = None,
) -> list[Note]:
return await self._client.note.action.get_time_line(
list_id=list_id,
list_id = list_id or self.__list_id
return await super().get_time_line(
limit=limit,
since_id=since_id,
until_id=until_id,
@ -574,12 +623,12 @@ class UserListActions(ClientUserListActions):
include_local_renotes=include_local_renotes,
with_renotes=with_renotes,
with_files=with_files,
list_id=list_id,
)
@override
async def get_all_time_line(
self,
list_id: str,
limit: int = 10,
since_id: str | None = None,
until_id: str | None = None,
@ -589,9 +638,12 @@ class UserListActions(ClientUserListActions):
include_local_renotes: bool = True,
with_renotes: bool = True,
with_files: bool = True,
*,
list_id: str | None = None,
):
async for i in self._client.note.action.get_all_time_line(
list_id=list_id,
list_id = list_id or self.__list_id
async for i in super().get_all_time_line(
limit=limit,
since_id=since_id,
until_id=until_id,
@ -601,5 +653,31 @@ class UserListActions(ClientUserListActions):
include_local_renotes=include_local_renotes,
with_renotes=with_renotes,
with_files=with_files,
list_id=list_id,
):
yield i
class UserListActions(SharedUserListActions):
def __init__(self, *, session: HTTPClient, client: ClientManager):
super().__init__(session=session, client=client)
async def create(self, name: str) -> UserList:
"""Create a new user list
Endpoint `/api/users/lists/create`
Parameters
----------
name : str
The name of the new user list
Returns
-------
UserList
The created user list
"""
raw_user_list: IUserList = await self._session.request(
Route("POST", "/api/users/lists/create"), json={"name": name}, auth=True
)
return UserList(raw_user_list=raw_user_list, client=self._client)

Loading…
Cancel
Save