diff --git a/mipac/actions/favorite.py b/mipac/actions/favorite.py index c2da140..8ab4a3e 100644 --- a/mipac/actions/favorite.py +++ b/mipac/actions/favorite.py @@ -1,39 +1,77 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, override from mipac.abstract.action import AbstractAction -from mipac.http import Route +from mipac.errors.base import ParameterError +from mipac.http import HTTPClient, Route +from mipac.utils.util import deprecated if TYPE_CHECKING: - from mipac.http import HTTPClient from mipac.manager.client import ClientManager -class FavoriteActions(AbstractAction): +class ClientFavoriteActions(AbstractAction): def __init__(self, note_id: str | None = None, *, session: HTTPClient, client: ClientManager): self.__note_id: str | None = note_id - self.__session: HTTPClient = session - self.__client: ClientManager = client + self._session: HTTPClient = session + self._client: ClientManager = client - async def add(self, note_id: str | None = None) -> bool: + async def create(self, *, note_id: str | None = None) -> bool: note_id = note_id or self.__note_id + + if note_id is None: + raise ParameterError("note_id is required") + data = {"noteId": note_id} return bool( - await self.__session.request( + await self._session.request( Route("POST", "/api/notes/favorites/create"), json=data, auth=True, ) ) - async def remove(self, note_id: str | None = None) -> bool: + @deprecated + async def add(self, *, note_id: str | None = None) -> bool: + return await self.create(note_id=note_id) + + async def delete(self, *, note_id: str | None = None) -> bool: note_id = note_id or self.__note_id + + if note_id is None: + raise ParameterError("note_id is required") + data = {"noteId": note_id} return bool( - await self.__session.request( + await self._session.request( Route("POST", "/api/notes/favorites/delete"), json=data, auth=True, ) ) + + @deprecated + async def remove(self, *, note_id: str | None = None) -> bool: + return await self.delete(note_id=note_id) + + +class FavoriteActions(ClientFavoriteActions): + def __init__(self, note_id: str | None = None, *, session: HTTPClient, client: ClientManager): + super().__init__(note_id, session=session, client=client) + + @override + async def create(self, note_id: str) -> bool: + return await super().create(note_id=note_id) + + @override + async def add(self, note_id: str) -> bool: + return await super().add(note_id=note_id) + + @override + async def delete(self, note_id: str) -> bool: + return await super().delete(note_id=note_id) + + @override + async def remove(self, note_id: str) -> bool: + return await super().remove(note_id=note_id) diff --git a/mipac/actions/note.py b/mipac/actions/note.py index 93e6b9a..654db46 100644 --- a/mipac/actions/note.py +++ b/mipac/actions/note.py @@ -268,6 +268,31 @@ class ClientNoteActions(AbstractAction): ) return [Note(note, client=self._client) for note in res] + async def delete(self, *, note_id: str | None = None) -> bool: + """Delete a note + + Endpoint: `/api/notes/delete` + + Parameters + ---------- + note_id : str | None, default=None + note id + + Returns + ------- + bool + success or not + """ + + note_id = note_id or self._note_id + + if note_id is None: + raise ParameterError("note_id is required") + + data = {"noteId": note_id} + res = await self._session.request(Route("POST", "/api/notes/delete"), json=data, auth=True) + return bool(res) + @cache(group="get_note_state") async def get_state(self, note_id: str | None = None) -> NoteState: """Get the state of the note @@ -344,31 +369,6 @@ class ClientNoteActions(AbstractAction): await self._session.request(Route("POST", "/api/clips/add-note"), json=data, auth=True) ) - async def delete(self, note_id: str | None = None) -> bool: - """Delete a note - - Endpoint: `/api/notes/delete` - - Parameters - ---------- - note_id : str | None, default=None - note id - - Returns - ------- - bool - success or not - """ - - note_id = note_id or self._note_id - - if note_id is None: - raise ParameterError("note_id is required") - - data = {"noteId": note_id} - res = await self._session.request(Route("POST", "/api/notes/delete"), json=data, auth=True) - return bool(res) - async def create_renote(self, note_id: str | None = None) -> Note: """Renote a note @@ -906,6 +906,10 @@ class NoteActions(ClientNoteActions): async def get_conversation(self, note_id: str, limit: int = 10, offset: int = 0) -> list[Note]: return await super().get_conversation(note_id=note_id, limit=limit, offset=offset) + @override + async def delete(self, note_id: str) -> bool: + return await super().delete(note_id=note_id) + @deprecated async def send( self, diff --git a/mipac/manager/favorite.py b/mipac/manager/favorite.py index a8bda39..7698dbf 100644 --- a/mipac/manager/favorite.py +++ b/mipac/manager/favorite.py @@ -3,12 +3,33 @@ from __future__ import annotations from typing import TYPE_CHECKING from mipac.abstract.manager import AbstractManager -from mipac.actions.favorite import FavoriteActions +from mipac.actions.favorite import ClientFavoriteActions, FavoriteActions from mipac.http import HTTPClient if TYPE_CHECKING: from mipac.client import ClientManager +class ClientFavoriteManager(AbstractManager): + def __init__(self, note_id: str, *, session: HTTPClient, client: ClientManager): + self.__note_id = note_id + self.__session: HTTPClient = session + self.__client: ClientManager = client + + @property + def action(self) -> ClientFavoriteActions: + """お気に入りに関するアクション + + Returns + ------- + ClientFavoriteActions + お気に入りに対するアクションを行うクラス + """ + return ClientFavoriteActions( + note_id=self.__note_id, + session=self.__session, + client=self.__client, + ) + class FavoriteManager(AbstractManager): def __init__(self, note_id: str | None = None, *, session: HTTPClient, client: ClientManager): diff --git a/mipac/manager/note.py b/mipac/manager/note.py index 6c9995b..8ebd291 100644 --- a/mipac/manager/note.py +++ b/mipac/manager/note.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING from mipac.abstract.manager import AbstractManager from mipac.actions.note import ClientNoteActions, NoteActions from mipac.http import HTTPClient -from mipac.manager.favorite import FavoriteManager +from mipac.manager.favorite import ClientFavoriteManager, FavoriteManager from mipac.manager.poll import ClientPollManager, PollManager from mipac.manager.reaction import ReactionManager @@ -14,14 +14,14 @@ if TYPE_CHECKING: class ClientNoteManager(AbstractManager): - def __init__(self, note_id: str | None = None, *, session: HTTPClient, client: ClientManager): + def __init__(self, note_id: str, *, session: HTTPClient, client: ClientManager): self.__note_id = note_id self.__session: HTTPClient = session self.__client: ClientManager = client self.reaction: ReactionManager = ReactionManager( note_id=note_id, session=session, client=client ) - self.favorite = FavoriteManager(note_id=note_id, session=session, client=client) + self.favorite = ClientFavoriteManager(note_id=note_id, session=session, client=client) self.poll: ClientPollManager = ClientPollManager( note_id=note_id, session=session, client=client ) @@ -46,9 +46,6 @@ class NoteManager(AbstractManager): note_id=note_id, session=session, client=client ) self.favorite = FavoriteManager(note_id=note_id, session=session, client=client) - self._client: ClientNoteManager = ClientNoteManager( - note_id=note_id, session=session, client=client - ) self.poll: PollManager = PollManager(note_id=note_id, session=session, client=client) self.__action: NoteActions = NoteActions( note_id=self.__note_id,