diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-09-17 11:58:26 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | 571952b5845910a0cac098bc83af632c7568e9c5 (patch) | |
tree | 0cf20ed2a901cf0e3006678c40442f8ad7f9237d | |
parent | c47aeb31b5f6f1843c0505e539fdff3352c76f80 (diff) | |
download | wee-slack-571952b5845910a0cac098bc83af632c7568e9c5.tar.gz |
Support opening a debug buffer with all ws messages
-rw-r--r-- | slack/commands.py | 6 | ||||
-rw-r--r-- | slack/slack_workspace.py | 46 |
2 files changed, 50 insertions, 2 deletions
diff --git a/slack/commands.py b/slack/commands.py index a08799a..7315359 100644 --- a/slack/commands.py +++ b/slack/commands.py @@ -297,7 +297,7 @@ def print_uncaught_error( print_error("This error does not have any data") -@weechat_command("tasks|buffer|errors|error", split_all_args=True) +@weechat_command("tasks|buffer|open_ws_buffer|errors|error", split_all_args=True) def command_slack_debug( buffer: str, args: List[str], options: Dict[str, Optional[str]] ): @@ -316,6 +316,10 @@ def command_slack_debug( "", f"Conversation id: {slack_buffer.parent.conversation.id}, Thread ts: {slack_buffer.parent.thread_ts}, Thread hash: {slack_buffer.parent.hash}", ) + elif args[0] == "open_ws_buffer": + slack_buffer = shared.buffers.get(buffer) + if slack_buffer: + slack_buffer.workspace.open_debug_ws_buffer() elif args[0] == "errors": num_arg = int(args[1]) if len(args) > 1 and args[1].isdecimal() else 5 num = min(num_arg, len(shared.uncaught_errors)) diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index 08895d1..63417e8 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -5,7 +5,17 @@ import socket import ssl import time from abc import ABC, abstractmethod -from typing import TYPE_CHECKING, Dict, Generic, Iterable, Optional, Type, TypeVar +from typing import ( + TYPE_CHECKING, + Dict, + Generic, + Iterable, + List, + Optional, + Tuple, + Type, + TypeVar, +) import weechat from websocket import ( @@ -174,6 +184,8 @@ class SlackWorkspace: self._connect_task: Optional[Task[None]] = None self._ws: Optional[WebSocket] = None self._hook_ws_fd: Optional[str] = None + self._ws_messages: List[Tuple[float, SlackRtmMessage]] = [] + self._debug_ws_buffer_pointer: Optional[str] = None self.conversations = SlackConversations(self) self.open_conversations: Dict[str, SlackConversation] = {} self.users = SlackUsers(self) @@ -283,6 +295,11 @@ class SlackWorkspace: run_async(self._ws_recv(json.loads(recv_data.decode()))) async def _ws_recv(self, data: SlackRtmMessage): + epoch = time.time() + # TODO: Remove old messages + self._ws_messages.append((epoch, data)) + self.print_debug_ws_buffer(epoch, data) + try: if data["type"] == "hello": return @@ -367,6 +384,33 @@ class SlackWorkspace: msg["thread_ts"] = buffer.parent.ts self._ws.send(json.dumps(msg)) + def _close_debug_ws_buffer_cb(self, data: str, buffer: str): + self._debug_ws_buffer_pointer = None + return weechat.WEECHAT_RC_OK + + def open_debug_ws_buffer(self): + if self._debug_ws_buffer_pointer: + weechat.buffer_set(self._debug_ws_buffer_pointer, "display", "1") + return + + name = f"{shared.SCRIPT_NAME}.debug.ws.{self.name}" + self._debug_ws_buffer_pointer = weechat.buffer_new_props( + name, + {"display": "1"}, + "", + "", + get_callback_name(self._close_debug_ws_buffer_cb), + "", + ) + for epoch, message in self._ws_messages: + self.print_debug_ws_buffer(epoch, message) + + def print_debug_ws_buffer(self, epoch: float, message: SlackRtmMessage): + if self._debug_ws_buffer_pointer: + weechat.prnt_date_tags( + self._debug_ws_buffer_pointer, int(epoch), "", json.dumps(message) + ) + async def reconnect(self): self.disconnect() await self.connect() |