aboutsummaryrefslogtreecommitdiffstats
path: root/slack/slack_workspace.py
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2023-01-14 06:48:11 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:53 +0100
commit78e551804dfd1784f10c627dd4c8d019214b66a1 (patch)
treee41a030098c6a3d786c6a5160a94b9b2957769d7 /slack/slack_workspace.py
parenteed3b0a919e3790c4383548e3d30c83070465e7d (diff)
downloadwee-slack-78e551804dfd1784f10c627dd4c8d019214b66a1.tar.gz
Split slack classes into multiple files
Diffstat (limited to 'slack/slack_workspace.py')
-rw-r--r--slack/slack_workspace.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py
new file mode 100644
index 0000000..df99f94
--- /dev/null
+++ b/slack/slack_workspace.py
@@ -0,0 +1,98 @@
+from __future__ import annotations
+
+import json
+from typing import Dict, Union
+from urllib.parse import urlencode
+
+import weechat
+
+from slack.http import http_request
+from slack.shared import shared
+from slack.slack_conversation import SlackConversation
+from slack.slack_user import SlackUser
+from slack.task import Future, create_task
+
+
+class SlackApi:
+ def __init__(self, workspace: SlackWorkspace):
+ self.workspace = workspace
+
+ def get_request_options(self):
+ return {
+ "useragent": f"wee_slack {shared.SCRIPT_VERSION}",
+ "httpheader": f"Authorization: Bearer {self.workspace.config.api_token.value}",
+ "cookie": self.workspace.config.api_cookies.value,
+ }
+
+ async def fetch(self, method: str, params: Dict[str, Union[str, int]] = {}):
+ url = f"https://api.slack.com/api/{method}?{urlencode(params)}"
+ response = await http_request(
+ url,
+ self.get_request_options(),
+ self.workspace.config.slack_timeout.value * 1000,
+ )
+ return json.loads(response)
+
+ async def fetch_list(
+ self,
+ method: str,
+ list_key: str,
+ params: Dict[str, Union[str, int]] = {},
+ pages: int = 1, # negative or 0 means all pages
+ ):
+ response = await self.fetch(method, params)
+ next_cursor = response.get("response_metadata", {}).get("next_cursor")
+ if pages != 1 and next_cursor and response["ok"]:
+ params["cursor"] = next_cursor
+ next_pages = await self.fetch_list(method, list_key, params, pages - 1)
+ response[list_key].extend(next_pages[list_key])
+ return response
+ return response
+
+
+class SlackWorkspace:
+ def __init__(self, name: str):
+ self.name = name
+ self.config = shared.config.create_workspace_config(self.name)
+ self.api = SlackApi(self)
+ self.is_connected = False
+ self.nick = "TODO"
+ # Maybe make private, so you have to use get_user? Maybe make get_user a getter, though don't know if that's a problem since it's async
+ self.users: Dict[str, Future[SlackUser]] = {}
+ self.conversations: Dict[str, SlackConversation] = {}
+
+ async def connect(self):
+ # rtm_connect = await self.api.fetch("rtm.connect")
+ user_channels_response = await self.api.fetch_list(
+ "users.conversations",
+ "channels",
+ {
+ "exclude_archived": True,
+ # "types": "public_channel,private_channel,im",
+ "types": "public_channel",
+ "limit": 1000,
+ },
+ -1,
+ )
+ user_channels = user_channels_response["channels"]
+
+ for channel in user_channels:
+ conversation = SlackConversation(self, channel["id"])
+ self.conversations[channel["id"]] = conversation
+ create_task(conversation.init())
+
+ # print(rtm_connect)
+ # print([c["name"] for c in user_channels])
+ self.is_connected = True
+ weechat.bar_item_update("input_text")
+
+ async def create_user(self, id: str) -> SlackUser:
+ user = SlackUser(self, id)
+ await user.init()
+ return user
+
+ async def get_user(self, id: str) -> SlackUser:
+ if id in self.users:
+ return await self.users[id]
+ self.users[id] = create_task(self.create_user(id))
+ return await self.users[id]