diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-10-18 20:21:56 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2023-10-18 21:18:37 +0200 |
commit | 2e5fdd068ad0ab0843be714bc7ac52316514103e (patch) | |
tree | 553838b612405261cea570bd5530ac82e620a75b | |
parent | 27c8262f2a60ef1276869a1d4d5d43a0339a785d (diff) | |
download | wee-slack-2e5fdd068ad0ab0843be714bc7ac52316514103e.tar.gz |
Properly fix rendering of < and >
I tried to do a fix for this in commit 6478c141, but it only fixed
normal text, not e.g. code blocks and attachments. With this I think it
should be correct everywhere.
Fixes #908
-rw-r--r-- | _pytest/test_render_message.py | 50 | ||||
-rw-r--r-- | _pytest/test_unwrap_attachments.py | 126 | ||||
-rw-r--r-- | wee_slack.py | 48 |
3 files changed, 174 insertions, 50 deletions
diff --git a/_pytest/test_render_message.py b/_pytest/test_render_message.py index c14fbd4..12b654d 100644 --- a/_pytest/test_render_message.py +++ b/_pytest/test_render_message.py @@ -89,6 +89,56 @@ import wee_slack ), "render_emoji_as_string": "both", }, + { + "input_message": { + "blocks": [ + { + "type": "rich_text", + "block_id": "ASgLI", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + {"type": "text", "text": "<text> & "}, + { + "type": "link", + "url": "https://text.link?x=<x>&z=z", + }, + {"type": "text", "text": " "}, + { + "type": "text", + "text": "<code> & ", + "style": {"code": True}, + }, + { + "type": "link", + "url": "https://code.link?x=<x>&z=z", + "style": {"code": True}, + }, + {"type": "text", "text": "\n"}, + ], + }, + { + "type": "rich_text_preformatted", + "elements": [ + {"type": "text", "text": "<code block> & "}, + { + "type": "link", + "url": "https://codeblock.link?x=<x>&z=z", + }, + ], + "border": 0, + }, + ], + } + ], + }, + "rendered": ( + "<text> & https://text.link?x=<x>&z=z `<code> & https://code.link?x=<x>&z=z`\n" + "```\n<code block> & https://codeblock.link?x=<x>&z=z\n```" + ), + "render_emoji_as_string": "both", + }, ], ) def test_render_message(case, channel_general): diff --git a/_pytest/test_unwrap_attachments.py b/_pytest/test_unwrap_attachments.py index 25ffd04..34d27de 100644 --- a/_pytest/test_unwrap_attachments.py +++ b/_pytest/test_unwrap_attachments.py @@ -77,28 +77,7 @@ import pytest } ] }, - "input_text_before": "http://link?a=1&b=2", - "output": "\n".join( - [ - "", - "| Title", - "| Attachment text", - ] - ), - }, - { - "input_message": { - "attachments": [ - { - "title": "Title", - "text": "Attachment text", - "title_link": "http://link?a=1&b=2", - "from_url": "http://link?a=1&b=2", - "image_url": "http://link?a=1&b=2", - } - ] - }, - "input_text_before": "http://link?a=1&b=2", + "input_text_before": "http://link?a=1&b=2", "output": "\n".join( [ "", @@ -460,6 +439,109 @@ import pytest ), "link_previews": False, }, + { + "input_message": { + "attachments": [ + { + "id": 1, + "ts": 1697480778, + "fallback": "title & <asd>", + "text": "text & <asd>", + "pretext": "pretext & <asd>", + "title": "title & <asd>", + "title_link": "https://title.link/?x=<x>&z=z", + "author_name": "author_name & <asd>", + "from_url": "https://from.url/?x=<x>&z=z", + "image_url": "https://image.url/?x=<x>&z=z", + "footer": "footer & <asd>", + "fields": [ + { + "value": "field value & <asd>", + "title": "field title & <asd>", + "short": False, + }, + { + "value": "field value mention <@U407ABLLW>", + "title": "field title mention <@U407ABLLW>", + "short": False, + }, + ], + }, + { + "id": 2, + "blocks": [ + { + "type": "rich_text", + "block_id": "IQm+Q", + "elements": [ + { + "type": "rich_text_preformatted", + "elements": [ + { + "type": "text", + "text": "block rich_text_preformatted & <asd>", + } + ], + } + ], + }, + { + "type": "rich_text", + "block_id": "a5bVo", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "block rich_text_section & <asd> ", + }, + { + "type": "link", + "url": "https://block.link?x=<x>&z=z", + "style": {"code": True}, + }, + ], + } + ], + }, + { + "type": "rich_text", + "block_id": "FeChA", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + {"type": "user", "user_id": "U407ABLLW"}, + {"type": "text", "text": ": <@ASD>"}, + ], + } + ], + }, + ], + "fallback": "[no preview available]", + }, + ] + }, + "input_text_before": "", + "output": "\n".join( + [ + "| pretext & <asd>", + "| author_name & <asd>: title & <asd> (https://title.link/?x=<x>&z=z)", + "| https://from.url/?x=<x>&z=z", + "| text & <asd>", + "| https://image.url/?x=<x>&z=z", + "| field title & <asd>: field value & <asd>", + "| field title mention <@U407ABLLW>: field value mention @alice", + "| footer & <asd> | Oct 16, 2023", + "| ```", + "| block rich_text_preformatted & <asd>", + "| ```", + "| block rich_text_section & <asd> `https://block.link?x=<x>&z=z`", + "| @alice: <@ASD>", + ] + ), + }, ), ) def test_unwrap_attachments(case, channel_general): diff --git a/wee_slack.py b/wee_slack.py index 3c35570..1709190 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -3402,7 +3402,7 @@ class SlackMessage(object): if blocks_rendered: text = blocks_rendered else: - text = self.message_json.get("text", "") + text = unhtmlescape(unfurl_refs(self.message_json.get("text", ""))) if self.message_json.get("mrkdwn", True): text = render_formatting(text) @@ -3412,9 +3412,7 @@ class SlackMessage(object): "group_join", ) and self.message_json.get("inviter"): inviter_id = self.message_json.get("inviter") - text += " by invitation from <@{}>".format(inviter_id) - - text = unfurl_refs(text) + text += unfurl_refs(" by invitation from <@{}>".format(inviter_id)) if self.subtype == "me_message" and not self.message_json["text"].startswith( self.sender @@ -3424,10 +3422,10 @@ class SlackMessage(object): if "edited" in self.message_json: text += " " + colorize_string(config.color_edited_suffix, "(edited)") - text += unfurl_refs(unwrap_attachments(self, text)) - text += unfurl_refs(unwrap_files(self, self.message_json, text)) - text += unfurl_refs(unwrap_huddle(self, self.message_json, text)) - text = unhtmlescape(text.lstrip().replace("\t", " ")) + text += unwrap_attachments(self, text) + text += unhtmlescape(unfurl_refs(unwrap_files(self, self.message_json, text))) + text += unwrap_huddle(self, self.message_json, text) + text = text.lstrip().replace("\t", " ") text += create_reactions_string( self.message_json.get("reactions", ""), self.team.myidentifier @@ -4773,7 +4771,7 @@ def unfurl_rich_text_section(block): def unfurl_block_rich_text_element(element): if element["type"] == "text": - return htmlescape(element["text"]) + return element["text"] elif element["type"] == "link": text = element.get("text") if text and text != element["url"]: @@ -4803,9 +4801,9 @@ def unfurl_block_rich_text_element(element): def unfurl_block_element(element): if element["type"] == "mrkdwn": - return render_formatting(element["text"]) + return render_formatting(unhtmlescape(unfurl_refs(element["text"]))) elif element["type"] == "plain_text": - return element["text"] + return unhtmlescape(unfurl_refs(element["text"])) elif element["type"] == "image": if element.get("alt_text"): return "{} ({})".format(element["image_url"], element["alt_text"]) @@ -4875,7 +4873,6 @@ def unhtmlescape(text): def unwrap_attachments(message, text_before): - text_before_unescaped = unhtmlescape(text_before) attachment_texts = [] a = message.message_json.get("attachments") if a: @@ -4901,9 +4898,7 @@ def unwrap_attachments(message, text_before): link_shown = False title = attachment.get("title") title_link = attachment.get("title_link", "") - if title_link and ( - title_link in text_before or title_link in text_before_unescaped - ): + if title_link and title_link in text_before: title_link = "" link_shown = True if title and title_link: @@ -4912,7 +4907,7 @@ def unwrap_attachments(message, text_before): % ( prepend_title_text, title, - title_link, + htmlescape(title_link), ) ) prepend_title_text = "" @@ -4926,12 +4921,8 @@ def unwrap_attachments(message, text_before): ) prepend_title_text = "" from_url = attachment.get("from_url", "") - if ( - from_url not in text_before - and from_url not in text_before_unescaped - and from_url != title_link - ): - t.append(from_url) + if from_url not in text_before and from_url != title_link: + t.append(htmlescape(from_url)) elif from_url: link_shown = True @@ -4941,17 +4932,13 @@ def unwrap_attachments(message, text_before): t.append(prepend_title_text + tx) prepend_title_text = "" - blocks = attachment.get("blocks", []) - t.extend(unfurl_blocks(blocks)) - image_url = attachment.get("image_url", "") if ( image_url not in text_before - and image_url not in text_before_unescaped and image_url != from_url and image_url != title_link ): - t.append(image_url) + t.append(htmlescape(image_url)) elif image_url: link_shown = True @@ -4965,6 +4952,11 @@ def unwrap_attachments(message, text_before): if files: t.append(files) + t = [unhtmlescape(unfurl_refs(x)) for x in t] + + blocks = attachment.get("blocks", []) + t.extend(unfurl_blocks(blocks)) + if attachment.get("is_msg_unfurl"): channel_name = resolve_ref("#{}".format(attachment["channel_id"])) if attachment.get("is_reply_unfurl"): @@ -4990,7 +4982,7 @@ def unwrap_attachments(message, text_before): "!date^{}^{{date_short_pretty}}{}".format(ts_int, time_string) ).capitalize() footer += " | {}".format(timestamp_formatted) - t.append(footer) + t.append(unhtmlescape(unfurl_refs(footer))) fallback = attachment.get("fallback") if t == [] and fallback and not link_shown: |