diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-08-25 17:28:21 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | 74b6bf4a8117b446907e45a88fe2e7f06ece29db (patch) | |
tree | 79babb2fb678244883817153c7cc2e10467eb4a1 | |
parent | fe3288e1aa8e9a497004d354bb674a26923f0b61 (diff) | |
download | wee-slack-74b6bf4a8117b446907e45a88fe2e7f06ece29db.tar.gz |
Support rendering files
-rw-r--r-- | slack/commands.py | 4 | ||||
-rw-r--r-- | slack/error.py | 13 | ||||
-rw-r--r-- | slack/slack_message.py | 57 |
3 files changed, 64 insertions, 10 deletions
diff --git a/slack/commands.py b/slack/commands.py index b1ac545..69183a2 100644 --- a/slack/commands.py +++ b/slack/commands.py @@ -9,7 +9,7 @@ from typing import Callable, Dict, List, Optional, Tuple import weechat -from slack.error import SlackRtmError, UncaughtError +from slack.error import SlackError, SlackRtmError, UncaughtError from slack.log import print_error from slack.python_compatibility import format_exception, removeprefix, removesuffix from slack.shared import shared @@ -283,6 +283,8 @@ def print_uncaught_error( if data: if isinstance(error.exception, SlackRtmError): weechat.prnt("", f" data: {json.dumps(error.exception.message_json)}") + elif isinstance(error.exception, SlackError): + weechat.prnt("", f" data: {json.dumps(error.exception.data)}") else: print_error("This error does not have any data") diff --git a/slack/error.py b/slack/error.py index 1a13504..7a38f29 100644 --- a/slack/error.py +++ b/slack/error.py @@ -2,7 +2,7 @@ from __future__ import annotations from dataclasses import dataclass, field from datetime import datetime -from typing import TYPE_CHECKING, Dict, Mapping, Sequence, Union +from typing import TYPE_CHECKING, Dict, Mapping, Optional, Sequence, Union from uuid import uuid4 from slack.python_compatibility import format_exception_only @@ -70,12 +70,15 @@ class SlackRtmError(Exception): class SlackError(Exception): - def __init__(self, workspace: SlackWorkspace, error: str): + def __init__( + self, workspace: SlackWorkspace, error: str, data: Optional[object] = None + ): super().__init__( f"{self.__class__.__name__}: workspace={workspace}, error={error}" ) self.workspace = workspace self.error = error + self.data = data @dataclass @@ -92,9 +95,13 @@ def format_exception_only_str(exc: BaseException) -> str: return format_exception_only(exc)[-1].strip() +def store_uncaught_error(uncaught_error: UncaughtError) -> None: + shared.uncaught_errors.append(uncaught_error) + + def store_and_format_uncaught_error(uncaught_error: UncaughtError) -> str: + store_uncaught_error(uncaught_error) e = uncaught_error.exception - shared.uncaught_errors.append(uncaught_error) stack_msg_command = f"/slack debug error {uncaught_error.id}" stack_msg = f"run `{stack_msg_command}` for the stack trace" diff --git a/slack/slack_message.py b/slack/slack_message.py index 920d501..94f0501 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -6,7 +6,12 @@ from typing import TYPE_CHECKING, List, Match, Optional, Union import weechat -from slack.error import UncaughtError, store_and_format_uncaught_error +from slack.error import ( + SlackError, + UncaughtError, + store_and_format_uncaught_error, + store_uncaught_error, +) from slack.log import print_error, print_exception_once from slack.python_compatibility import removeprefix, removesuffix from slack.shared import shared @@ -282,14 +287,19 @@ class SlackMessage: else: if "blocks" in self._message_json: - text = await self._render_blocks(self._message_json["blocks"]) + texts = await self._render_blocks(self._message_json["blocks"]) else: text = await self._unfurl_refs(self._message_json["text"]) + texts = [text] if text else [] + + files_texts = self._render_files() + + full_text = "\n".join(texts + files_texts) if self._message_json.get("subtype") == "me_message": - return f"{await self._nick()} {text}" + return f"{await self._nick()} {full_text}" else: - return text + return full_text async def _render_message(self) -> str: if self._deleted: @@ -436,7 +446,7 @@ class SlackMessage: else: return "" - async def _render_blocks(self, blocks: List[SlackMessageBlock]) -> str: + async def _render_blocks(self, blocks: List[SlackMessageBlock]) -> List[str]: block_texts: List[str] = [] for block in blocks: try: @@ -538,7 +548,7 @@ class SlackMessage: with_color(shared.config.color.render_error.value, text) ) - return "\n".join(block_texts) + return block_texts async def _render_block_rich_text_section( self, section: SlackMessageBlockRichTextSection @@ -660,3 +670,38 @@ class SlackMessage: return "◦" else: return "▪︎" + + def _render_files(self) -> List[str]: + if "files" not in self._message_json: + return [] + + texts: List[str] = [] + for file in self._message_json.get("files", []): + if file.get("mode") == "tombstone": + text = with_color( + shared.config.color.deleted_message.value, "(This file was deleted)" + ) + elif file.get("mode") == "hidden_by_limit": + text = with_color( + shared.config.color.deleted_message.value, + "(This file is not available because the workspace has passed its storage limit)", + ) + elif file.get("mimetype") == "application/vnd.slack-docs": + url = f"{file['permalink']}?origin_team={self.workspace.id}&origin_channel={self.conversation.id}" + text = f"{url} ({file['title']})" + elif file.get("url_private"): + if file.get("title"): + text = f"{file['url_private']} ({file['title']})" + else: + text = file["url_private"] + else: + error = SlackError(self.workspace, "Unsupported file", file) + uncaught_error = UncaughtError(error) + store_uncaught_error(uncaught_error) + text = with_color( + shared.config.color.render_error.value, + f"<Unsupported file, error id: {uncaught_error.id}>", + ) + texts.append(text) + + return texts |