Skip to content

Commit

Permalink
feat!: MeRoleを削除
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Feb 20, 2024
1 parent bfce899 commit a4c878c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 62 deletions.
17 changes: 6 additions & 11 deletions mipac/actions/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from typing import TYPE_CHECKING, AsyncGenerator

from mipac.abstract.action import AbstractAction
from mipac.config import config
from mipac.http import HTTPClient, Route
from mipac.models.note import Note
from mipac.models.roles import MeRole, Role, RoleUser
from mipac.models.roles import Role, RoleUser
from mipac.types.note import INote
from mipac.types.roles import IMeRole, IRole, IRoleUser, is_me_role
from mipac.types.roles import IRole, IRoleUser
from mipac.utils.pagination import Pagination

if TYPE_CHECKING:
Expand Down Expand Up @@ -64,7 +63,7 @@ async def get_users(
limit: int = 10,
*,
get_all: bool = False,
) -> AsyncGenerator[MeRole | RoleUser, None]:
) -> AsyncGenerator[RoleUser, None]:
"""
Get users in a role.
Endpoint: `/api/roles/users`
Expand All @@ -84,7 +83,7 @@ async def get_users(
Yields
------
AsyncGenerator[MeRole | RoleUser, None]
AsyncGenerator[RoleUser, None]
The role user data.
"""

Expand All @@ -96,17 +95,13 @@ async def get_users(

body = {"roleId": role_id, "limit": limit, "sinceId": since_id, "untilId": until_id}

pagination = Pagination[IRoleUser | IMeRole](
pagination = Pagination[IRoleUser](
self.__session, Route("POST", "/api/roles/users"), auth=True, json=body
)
while True:
raw_users = await pagination.next()
for raw_user in raw_users:
yield (
MeRole(raw_user, client=self.__client)
if is_me_role(raw_user, config.account_id)
else RoleUser(raw_user, client=self.__client)
)
yield RoleUser(raw_user, client=self.__client)

if pagination.is_final or get_all is False:
break
Expand Down
43 changes: 5 additions & 38 deletions mipac/models/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

from mipac.models.lite.role import PartialRole
from mipac.models.user import MeDetailed, UserDetailedNotMe, packed_user
from mipac.types.roles import IMeRole, IRole, IRolePolicies, IRoleUser
from mipac.types.roles import IRole, IRolePolicies, IRoleUser
from mipac.utils.format import str_to_datetime

if TYPE_CHECKING:
from mipac.manager.client import ClientManager
from mipac.manager.user import UserManager


class RoleUser:
Expand All @@ -23,40 +22,12 @@ def id(self) -> str:
return self.__role_user["id"]

@property
def user(self) -> UserDetailedNotMe | MeDetailed:
return packed_user(self.__role_user["user"], client=self.__client)
def created_at(self) -> datetime:
return str_to_datetime(self.__role_user["created_at"])

@property
def expires_at(self) -> datetime | None:
return (
str_to_datetime(self.__role_user["expires_at"])
if self.__role_user["expires_at"]
else None
)

@property
def action(self) -> UserManager:
return self.__client._create_user_instance(self.user)

def __eq__(self, __value: object) -> bool:
return isinstance(__value, RoleUser) and self.id == __value.id

def __ne__(self, __value: object) -> bool:
return not self.__eq__(__value)


class MeRole:
def __init__(self, data: IMeRole, *, client: ClientManager) -> None:
self.__role_user = data
self.__client = client

@property
def id(self) -> str:
return self.__role_user["id"]

@property
def user(self) -> MeDetailed:
return MeDetailed(self.__role_user["user"], client=self.__client)
def user(self) -> UserDetailedNotMe | MeDetailed:
return packed_user(self.__role_user["user"], client=self.__client)

@property
def expires_at(self) -> datetime | None:
Expand All @@ -66,10 +37,6 @@ def expires_at(self) -> datetime | None:
else None
)

@property
def action(self) -> UserManager:
return self.__client._create_user_instance(self.user)

def __eq__(self, __value: object) -> bool:
return isinstance(__value, RoleUser) and self.id == __value.id

Expand Down
19 changes: 6 additions & 13 deletions mipac/types/roles.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Literal, TypedDict, TypeGuard
from typing import TYPE_CHECKING, Literal, TypedDict

from mipac.types.user import IUserDetailedNotMeSchema

if TYPE_CHECKING:
from mipac.types.user import IMeDetailedSchema, IUserDetailed
from mipac.types.user import IMeDetailedSchema


class IRoleUser(TypedDict):
id: str
user: IUserDetailed
expires_at: str | None


class IMeRole(TypedDict):
id: str
user: IMeDetailedSchema
created_at: str
user: IUserDetailedNotMeSchema | IMeDetailedSchema
expires_at: str | None


Expand Down Expand Up @@ -67,7 +64,3 @@ class IRole(IPartialRole):
can_edit_members_by_moderator: bool
policies: IRolePolicies
users_count: int


def is_me_role(data: IMeRole | IRoleUser, me_id: str) -> TypeGuard[IMeRole]:
return data["user"]["id"] == me_id and data["user"].get("avatar_id") is not None

0 comments on commit a4c878c

Please sign in to comment.