diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-01-20 18:11:39 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 3ae792a3cd858261c3b8fc7a0e1cdd9fa570096e (patch) | |
tree | 4a72b4fff96cb44afef6df15f62c259cad3191de /slack | |
parent | c074506d5204f25abac7a8f3e03b37a4c072ad6e (diff) | |
download | wee-slack-3ae792a3cd858261c3b8fc7a0e1cdd9fa570096e.tar.gz |
Simplify SlackUser/Bot by using classmethod to create
Diffstat (limited to 'slack')
-rw-r--r-- | slack/slack_user.py | 55 | ||||
-rw-r--r-- | slack/slack_workspace.py | 14 |
2 files changed, 19 insertions, 50 deletions
diff --git a/slack/slack_user.py b/slack/slack_user.py index 439bbdd..871c1ef 100644 --- a/slack/slack_user.py +++ b/slack/slack_user.py @@ -1,11 +1,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING import weechat from slack.shared import shared -from slack.task import create_task from slack.util import with_color if TYPE_CHECKING: @@ -44,32 +43,20 @@ def format_bot_nick(nick: str, colorize: bool = False) -> str: class SlackUser: - def __init__( - self, - workspace: SlackWorkspace, - id: str, - info: Optional[SlackUserInfo] = None, - ): + def __init__(self, workspace: SlackWorkspace, id: str, info: SlackUserInfo): self.workspace = workspace self.id = id - if info: - self._info = info - self._set_info_task = None - else: - self._set_info_task = create_task(self._set_info()) + self._info = info + + @classmethod + async def create(cls, workspace: SlackWorkspace, id: str): + info_response = await workspace.api.fetch_user_info(id) + return cls(workspace, id, info_response["user"]) @property def _api(self) -> SlackApi: return self.workspace.api - async def _set_info(self): - info_response = await self._api.fetch_user_info(self.id) - self._info = info_response["user"] - - async def ensure_initialized(self): - if self._set_info_task: - await self._set_info_task - def nick(self, colorize: bool = False) -> str: nick = self._name_without_spaces() @@ -94,31 +81,19 @@ class SlackUser: class SlackBot: - def __init__( - self, - workspace: SlackWorkspace, - id: str, - info: Optional[SlackBotInfo] = None, - ): + def __init__(self, workspace: SlackWorkspace, id: str, info: SlackBotInfo): self.workspace = workspace self.id = id - if info: - self._info = info - self._set_info_task = None - else: - self._set_info_task = create_task(self._set_info()) + self._info = info + + @classmethod + async def create(cls, workspace: SlackWorkspace, id: str): + info_response = await workspace.api.fetch_bot_info(id) + return cls(workspace, id, info_response["bot"]) @property def _api(self) -> SlackApi: return self.workspace.api - async def _set_info(self): - info_response = await self._api.fetch_bot_info(self.id) - self._info = info_response["bot"] - - async def ensure_initialized(self): - if self._set_info_task: - await self._set_info_task - def nick(self, colorize: bool = False) -> str: return format_bot_nick(self._info["name"], colorize) diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index e900101..20d835e 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -45,12 +45,9 @@ class SlackUsers(Dict[str, Future[SlackUser]]): ) -> SlackUser: if items_info_task: items_info = await items_info_task - item = SlackUser(self.workspace, item_id, items_info[item_id]) + return SlackUser(self.workspace, item_id, items_info[item_id]) else: - item = SlackUser(self.workspace, item_id) - - await item.ensure_initialized() - return item + return await SlackUser.create(self.workspace, item_id) async def _fetch_items_info(self, item_ids: Iterable[str]): response = await self.workspace.api.fetch_users_info(item_ids) @@ -80,12 +77,9 @@ class SlackBots(Dict[str, Future[SlackBot]]): ) -> SlackBot: if items_info_task: items_info = await items_info_task - item = SlackBot(self.workspace, item_id, items_info[item_id]) + return SlackBot(self.workspace, item_id, items_info[item_id]) else: - item = SlackBot(self.workspace, item_id) - - await item.ensure_initialized() - return item + return await SlackBot.create(self.workspace, item_id) async def _fetch_items_info(self, item_ids: Iterable[str]): response = await self.workspace.api.fetch_bots_info(item_ids) |