From 39085c1a259ecde4924cb0b37d273684772f8308 Mon Sep 17 00:00:00 2001 From: PieterCK Date: Tue, 19 Nov 2024 14:16:08 +0700 Subject: [PATCH] slack_incoming: Use Slack text reformatters from data import. This commit updates `slack_incoming` to use the Slack text reformatting functions from `slack_message_conversion.py`, which were previously incompatible because of the extra formatting from `render_attachment` and `render_block`. --- zerver/webhooks/slack_incoming/view.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/zerver/webhooks/slack_incoming/view.py b/zerver/webhooks/slack_incoming/view.py index 252fa1e6e2..042f23cbec 100644 --- a/zerver/webhooks/slack_incoming/view.py +++ b/zerver/webhooks/slack_incoming/view.py @@ -7,7 +7,12 @@ from django.http import HttpRequest, HttpResponse, JsonResponse from django.utils.translation import gettext as _ from typing_extensions import ParamSpec -from zerver.data_import.slack_message_conversion import render_attachment, render_block +from zerver.data_import.slack_message_conversion import ( + convert_slack_formatting, + render_attachment, + render_block, + replace_links, +) from zerver.decorator import webhook_view from zerver.lib.exceptions import JsonableError from zerver.lib.request import RequestVariableMissingError @@ -90,19 +95,6 @@ def api_slack_incoming_webhook( body = body.strip() if body != "": - body = replace_formatting(replace_links(body).strip()) + body = convert_slack_formatting(replace_links(body).strip()) check_send_webhook_message(request, user_profile, user_specified_topic, body) return json_success(request, data={"ok": True}) - - -def replace_links(text: str) -> str: - return re.sub(r"<(\w+?:\/\/.*?)\|(.*?)>", r"[\2](\1)", text) - - -def replace_formatting(text: str) -> str: - # Slack uses *text* for bold, whereas Zulip interprets that as italics - text = re.sub(r"([^\w]|^)\*(?!\s+)([^\*\n]+)(?