Skip to content

Commit

Permalink
feat: channel周りを整備 #64
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Apr 17, 2023
1 parent c9faf0a commit 7b3efaa
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
74 changes: 74 additions & 0 deletions mipac/actions/channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Literal, overload

from mipac.abstract.action import AbstractAction
from mipac.http import HTTPClient, Route
from mipac.models.channel import Channel
from mipac.models.lite.channel import ChannelLite
from mipac.types.channel import IChannel, IChannelLite

if TYPE_CHECKING:
from mipac.client import ClientManager


class ClientChannelActions(AbstractAction):
def __init__(
self, channel_id: str | None = None, *, session: HTTPClient, client: ClientManager
):
self._channel_id: str | None = channel_id
self._session: HTTPClient = session
self._client: ClientManager = client

async def favorite(self, channel_id: str | None = None):
if self._client._config.use_version < 13:
raise Exception()

channel_id = self._channel_id or channel_id
if channel_id is None:
raise Exception()

res: bool = await self._session.request(
Route('POST', '/api/channels/favorite'), auth=True, json={'channelId': channel_id}
)

return res

async def unfavorite(self, channel_id: str | None = None):

if self._client._config.use_version < 13:
raise Exception()

channel_id = self._channel_id or channel_id
if channel_id is None:
raise Exception()

res: bool = await self._session.request(
Route('POST', '/api/channels/unfavorite'), auth=True, json={'channelId': channel_id}
)

return res


class ChannelActions(ClientChannelActions):
def __init__(
self, channel_id: str | None = None, *, session: HTTPClient, client: ClientManager
):
super().__init__(channel_id=channel_id, session=session, client=client)

async def get_my_favorite(self) -> list[Channel]:
"""お気に入りに登録したチャンネルの一覧を取得します。
Returns
-------
Channel
チャンネル
"""
if self._client._config.use_version < 13:
raise Exception()

res: list[IChannel] = await self._session.request(
Route('POST', '/api/channels/my-favorites'), auth=True
)
return [Channel(i, client=self._client) for i in res]

21 changes: 21 additions & 0 deletions mipac/manager/channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

from __future__ import annotations
from typing import TYPE_CHECKING

from mipac.abstract.manager import AbstractManager
from mipac.http import HTTPClient
from mipac.actions.channel import ChannelActions

if TYPE_CHECKING:
from mipac.manager.client import ClientManager


class ChannelManager(AbstractManager):
def __init__(self, channel_id: str | None = None, *, session: HTTPClient, client: ClientManager):
self.__channel_id: str | None = channel_id
self.__session: HTTPClient = session
self.__client: ClientManager = client

@property
def action(self) -> ChannelActions:
return ChannelActions(channel_id=self.__channel_id, session=self.__session, client=self.__client)
3 changes: 3 additions & 0 deletions mipac/manager/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ def _create_user_instance(self, user: LiteUser) -> UserManager:
def _create_note_instance(self, note_id: str) -> NoteManager:
return NoteManager(note_id, session=self.__session, client=self)

def _create_channel_instance(self, channel_id: str) -> ChannelManager:
return ChannelManager(channel_id=channel_id, session=self.__session, client=self)

async def get_me(self) -> UserDetailed:
return await self.user.action.get_me()
23 changes: 14 additions & 9 deletions mipac/models/lite/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from datetime import datetime
from typing import TYPE_CHECKING, Generic, TypeVar

from mipac.types.channel import IChannelLite
from mipac.utils.format import str_to_datetime

if TYPE_CHECKING:
from mipac.manager import ClientManager
from mipac.manager.channel import ChannelManager


T = TypeVar('T', bound=IChannelLite)

Expand Down Expand Up @@ -39,21 +40,25 @@ def description(self) -> str | None:
return self._channel['description']

@property
def banner_url(self) -> str | None:
return self._channel['banner_url']
def user_id(self) -> str:
return self._channel['user_id']

@property
def notes_count(self) -> int:
return self._channel['notes_count']
def banner_url(self) -> str | None:
return self._channel['banner_url']

@property
def users_count(self) -> int:
return self._channel['users_count']

@property
def is_following(self) -> bool:
return bool(self._channel['is_following'])
def notes_count(self) -> int:
return self._channel['notes_count']

@property
def user_id(self) -> str:
return self._channel['user_id']
def pinned_note_ids(self) -> list:
return self._channel.get('pinned_note_ids', [])

@property
def api(self) -> ChannelManager:
return self.__client._create_channel_instance(channel_id=self.id)

0 comments on commit 7b3efaa

Please sign in to comment.