aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2020-05-13 22:23:07 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2020-05-13 22:33:47 +0200
commite5c5d16cf86bc460fc3d3d47412230f9a612e67b (patch)
treebf1c62eb687b1ecf550759177928f1b9666e5c05 /wee_slack.py
parentc7ddfff317ac2a47c73ddeac62b91eaee1eff82c (diff)
downloadwee-slack-e5c5d16cf86bc460fc3d3d47412230f9a612e67b.tar.gz
If message has blocks, only render blocks unless rich_text is present
Slack doesn't render the `text` property at all when `blocks` is non-empty, so we shouldn't render both `text` and `blocks`. However, because we don't support the `rich_text` block type yet, we have to fall back to the `text` property when `rich_text` is present. Therefore, render both `text` and `blocks` if `rich_text` is present, else render only `blocks` if it is non-empty, and only `text` if `blocks` is empty.
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py23
1 files changed, 16 insertions, 7 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 4b0c335..575d108 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -2507,7 +2507,19 @@ class SlackMessage(object):
if not force and self.message_json.get("_rendered_text"):
return self.message_json["_rendered_text"]
- text = self.message_json.get("text", "")
+ blocks = self.message_json.get("blocks", [])
+ blocks_rendered = unfurl_blocks(blocks)
+ has_rich_text = any(block["type"] == "rich_text" for block in blocks)
+ if has_rich_text:
+ text = self.message_json.get("text", "")
+ if blocks_rendered:
+ if text:
+ text += "\n"
+ text += blocks_rendered
+ elif blocks_rendered:
+ text = blocks_rendered
+ else:
+ text = self.message_json.get("text", "")
if self.message_json.get('mrkdwn', True):
text = render_formatting(text)
@@ -2517,9 +2529,6 @@ class SlackMessage(object):
inviter_id = self.message_json.get('inviter')
text += " by invitation from <@{}>".format(inviter_id)
- if "blocks" in self.message_json:
- text += unfurl_blocks(self.message_json)
-
text = unfurl_refs(text)
if (self.message_json.get('subtype') == 'me_message' and
@@ -3375,9 +3384,9 @@ def linkify_text(message, team, only_users=False):
return re.sub(linkify_regex, linkify_word, message_escaped, flags=re.UNICODE)
-def unfurl_blocks(message_json):
- block_text = [""]
- for block in message_json["blocks"]:
+def unfurl_blocks(blocks):
+ block_text = []
+ for block in blocks:
try:
if block["type"] == "section":
fields = block.get("fields", [])