aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2022-10-29 18:08:53 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2022-10-30 12:21:30 +0100
commit51f124228b0d308719dd0d9b111b53235748bc67 (patch)
tree685e86f07ed994e4173062a5796fec2202247075
parent8a89b8038cc0d0e2c88d4ac77aafdd14470dca4a (diff)
downloadwee-slack-51f124228b0d308719dd0d9b111b53235748bc67.tar.gz
Show useful link for Slack posts
The url_private link for Slack posts just downloads a json of the post, which isn't very useful to click on. The permalink goes to the web view for a post, which is what we want. The web client adds origin_team and origin_channel, which makes the post page show info about where the post is shared, so add that here as well. We still want to use url_private for images etc., not permalink there as well, because that links to the Slack client with the image shown in the sidebar, which is much more inconvenient than just getting the image directly.
-rw-r--r--_pytest/test_unwrap_attachments.py8
-rw-r--r--wee_slack.py17
2 files changed, 15 insertions, 10 deletions
diff --git a/_pytest/test_unwrap_attachments.py b/_pytest/test_unwrap_attachments.py
index d4116eb..25ffd04 100644
--- a/_pytest/test_unwrap_attachments.py
+++ b/_pytest/test_unwrap_attachments.py
@@ -462,9 +462,9 @@ import pytest
},
),
)
-def test_unwrap_attachments(case):
+def test_unwrap_attachments(case, channel_general):
wee_slack.config.link_previews = case.get("link_previews")
- result = wee_slack.unwrap_attachments(
- case["input_message"], case["input_text_before"]
- )
+ message_json = {"ts": str(wee_slack.SlackTS()), **case["input_message"]}
+ message = wee_slack.SlackMessage("normal", message_json, channel_general)
+ result = wee_slack.unwrap_attachments(message, case["input_text_before"])
assert result == case["output"]
diff --git a/wee_slack.py b/wee_slack.py
index 006cfaf..d234113 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -3407,8 +3407,8 @@ class SlackMessage(object):
if "edited" in self.message_json:
text += " " + colorize_string(config.color_edited_suffix, "(edited)")
- text += unfurl_refs(unwrap_attachments(self.message_json, text))
- text += unfurl_refs(unwrap_files(self.message_json, text))
+ text += unfurl_refs(unwrap_attachments(self, text))
+ text += unfurl_refs(unwrap_files(self, self.message_json, text))
text = unhtmlescape(text.lstrip().replace("\t", " "))
text += create_reactions_string(
@@ -4651,10 +4651,10 @@ def unhtmlescape(text):
return text.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&")
-def unwrap_attachments(message_json, text_before):
+def unwrap_attachments(message, text_before):
text_before_unescaped = unhtmlescape(text_before)
attachment_texts = []
- a = message_json.get("attachments")
+ a = message.message_json.get("attachments")
if a:
if text_before:
attachment_texts.append("")
@@ -4738,7 +4738,7 @@ def unwrap_attachments(message_json, text_before):
else:
t.append(field["value"])
- files = unwrap_files(attachment, None)
+ files = unwrap_files(message, attachment, None)
if files:
t.append(files)
@@ -4781,7 +4781,7 @@ def unwrap_attachments(message_json, text_before):
return "\n".join(attachment_texts)
-def unwrap_files(message_json, text_before):
+def unwrap_files(message, message_json, text_before):
files_texts = []
for f in message_json.get("files", []):
if f.get("mode", "") == "tombstone":
@@ -4791,6 +4791,11 @@ def unwrap_files(message_json, text_before):
config.color_deleted,
"(This file is hidden because the workspace has passed its storage limit.)",
)
+ elif f.get("mimetype") == "application/vnd.slack-docs":
+ url = "{}?origin_team={}&origin_channel={}".format(
+ f["permalink"], message.team.identifier, message.channel.identifier
+ )
+ text = "{} ({})".format(url, f["title"])
elif (
f.get("url_private", None) is not None and f.get("title", None) is not None
):