Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pymax/api/chats/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ChatPayloadKey(str, Enum):
CHAT = "chat"
CHATS = "chats"
MEMBERS = "members"
MARKER = "marker"


class ChatLinkPrefix(str, Enum):
Expand Down
7 changes: 7 additions & 0 deletions src/pymax/api/chats/payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ class GetChatInfoPayload(CamelModel):
chat_ids: list[int]


class GetChatMembersPayload(CamelModel):
type: str = "MEMBER" # TODO: ENUMM!!!
chat_id: int
marker: int
count: int = 50


class LeaveChatPayload(CamelModel):
chat_id: int

Expand Down
18 changes: 18 additions & 0 deletions src/pymax/api/chats/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pymax.api.response import (
parse_payload_item_model,
parse_payload_list,
payload_item,
require_payload_item_model,
require_payload_model,
)
Expand All @@ -27,6 +28,7 @@
FetchChatsPayload,
FetchJoinRequests,
GetChatInfoPayload,
GetChatMembersPayload,
InviteUsersPayload,
JoinChatPayload,
JoinRequestActionPayload,
Expand Down Expand Up @@ -266,6 +268,22 @@ async def get_chats(self, chat_ids: list[int]) -> list[Chat]:

return [cached[chat_id] for chat_id in chat_ids if chat_id in cached]

async def get_chat_members(
self,
chat_id: int,
marker: int | None = None,
count: int = 50,
) -> tuple[list[Member], int]:
frame = GetChatMembersPayload(chat_id=chat_id, marker=marker or 0, count=count)
response = await self.app.invoke(Opcode.CHAT_MEMBERS, frame.to_payload())

members = bind_api_model(
self.app,
parse_payload_list(response, ChatPayloadKey.MEMBERS, Member),
)
next_marker = payload_item(response, ChatPayloadKey.MARKER, int) or 0
return members, next_marker

async def get_chat(self, chat_id: int) -> Chat:
chats = await self.get_chats([chat_id])
if not chats:
Expand Down
22 changes: 22 additions & 0 deletions src/pymax/infra/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ async def get_chat(self, chat_id: int) -> Chat:
"""
return await self._app.api.chats.get_chat(chat_id)

async def get_chat_members(
self,
chat_id: int,
marker: int | None = None,
count: int = 50,
) -> tuple[list[Member], int]:
"""Возвращает страницу участников чата по ID.

Args:
chat_id: ID чата.
marker: Маркер страницы. Если ``None``, запрашивается первая
страница.
count: Максимальное количество участников в ответе.

Returns:
``(members, next_marker)``. Если участников больше, чем
уместилось в ответ, ``next_marker`` ненулевой — передайте его в
следующий вызов, чтобы получить следующую страницу. ``0``
означает, что дальше страниц нет.
"""
return await self._app.api.chats.get_chat_members(chat_id, marker, count)

async def leave_group(self, chat_id: int) -> None:
"""Выходит из группы.

Expand Down