diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2024-01-05 17:29:13 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:54 +0100 |
commit | a8fa7a1bc08d326dea3fde8a3b6437c684b9fac5 (patch) | |
tree | bf24b1520554f1edfcb13232825d3e065d2db832 | |
parent | 51211931453f02a233ed38a35cde30981d6298ba (diff) | |
download | wee-slack-a8fa7a1bc08d326dea3fde8a3b6437c684b9fac5.tar.gz |
Fix URLs not being shown (only URL text was) in _unfurl_refs
-rw-r--r-- | slack/slack_message.py | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/slack/slack_message.py b/slack/slack_message.py index 1e52229..ddc91fc 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -75,6 +75,16 @@ def format_date(timestamp: int, token_string: str, link: Optional[str] = None) - return re.sub(r"{([^}]+)}", replace_token, token_string) + link_suffix +def format_url(url: str, text: Optional[str]) -> str: + if text is not None: + if url.endswith(text): + return text + else: + return f"{url} ({text})" + else: + return url + + def convert_int_to_letter(num: int) -> str: letter = "" while num > 0: @@ -732,7 +742,7 @@ class SlackMessage: def _resolve_ref( self, item_id: str, fallback_name: Optional[str] - ) -> Optional[Union[str, PendingMessageItem]]: + ) -> Union[str, PendingMessageItem]: if item_id.startswith("#"): return PendingMessageItem( self, @@ -762,6 +772,8 @@ class SlackMessage: timestamp = int(parts[1]) link = parts[3] if len(parts) > 3 else None return format_date(timestamp, parts[2], link) + else: + return format_url(item_id, fallback_name) def _unfurl_refs( self, message: str @@ -772,14 +784,7 @@ class SlackMessage: for match in re_ref.finditer(message): if i < match.start(0): yield message[i : match.start(0)] - fallback_name = match["fallback_name"] - item = self._resolve_ref(match["id"], fallback_name) - if item: - yield item - elif fallback_name is not None: - yield fallback_name - else: - yield match[0] + yield self._resolve_ref(match["id"], match["fallback_name"]) i = match.end(0) if i < len(message): @@ -1032,13 +1037,13 @@ class SlackMessage: if element["type"] == "text": return element["text"].replace("\n", "\n" + lines_prepend) elif element["type"] == "link": - if "text" in element: - if element.get("style", {}).get("code"): + if element.get("style", {}).get("code"): + if "text" in element: return element["text"] else: - return f"{element['url']} ({element['text']})" + return element["url"] else: - return element["url"] + return format_url(element["url"], element.get("text")) elif element["type"] == "emoji": return get_emoji(element["name"], element.get("skin_tone")) elif element["type"] == "color": |