diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2022-11-20 22:42:25 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | d1e2c0eb740032da0589deeef0a85f5b98ee6181 (patch) | |
tree | 16d10aebace04602b499f65a290112fa3115fe58 /slack/api.py | |
parent | cbf60cfc46446f55ccb16dea2a3194fcd1b754d5 (diff) | |
download | wee-slack-d1e2c0eb740032da0589deeef0a85f5b98ee6181.tar.gz |
Add SlackMessage class
Diffstat (limited to 'slack/api.py')
-rw-r--r-- | slack/api.py | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/slack/api.py b/slack/api.py index a8731e1..95061f2 100644 --- a/slack/api.py +++ b/slack/api.py @@ -1,14 +1,15 @@ from __future__ import annotations import json -from typing import TYPE_CHECKING, Dict, Optional, Union +import time +from typing import TYPE_CHECKING, Any, Dict, Optional, Union from urllib.parse import urlencode import weechat from slack.http import http_request from slack.shared import shared -from slack.task import await_all_concurrent_dict, create_task +from slack.task import create_task, gather if TYPE_CHECKING: from slack_api import ( @@ -108,10 +109,13 @@ class SlackWorkspace: class SlackUser: def __init__(self, workspace: SlackWorkspace, id: str): self.workspace = workspace - self.api = workspace.api self.id = id self.name: str + @property + def api(self) -> SlackApi: + return self.workspace.api + async def init(self): info = await self.api.fetch("users.info", {"user": self.id}) self.name = info["user"]["name"] @@ -120,13 +124,17 @@ class SlackUser: class SlackConversation: def __init__(self, workspace: SlackWorkspace, id: str): self.workspace = workspace - self.api = workspace.api self.id = id + # TODO: buffer_pointer may be accessed by buffer_switch before it's initialized self.buffer_pointer: str self.name: str self.history_filled = False self.history_pending = False + @property + def api(self) -> SlackApi: + return self.workspace.api + async def init(self): info = await self.fetch_info() self.name = info["channel"]["name"] @@ -141,20 +149,39 @@ class SlackConversation: self.history_pending = True history = await self.api.fetch("conversations.history", {"channel": self.id}) + start = time.time() - messages = history["messages"] - user_ids = {message["user"] for message in messages if "user" in message} - users = await await_all_concurrent_dict( - {user_id: self.workspace.get_user(user_id) for user_id in user_ids} + messages = [SlackMessage(self, message) for message in history["messages"]] + messages_rendered = await gather( + *(message.render_message() for message in messages) ) - for message in reversed(messages): - if "user" in message: - user = users[message["user"]] - username = user.name - else: - username = "bot" - weechat.prnt(self.buffer_pointer, f'{username}\t{message["text"]}') + for rendered in reversed(messages_rendered): + weechat.prnt(self.buffer_pointer, rendered) + print(f"history w/o fetch took: {time.time() - start}") self.history_filled = True self.history_pending = False + + +class SlackMessage: + def __init__(self, conversation: SlackConversation, message_json: Any): + self.conversation = conversation + self.ts = message_json["ts"] + self.message_json = message_json + + @property + def workspace(self) -> SlackWorkspace: + return self.conversation.workspace + + @property + def api(self) -> SlackApi: + return self.workspace.api + + async def render_message(self): + if "user" in self.message_json: + user = await self.workspace.get_user(self.message_json["user"]) + username = user.name + else: + username = "bot" + return f'{username}\t{self.message_json["text"]}' |