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`.
This commit is contained in:
PieterCK 2024-11-19 14:16:08 +07:00
parent 8764e15599
commit 39085c1a25
1 changed files with 7 additions and 15 deletions

View File

@ -7,7 +7,12 @@ from django.http import HttpRequest, HttpResponse, JsonResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from typing_extensions import ParamSpec 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.decorator import webhook_view
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.request import RequestVariableMissingError from zerver.lib.request import RequestVariableMissingError
@ -90,19 +95,6 @@ def api_slack_incoming_webhook(
body = body.strip() body = body.strip()
if body != "": 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) check_send_webhook_message(request, user_profile, user_specified_topic, body)
return json_success(request, data={"ok": True}) 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]+)(?<!\s)\*((?=[^\w])|$)", r"\1**\2**\3", text)
# Slack uses _text_ for emphasis, whereas Zulip interprets that as nothing
text = re.sub(r"([^\w]|^)[_](?!\s+)([^\_\n]+)(?<!\s)[_]((?=[^\w])|$)", r"\1*\2*\3", text)
return text