slack_import: Rearrange get_user_mentions function.

This prep commit moves the helper function get_user_mentions to appear
before its primary usage in `convert_to_zulip_markdown`. This adjustment
aligns with code style conventions, improving readability.

The change also prepares the codebase for reusing Slack text
reformatting logic in the Slack incoming webhook.
This commit is contained in:
PieterCK 2024-11-21 15:29:35 +07:00
parent 65f05794ee
commit 8fe0fbc4a0
1 changed files with 19 additions and 19 deletions

View File

@ -79,6 +79,25 @@ def get_user_full_name(user: ZerverFieldsT) -> str:
return user["name"]
def get_user_mentions(
token: str, users: list[ZerverFieldsT], slack_user_id_to_zulip_user_id: SlackToZulipUserIDT
) -> tuple[str, int | None]:
slack_usermention_match = re.search(SLACK_USERMENTION_REGEX, token, re.VERBOSE)
assert slack_usermention_match is not None
short_name = slack_usermention_match.group(4)
slack_id = slack_usermention_match.group(2)
for user in users:
if (user["id"] == slack_id and user["name"] == short_name and short_name) or (
user["id"] == slack_id and short_name is None
):
full_name = get_user_full_name(user)
user_id = slack_user_id_to_zulip_user_id[slack_id]
mention = "@**" + full_name + "**"
token = re.sub(SLACK_USERMENTION_REGEX, mention, token, flags=re.VERBOSE)
return token, user_id
return token, None
# Markdown mapping
def convert_to_zulip_markdown(
text: str,
@ -127,25 +146,6 @@ def convert_to_zulip_markdown(
return text, mentioned_users_id, message_has_link
def get_user_mentions(
token: str, users: list[ZerverFieldsT], slack_user_id_to_zulip_user_id: SlackToZulipUserIDT
) -> tuple[str, int | None]:
slack_usermention_match = re.search(SLACK_USERMENTION_REGEX, token, re.VERBOSE)
assert slack_usermention_match is not None
short_name = slack_usermention_match.group(4)
slack_id = slack_usermention_match.group(2)
for user in users:
if (user["id"] == slack_id and user["name"] == short_name and short_name) or (
user["id"] == slack_id and short_name is None
):
full_name = get_user_full_name(user)
user_id = slack_user_id_to_zulip_user_id[slack_id]
mention = "@**" + full_name + "**"
token = re.sub(SLACK_USERMENTION_REGEX, mention, token, flags=re.VERBOSE)
return token, user_id
return token, None
# Map italic, bold and strikethrough Markdown
def convert_markdown_syntax(text: str, regex: str, zulip_keyword: str) -> str:
"""