diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2022-11-20 19:35:28 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 7ad074083492e78c66e705b97699c92e5d38541a (patch) | |
tree | b455ddc72b6c7dbf69375a9063d611617da88d78 /slack/api.py | |
parent | 9934388d03ff6cee5302383cfd231c0e559f7ffa (diff) | |
download | wee-slack-7ad074083492e78c66e705b97699c92e5d38541a.tar.gz |
Create buffers on connect
Diffstat (limited to 'slack/api.py')
-rw-r--r-- | slack/api.py | 111 |
1 files changed, 95 insertions, 16 deletions
diff --git a/slack/api.py b/slack/api.py index 52479ae..3b7feb9 100644 --- a/slack/api.py +++ b/slack/api.py @@ -1,14 +1,21 @@ from __future__ import annotations import json -from typing import TYPE_CHECKING, Dict, Union +from typing import TYPE_CHECKING, 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, create_task if TYPE_CHECKING: - from slack_api import SlackConversation, SlackConversationIm, SlackConversationNotIm + from slack_api import ( + SlackConversationIm, + SlackConversationInfo, + SlackConversationNotIm, + ) class SlackApi: @@ -55,27 +62,99 @@ class SlackWorkspace: self.api = SlackApi(self) self.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, 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]) + + async def get_user(self, id: str) -> SlackUser: + if id in self.users: + return self.users[id] + user = SlackUser(self, id) + await user.init() + self.users[id] = user + return user + + def get_conversation_from_buffer_pointer( + self, buffer_pointer: str + ) -> Optional[SlackConversation]: + for conversation in self.conversations.values(): + if conversation.buffer_pointer == buffer_pointer: + return conversation + + +class SlackUser: + def __init__(self, workspace: SlackWorkspace, id: str): + self.workspace = workspace + self.api = workspace.api + self.id = id + self.name: str + async def init(self): + info = await self.api.fetch("users.info", {"user": self.id}) + self.name = info["user"]["name"] -class SlackChannelCommonNew: - def __init__(self, workspace: SlackWorkspace, slack_info: SlackConversation): + +class SlackConversation: + def __init__(self, workspace: SlackWorkspace, id: str): self.workspace = workspace self.api = workspace.api - self.id = slack_info["id"] - # self.fetch_info() + self.id = id + self.buffer_pointer: str + self.name: str + self.history_filled = False + self.history_pending = False + + async def init(self): + info = await self.fetch_info() + self.name = info["channel"]["name"] + self.buffer_pointer = weechat.buffer_new(self.name, "", "", "", "") async def fetch_info(self): - response = await self.api.fetch("conversations.info", {"channel": self.id}) - print(len(response)) + return await self.api.fetch("conversations.info", {"channel": self.id}) + async def fill_history(self): + if self.history_filled or self.history_pending: + return + self.history_pending = True -class SlackChannelNew(SlackChannelCommonNew): - def __init__(self, workspace: SlackWorkspace, slack_info: SlackConversationNotIm): - super().__init__(workspace, slack_info) - self.name = slack_info["name"] + 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] + ) + for message in reversed(messages): + if "user" in message: + user = await self.workspace.get_user(message["user"]) + username = user.name + else: + username = "bot" + weechat.prnt(self.buffer_pointer, f'{username}\t{message["text"]}') -class SlackIm(SlackChannelCommonNew): - def __init__(self, workspace: SlackWorkspace, slack_info: SlackConversationIm): - super().__init__(workspace, slack_info) - self.user = slack_info["user"] + self.history_filled = True + self.history_pending = False |