refactor: note favorite 周りの実装を新しく

pull/125/head
yupix 8 months ago
parent e53fd41e7a
commit 5c11b80e62
No known key found for this signature in database
GPG Key ID: 2FF705F5C56D9C06

@ -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)

@ -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,

@ -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):

@ -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,

Loading…
Cancel
Save