slack_import: Refactor a convert_slack_workspace_mentions.

This prep commit extracts the logic for reformatting Slack mentions into
Zulip mentions from `convert_to_zulip_markdown` into a new helper
function, `convert_slack_workspace_mentions`.

This is done to make the reformatting logic be reusable else where such
as in the Slack webhook or Slack incoming webhook.

(cherry picked from commit 4b472611d0)
This commit is contained in:
PieterCK
2024-11-21 15:15:33 +07:00
committed by Tim Abbott
parent 3e816ac12b
commit 2d9c167ca3

View File

@@ -151,6 +151,17 @@ def convert_markdown_syntax(text: str, regex: str, zulip_keyword: str) -> str:
return text return text
def convert_slack_workspace_mentions(text: str) -> str:
# Map Slack's '<!everyone>', '<!channel>' and '<!here>'
# mentions to Zulip's '@**all**' wildcard mention.
# No regex for these as they can be present anywhere
# in the sentence.
text = text.replace("<!everyone>", "@**all**")
text = text.replace("<!channel>", "@**all**")
text = text.replace("<!here>", "@**all**")
return text
# Markdown mapping # Markdown mapping
def convert_to_zulip_markdown( def convert_to_zulip_markdown(
text: str, text: str,
@@ -163,13 +174,7 @@ def convert_to_zulip_markdown(
text = convert_markdown_syntax(text, SLACK_STRIKETHROUGH_REGEX, "~~") text = convert_markdown_syntax(text, SLACK_STRIKETHROUGH_REGEX, "~~")
text = convert_markdown_syntax(text, SLACK_ITALIC_REGEX, "*") text = convert_markdown_syntax(text, SLACK_ITALIC_REGEX, "*")
# Map Slack's mention all: '<!everyone>' to '@**all** ' text = convert_slack_workspace_mentions(text)
# Map Slack's mention all: '<!channel>' to '@**all** '
# Map Slack's mention all: '<!here>' to '@**all** '
# No regex for this as it can be present anywhere in the sentence
text = text.replace("<!everyone>", "@**all**")
text = text.replace("<!channel>", "@**all**")
text = text.replace("<!here>", "@**all**")
# Map Slack channel mention: '<#C5Z73A7RA|general>' to '#**general**' # Map Slack channel mention: '<#C5Z73A7RA|general>' to '#**general**'
for cname, ids in added_channels.items(): for cname, ids in added_channels.items():