diff options
-rw-r--r-- | slack/api.py | 21 | ||||
-rw-r--r-- | typings/slack_api.pyi | 175 |
2 files changed, 144 insertions, 52 deletions
diff --git a/slack/api.py b/slack/api.py index 4fdef4d..6f55227 100644 --- a/slack/api.py +++ b/slack/api.py @@ -15,11 +15,7 @@ from slack.task import Future, create_task, gather from slack.util import get_callback_name if TYPE_CHECKING: - from slack_api import ( - SlackConversationIm, - SlackConversationInfo, - SlackConversationNotIm, - ) + from slack_api import SlackConversationInfoResponse def get_conversation_from_buffer_pointer( @@ -164,13 +160,24 @@ class SlackConversation: async def init(self): with self.loading(): info = await self.fetch_info() - self.name = info["channel"]["name"] + if info["ok"] != True: + # TODO: Handle error + return + + info_channel = info["channel"] + if info_channel["is_im"] == True: + self.name = "IM" # TODO + elif info_channel["is_mpim"] == True: + self.name = "MPIM" # TODO + else: + self.name = info_channel["name"] + self.buffer_pointer = weechat.buffer_new( self.name, get_callback_name(buffer_input_cb), "", "", "" ) weechat.buffer_set(self.buffer_pointer, "localvar_set_nick", "nick") - async def fetch_info(self): + async def fetch_info(self) -> SlackConversationInfoResponse: with self.loading(): info = await self.api.fetch("conversations.info", {"channel": self.id}) return info diff --git a/typings/slack_api.pyi b/typings/slack_api.pyi index 1f966e5..4842571 100644 --- a/typings/slack_api.pyi +++ b/typings/slack_api.pyi @@ -1,73 +1,158 @@ -from typing import TypedDict +from __future__ import annotations +from typing import List, Literal, TypedDict, final + +@final class SlackTopic(TypedDict): value: str creator: str last_set: int +@final class SlackPurpose(TypedDict): value: str creator: str last_set: int -class SlackConversationCommon(TypedDict): - id: str +@final +class SlackBlockElement(TypedDict): + type: str + text: str + +@final +class SlackBlockElementParent(TypedDict): + type: str + elements: List[SlackBlockElement] + +@final +class SlackBlock(TypedDict): + type: str + block_id: str + elements: List[SlackBlockElementParent] -class SlackConversationCommonNotIm(SlackConversationCommon): +@final +class SlackLatest(TypedDict): + client_msg_id: str + type: str + text: str + user: str + ts: str + blocks: List[SlackBlock] + team: str + +class SlackConversationInfoCommon(TypedDict): + id: str created: int - creator: str is_archived: bool + is_org_shared: bool + context_team_id: str + last_read: str + +class SlackConversationInfoCommonNotIm(SlackConversationInfoCommon): + name: str is_channel: bool - is_ext_shared: bool - is_general: bool is_group: bool - is_im: bool - is_member: bool - is_mpim: bool - is_org_shared: bool - is_pending_ext_shared: bool - is_private: bool - is_shared: bool + is_im: Literal[False] + is_general: bool + unlinked: int name_normalized: str - name: str - num_members: int + is_shared: bool + is_pending_ext_shared: bool + pending_shared: List # pyright: ignore [reportMissingTypeArgument] parent_conversation: None - pending_connected_team_ids: list - pending_shared: list - previous_names: list[str] - purpose: SlackPurpose - shared_team_ids: list[str] + creator: str + is_ext_shared: bool + shared_team_ids: List[str] + pending_connected_team_ids: List # pyright: ignore [reportMissingTypeArgument] + is_member: bool topic: SlackTopic - unlinked: int + purpose: SlackPurpose -class SlackConversationPublic(SlackConversationCommonNotIm): - num_members: int - previous_names: list[str] +@final +class SlackConversationInfoPublic(SlackConversationInfoCommonNotIm): + is_mpim: Literal[False] + is_private: Literal[False] + previous_names: List[str] # TODO: Check if private and mpim has this -class SlackConversationPrivate(SlackConversationCommonNotIm): - num_members: int +@final +class SlackConversationInfoPrivate(SlackConversationInfoCommonNotIm): + is_mpim: Literal[False] + is_private: Literal[True] + is_open: bool -class SlackConversationMpim(SlackConversationCommonNotIm): - num_members: int +@final +class SlackConversationInfoMpim(SlackConversationInfoCommonNotIm): + is_mpim: Literal[True] + is_private: Literal[True] + is_open: bool -class SlackConversationGroup(SlackConversationCommonNotIm): +@final +class SlackConversationInfoIm(SlackConversationInfoCommon): + is_im: Literal[True] + user: str + latest: SlackLatest + unread_count: int + unread_count_display: int is_open: bool - last_read: str priority: int -class SlackConversationIm(SlackConversationCommon): - created: int - is_archived: bool - is_im: bool - is_org_shared: bool - is_user_deleted: bool - priority: int - user: str +SlackConversationInfoNotIm = ( + SlackConversationInfoPublic + | SlackConversationInfoPrivate + | SlackConversationInfoMpim +) +SlackConversationInfo = SlackConversationInfoNotIm | SlackConversationInfoIm -SlackConversationNotIm = ( - SlackConversationPublic - | SlackConversationPrivate - | SlackConversationMpim - | SlackConversationGroup +@final +class SlackConversationInfoErrorResponse(TypedDict): + ok: Literal[False] + error: str + +@final +class SlackConversationInfoPublicSuccessResponse(TypedDict): + ok: Literal[True] + channel: SlackConversationInfoPublic + +@final +class SlackConversationInfoPrivateSuccessResponse(TypedDict): + ok: Literal[True] + channel: SlackConversationInfoPrivate + +@final +class SlackConversationInfoMpimSuccessResponse(TypedDict): + ok: Literal[True] + channel: SlackConversationInfoMpim + +@final +class SlackConversationInfoImSuccessResponse(TypedDict): + ok: Literal[True] + channel: SlackConversationInfoIm + +@final +class SlackConversationInfoNotImSuccessResponse(TypedDict): + ok: Literal[True] + channel: SlackConversationInfoNotIm + +@final +class SlackConversationInfoSuccessResponse(TypedDict): + ok: Literal[True] + channel: SlackConversationInfo + +SlackConversationInfoPublicResponse = ( + SlackConversationInfoPublicSuccessResponse | SlackConversationInfoErrorResponse +) +SlackConversationInfoPrivateResponse = ( + SlackConversationInfoPrivateSuccessResponse | SlackConversationInfoErrorResponse +) +SlackConversationInfoMpimResponse = ( + SlackConversationInfoMpimSuccessResponse | SlackConversationInfoErrorResponse +) +SlackConversationInfoImResponse = ( + SlackConversationInfoImSuccessResponse | SlackConversationInfoErrorResponse +) +SlackConversationInfoNotImResponse = ( + SlackConversationInfoNotImSuccessResponse | SlackConversationInfoErrorResponse +) +SlackConversationInfoResponse = ( + SlackConversationInfoSuccessResponse | SlackConversationInfoErrorResponse ) -SlackConversationInfo = SlackConversationNotIm | SlackConversationIm |