aboutsummaryrefslogtreecommitdiffstats
path: root/slack
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2022-11-20 22:42:25 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:53 +0100
commitd1e2c0eb740032da0589deeef0a85f5b98ee6181 (patch)
tree16d10aebace04602b499f65a290112fa3115fe58 /slack
parentcbf60cfc46446f55ccb16dea2a3194fcd1b754d5 (diff)
downloadwee-slack-d1e2c0eb740032da0589deeef0a85f5b98ee6181.tar.gz
Add SlackMessage class
Diffstat (limited to 'slack')
-rw-r--r--slack/api.py57
-rw-r--r--slack/task.py11
2 files changed, 44 insertions, 24 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"]}'
diff --git a/slack/task.py b/slack/task.py
index f166bfe..79f5712 100644
--- a/slack/task.py
+++ b/slack/task.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Any, Awaitable, Coroutine, Dict, Generator, List, Tuple, TypeVar
+from typing import Any, Awaitable, Coroutine, Generator, List, Tuple, TypeVar
from uuid import uuid4
import weechat
@@ -75,18 +75,11 @@ def create_task(
return task
-async def await_all_concurrent(requests: List[Coroutine[Any, Any, T]]) -> List[T]:
+async def gather(*requests: Coroutine[Any, Any, T]) -> List[T]:
tasks = [create_task(request) for request in requests]
return [await task for task in tasks]
-async def await_all_concurrent_dict(
- requests: Dict[str, Coroutine[Any, Any, T]]
-) -> Dict[str, T]:
- tasks = {key: create_task(request) for key, request in requests.items()}
- return {key: await task for key, task in tasks.items()}
-
-
async def sleep(milliseconds: int):
future = FutureTimer()
weechat.hook_timer(