aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-01-08 21:36:38 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:55 +0100
commit69bb654e85665b06818aa807fd51b151a487855b (patch)
tree3b1a7d34cc8e37839b42dab2da0a69032f2578c0
parentffe13bb8714dbabb983aea314eab753b676773ce (diff)
downloadwee-slack-69bb654e85665b06818aa807fd51b151a487855b.tar.gz
Use format_url everywhere a URL is printed
-rw-r--r--slack/slack_message.py35
-rw-r--r--tests/test_render_attachments.py25
2 files changed, 30 insertions, 30 deletions
diff --git a/slack/slack_message.py b/slack/slack_message.py
index b37cb41..f4f2f54 100644
--- a/slack/slack_message.py
+++ b/slack/slack_message.py
@@ -46,7 +46,6 @@ if TYPE_CHECKING:
def format_date(timestamp: int, token_string: str, link: Optional[str] = None) -> str:
ref_datetime = datetime.fromtimestamp(timestamp)
- link_suffix = f" ({link})" if link else ""
token_to_format = {
"date_num": "%Y-%m-%d",
"date": "%B %d, %Y",
@@ -72,10 +71,14 @@ def format_date(timestamp: int, token_string: str, link: Optional[str] = None) -
else:
return match.group(0)
- return re.sub(r"{([^}]+)}", replace_token, token_string) + link_suffix
+ formatted_date = re.sub(r"{([^}]+)}", replace_token, token_string)
+ if link is not None:
+ return format_url(link, formatted_date)
+ else:
+ return formatted_date
-def format_url(url: str, text: Optional[str]) -> str:
+def format_url(url: str, text: Optional[str] = None) -> str:
return weechat.string_eval_expression(
shared.config.look.render_url_as.value,
{},
@@ -890,7 +893,7 @@ class SlackMessage:
if element["type"] == "button":
items.extend(self._render_block_element(element["text"]))
if "url" in element:
- items.append(element["url"])
+ items.append(format_url(element["url"]))
else:
text = (
f'<Unsupported block action type "{element["type"]}">'
@@ -901,7 +904,7 @@ class SlackMessage:
block_lines.append(intersperse(items, " | "))
elif block["type"] == "call":
url = block["call"]["v1"]["join_url"]
- block_lines.append(["Join via " + url])
+ block_lines.append(["Join via " + format_url(url)])
elif block["type"] == "divider":
block_lines.append(["---"])
elif block["type"] == "context":
@@ -947,7 +950,9 @@ class SlackMessage:
block_lines.append(items)
elif element["type"] == "rich_text_preformatted":
texts: List[str] = [
- sub_element.get("text", sub_element.get("url", ""))
+ sub_element["text"]
+ if "text" in sub_element
+ else sub_element["url"]
for sub_element in element["elements"]
]
if texts:
@@ -1077,10 +1082,7 @@ class SlackMessage:
unhtmlescape(item) if isinstance(item, str) else item for item in items
]
elif element["type"] == "image":
- if element.get("alt_text"):
- return [f"{element['image_url']} ({element['alt_text']})"]
- else:
- return [element["image_url"]]
+ return [format_url(element["image_url"], element.get("alt_text"))]
else:
text = f'<Unsupported block element type "{element["type"]}">'
return [with_color(shared.config.color.render_error.value, text)]
@@ -1118,12 +1120,9 @@ class SlackMessage:
)
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']})"
+ text = format_url(url, file["title"])
elif file.get("url_private"):
- if file.get("title"):
- text = f"{file['url_private']} ({file['title']})"
- else:
- text = file["url_private"]
+ text = format_url(file["url_private"], file.get("title"))
else:
error = SlackError(self.workspace, "Unsupported file", file)
uncaught_error = UncaughtError(error)
@@ -1177,7 +1176,7 @@ class SlackMessage:
link_shown = True
if title and title_link:
lines.append(
- [f"{prepend_title_text}{title} ({htmlescape(title_link)})"]
+ [f"{prepend_title_text}{format_url(htmlescape(title_link), title)}"]
)
prepend_title_text = ""
elif title and not title_link:
@@ -1190,7 +1189,7 @@ class SlackMessage:
)
and from_url != title_link
):
- lines.append([htmlescape(from_url)])
+ lines.append([format_url(htmlescape(from_url))])
elif from_url:
link_shown = True
@@ -1208,7 +1207,7 @@ class SlackMessage:
and image_url != from_url
and image_url != title_link
):
- lines.append([htmlescape(image_url)])
+ lines.append([format_url(htmlescape(image_url))])
elif image_url:
link_shown = True
diff --git a/tests/test_render_attachments.py b/tests/test_render_attachments.py
index 4a23b5a..45b51c4 100644
--- a/tests/test_render_attachments.py
+++ b/tests/test_render_attachments.py
@@ -92,7 +92,7 @@ cases: List[Case] = [
"output": "\n".join(
[
"| Title (http://title.link)",
- "| http://from.url",
+ "| (http://from.url)",
"| Attachment text",
]
),
@@ -114,7 +114,7 @@ cases: List[Case] = [
[
"| Title (http://title.link)",
"| Attachment text",
- "| http://image.url",
+ "| (http://image.url)",
]
),
},
@@ -174,7 +174,7 @@ cases: List[Case] = [
"output": "\n".join(
[
"| Title",
- "| http://link",
+ "| (http://link)",
"| Attachment text",
]
),
@@ -215,7 +215,7 @@ cases: List[Case] = [
[
"| Pretext",
"| Author: Title (http://title.link)",
- "| http://from.url",
+ "| (http://from.url)",
"| Attachment text",
]
),
@@ -234,7 +234,7 @@ cases: List[Case] = [
"input_text_before": "",
"output": "\n".join(
[
- "| http://from.url",
+ "| (http://from.url)",
"| Author: Attachment text",
]
),
@@ -352,7 +352,7 @@ cases: List[Case] = [
"output": "\n".join(
[
"| Original message",
- "| http://link (File)",
+ "| File (http://link)",
]
),
},
@@ -404,10 +404,10 @@ cases: List[Case] = [
"output": "\n".join(
[
"| First attachment title (http://title.link.1)",
- "| http://from.url.1",
+ "| (http://from.url.1)",
"| First attachment text",
"| Second attachment title (http://title.link.2)",
- "| http://from.url.2",
+ "| (http://from.url.2)",
"| Second attachment text",
]
),
@@ -555,9 +555,9 @@ cases: List[Case] = [
[
"| pretext & <asd>",
"| author_name & <asd>: title & <asd> (https://title.link/?x=<x>&z=z)",
- "| https://from.url/?x=<x>&z=z",
+ "| (https://from.url/?x=<x>&z=z)",
"| text & <asd>",
- "| https://image.url/?x=<x>&z=z",
+ "| (https://image.url/?x=<x>&z=z)",
"| field title & <asd>: field value & <asd>",
f"| field title mention <@{user_test1_id}>: field value mention {color_user_mention}@Test_1{color_reset}",
"| footer & <asd> | Oct 16, 2023",
@@ -592,7 +592,7 @@ cases: List[Case] = [
"input_text_before": "",
"output": "\n".join(
[
- "| https://from.url",
+ "| (https://from.url)",
"| Author name: text",
"| Posted in <[color:chat_channel]>#channel1<[color:reset]> | Oct 15, 2023",
]
@@ -622,7 +622,7 @@ cases: List[Case] = [
"input_text_before": "",
"output": "\n".join(
[
- "| https://from.url",
+ "| (https://from.url)",
"| Author name: text",
"| From a thread in <[color:chat_channel]>#channel1<[color:reset]> | Oct 15, 2023",
]
@@ -633,6 +633,7 @@ cases: List[Case] = [
@pytest.mark.parametrize("case", cases)
def test_render_attachments(case: Case, message1_in_channel_public: SlackMessage):
+ shared.config.look.render_url_as.value = "${text} (${url})"
shared.config.look.display_link_previews.value = case.get("link_previews", True)
message1_in_channel_public.update_message_json(case["input_message"])
parsed = message1_in_channel_public._render_attachments( # pyright: ignore [reportPrivateUsage]