Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

import_util: Refactor a `ImportedBotEmail`.

Mateusz Manderad7de16ab1830
diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.py
index 7ad8315f907..4dcbf983eeb 100644
--- a/zerver/data_import/import_util.py
+++ b/zerver/data_import/import_util.py
@@ -8,6 +8,8 @@ from collections import defaultdict
from collections.abc import Callable, Iterable, Iterator, Mapping
from collections.abc import Set as AbstractSet
from dataclasses import dataclass
+from email.errors import HeaderDefect
+from email.headerregistry import Address
from typing import Any, Protocol, TypeAlias, TypeVar
from urllib.parse import SplitResult
@@ -937,3 +939,50 @@ def get_domain_name_for_import() -> str:
hostname = SplitResult("", settings.EXTERNAL_HOST, "", "", "").hostname
assert hostname is not None
return hostname
+
+
+class ImportedBotEmail:
+ duplicate_email_count: dict[str, int] = {}
+ # Mapping of `bot_id` to final email assigned to the bot.
+ assigned_email: dict[str, str] = {}
+
+ @classmethod
+ def get_email(
+ cls,
+ user_profile: ZerverFieldsT,
+ domain_name: str,
+ bot_id: str,
+ bot_name_getter: Callable[[ZerverFieldsT], str],
+ ) -> str:
+ if bot_id in cls.assigned_email:
+ return cls.assigned_email[bot_id]
+
+ bot_name = bot_name_getter(user_profile)
+
+ email = Address(
+ username=bot_name.replace("Bot", "").replace(" ", "").lower() + "-bot",
+ domain=domain_name,
+ ).addr_spec
+ # The address formed above may not be a valid email format - e.g. containing
+ # non-ASCII characters in the local part, if the bot_name contains them.
+ # Only Address(addr_spec=...) triggers the necessary validation.
+ # Thus we call it here, and if issues are detected, we fall back to forming the
+ # email address in a safer way - using the bot id string.
+ try:
+ Address(addr_spec=email)
+ except HeaderDefect:
+ email = Address(
+ username=bot_id + "-bot",
+ domain=domain_name,
+ ).addr_spec
+
+ if email in cls.duplicate_email_count:
+ cls.duplicate_email_count[email] += 1
+ address = Address(addr_spec=email)
+ email_username = address.username + "-" + str(cls.duplicate_email_count[email])
+ email = Address(username=email_username, domain=address.domain).addr_spec
+ else:
+ cls.duplicate_email_count[email] = 1
+
+ cls.assigned_email[bot_id] = email
+ return email
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py
index 1469b029e77..a9e77bbd05e 100644
--- a/zerver/data_import/slack.py
+++ b/zerver/data_import/slack.py
@@ -10,7 +10,6 @@ from collections import defaultdict
from collections.abc import Callable, Iterator
from dataclasses import dataclass
from datetime import datetime, timezone
-from email.errors import HeaderDefect
from email.headerregistry import Address
from typing import Any, TypeAlias
from urllib.parse import urlsplit
@@ -22,6 +21,7 @@ from django.forms.models import model_to_dict
from django.utils.timezone import now as timezone_now
from zerver.data_import.import_util import (
+ ImportedBotEmail,
UploadFileRequest,
UploadRecordData,
ZerverFieldsT,
@@ -114,50 +114,20 @@ for emoji_dict in emoji_data:
class SlackBotEmail:
- duplicate_email_count: dict[str, int] = {}
- # Mapping of `bot_id` to final email assigned to the bot.
- assigned_email: dict[str, str] = {}
-
@classmethod
def get_email(cls, user_profile: ZerverFieldsT, domain_name: str) -> str:
slack_bot_id = user_profile["bot_id"]
- if slack_bot_id in cls.assigned_email:
- return cls.assigned_email[slack_bot_id]
- if "real_name_normalized" in user_profile:
- slack_bot_name = user_profile["real_name_normalized"]
- elif "first_name" in user_profile:
- slack_bot_name = user_profile["first_name"]
- else:
- raise AssertionError("Could not identify bot type")
-
- email = Address(
- username=slack_bot_name.replace("Bot", "").replace(" ", "").lower() + "-bot",
- domain=domain_name,
- ).addr_spec
- # The address formed above may not be a valid email format - e.g. containing
- # non-ASCII characters in the local part, if the slack_bot_name contains them.
- # Only Address(addr_spec=...) triggers the necessary validation.
- # Thus we call it here, and if issues are detected, we fall back to forming the
- # email address in a safer way - using the bot id string.
- try:
- Address(addr_spec=email)
- except HeaderDefect:
- email = Address(
- username=slack_bot_id + "-bot",
- domain=domain_name,
- ).addr_spec
-
- if email in cls.duplicate_email_count:
- cls.duplicate_email_count[email] += 1
- address = Address(addr_spec=email)
- email_username = address.username + "-" + str(cls.duplicate_email_count[email])
- email = Address(username=email_username, domain=address.domain).addr_spec
- else:
- cls.duplicate_email_count[email] = 1
+ def bot_name_getter(_user_profile: ZerverFieldsT) -> str:
+ if "real_name_normalized" in _user_profile:
+ slack_bot_name = _user_profile["real_name_normalized"]
+ elif "first_name" in _user_profile:
+ slack_bot_name = _user_profile["first_name"]
+ else:
+ raise AssertionError("Could not identify bot type")
+ return slack_bot_name
- cls.assigned_email[slack_bot_id] = email
- return email
+ return ImportedBotEmail.get_email(user_profile, domain_name, slack_bot_id, bot_name_getter)
def rm_tree(path: str) -> None: