Skip to content

Commit

Permalink
feat: blockingをclientとで分けた close #123
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Feb 20, 2024
1 parent e7bf18e commit cfad9fb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
59 changes: 43 additions & 16 deletions mipac/actions/blocking.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, AsyncGenerator
from typing import TYPE_CHECKING, AsyncGenerator, override

from mipac.abstract.action import AbstractAction
from mipac.http import HTTPClient, Route
Expand All @@ -12,25 +12,52 @@
from mipac.client import ClientManager


class BlockingActions(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
class SharedBlockingActions(AbstractAction):
def __init__(self, *, session: HTTPClient, client: ClientManager):
self._session: HTTPClient = session
self._client: ClientManager = client

async def add(self, user_id: str | None = None) -> UserDetailedNotMe | MeDetailed:
user_id = self.__user_id or user_id
res: IUserDetailed = await self.__session.request(
async def add(self, *, user_id: str) -> UserDetailedNotMe | MeDetailed:
res: IUserDetailed = await self._session.request(
Route("POST", "/api/blocking/create"), auth=True, json={"userId": user_id}, lower=True
)
return packed_user(res, client=self.__client)
return packed_user(res, client=self._client)

async def remove(self, user_id: str | None = None) -> UserDetailedNotMe | MeDetailed:
user_id = self.__user_id or user_id
res: IUserDetailed = await self.__session.request(
async def remove(self, *, user_id: str) -> UserDetailedNotMe | MeDetailed:
res: IUserDetailed = await self._session.request(
Route("POST", "/api/blocking/delete"), auth=True, json={"userId": user_id}, lower=True
)
return packed_user(res, client=self.__client)
return packed_user(res, client=self._client)


class ClientBlockingActions(SharedBlockingActions):
"""クライアント用のブロックアクション
基本的にoverride以外は行わない
"""

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 add(self, *, user_id: str | None = None) -> UserDetailedNotMe | MeDetailed:
user_id = user_id or self.__user_id
return await super().add(user_id=user_id)

@override
async def remove(self, *, user_id: str | None = None) -> UserDetailedNotMe | MeDetailed:
user_id = user_id or self.__user_id
return await super().remove(user_id=user_id)


class BlockingActions(SharedBlockingActions):
"""ブロックアクション
user_idを持たないメソッドのみを持ち、持つものはSharedBlockingActionsに実装する
"""

def __init__(self, *, session: HTTPClient, client: ClientManager):
super().__init__(session=session, client=client)

async def get_list(
self,
Expand All @@ -45,12 +72,12 @@ async def get_list(
data = {"limit": limit, "sinceId": since_id, "untilId": until_id}

pagination = Pagination[IBlockingUser](
self.__session, Route("POST", "/api/blocking/list"), json=data
self._session, Route("POST", "/api/blocking/list"), json=data
)

while True:
res_users = await pagination.next()
for user in res_users:
yield BlockingUser(user, client=self.__client)
yield BlockingUser(user, client=self._client)
if get_all is False or pagination.is_final:
break
25 changes: 17 additions & 8 deletions mipac/manager/blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
from typing import TYPE_CHECKING

from mipac.abstract.manager import AbstractManager
from mipac.actions.blocking import BlockingActions
from mipac.actions.blocking import BlockingActions, ClientBlockingActions
from mipac.http import HTTPClient

if TYPE_CHECKING:
from mipac.manager.client import ClientManager


class ClinetBlockingManager(BlockingActions):
def __init__(self, user_id: str, *, session: HTTPClient, client: ClientManager):

Check warning on line 14 in mipac/manager/blocking.py

View workflow job for this annotation

GitHub Actions / Qodana Community for Python

Missed call to '__init__' of the super class

Call to __init__ of super class is missed

Check warning on line 14 in mipac/manager/blocking.py

View workflow job for this annotation

GitHub Actions / Qodana Community for Python

Missed call to '__init__' of the super class

Call to __init__ of super class is missed
self.__action: ClientBlockingActions = ClientBlockingActions(
user_id=user_id, session=session, client=client
)

@property
def action(self) -> ClientBlockingActions:
return self.__action


class BlockingManager(AbstractManager):
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
def __init__(self, *, session: HTTPClient, client: ClientManager):
self.__action: BlockingActions = BlockingActions(
session=session, client=client
)

@property
def action(self) -> BlockingActions:
return BlockingActions(
user_id=self.__user_id, session=self.__session, client=self.__client
)
return self.__action
4 changes: 2 additions & 2 deletions mipac/manager/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mipac.abstract.manager import AbstractManager
from mipac.actions.user import ClientUserActions, UserActions

Check notice on line 6 in mipac/manager/user.py

View workflow job for this annotation

GitHub Actions / Qodana Community for Python

Accessing a protected member of a class or a module

'ClientUserActions' is not declared in __all__

Check notice on line 6 in mipac/manager/user.py

View workflow job for this annotation

GitHub Actions / Qodana Community for Python

Accessing a protected member of a class or a module

'ClientUserActions' is not declared in __all__
from mipac.http import HTTPClient
from mipac.manager.blocking import BlockingManager
from mipac.manager.blocking import ClinetBlockingManager, BlockingManager
from mipac.manager.follow import FollowManager
from mipac.manager.users.list import (
ClientPartialUserListManager,
Expand Down Expand Up @@ -33,7 +33,7 @@ def __init__(self, user: PartialUser, *, session: HTTPClient, client: ClientMana
self.mute: ClientMuteManager = ClientMuteManager(
user_id=user.id, session=session, client=client
)
# self.block = BlockingManager(session=session, client=client) TODO: Client版のBlockingManagerを作る
self.block = ClinetBlockingManager(user_id=user.id, session=session, client=client)
self.list = ClientPartialUserListManager(user_id=user.id, session=session, client=client)

@property
Expand Down

0 comments on commit cfad9fb

Please sign in to comment.