diff options
-rw-r--r-- | slack/api.py | 10 | ||||
-rw-r--r-- | slack/task.py | 9 |
2 files changed, 13 insertions, 6 deletions
diff --git a/slack/api.py b/slack/api.py index 3b7feb9..a8731e1 100644 --- a/slack/api.py +++ b/slack/api.py @@ -8,7 +8,7 @@ import weechat from slack.http import http_request from slack.shared import shared -from slack.task import await_all_concurrent, create_task +from slack.task import await_all_concurrent_dict, create_task if TYPE_CHECKING: from slack_api import ( @@ -143,14 +143,14 @@ class SlackConversation: history = await self.api.fetch("conversations.history", {"channel": self.id}) messages = history["messages"] - user_ids = [message["user"] for message in messages if "user" in message] - await await_all_concurrent( - [self.workspace.get_user(user_id) for user_id in user_ids] + 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} ) for message in reversed(messages): if "user" in message: - user = await self.workspace.get_user(message["user"]) + user = users[message["user"]] username = user.name else: username = "bot" diff --git a/slack/task.py b/slack/task.py index 27abd48..f166bfe 100644 --- a/slack/task.py +++ b/slack/task.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Awaitable, Coroutine, Generator, List, Tuple, TypeVar +from typing import Any, Awaitable, Coroutine, Dict, Generator, List, Tuple, TypeVar from uuid import uuid4 import weechat @@ -80,6 +80,13 @@ async def await_all_concurrent(requests: List[Coroutine[Any, Any, T]]) -> List[T 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( |