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.

data_import: Make slack bot emails unique.

Priyansh Garg4815f6e28b2e
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py
index b4fe406efc6..1dfa3e93f25 100755
--- a/zerver/data_import/slack.py
+++ b/zerver/data_import/slack.py
@@ -5,7 +5,7 @@ import secrets
import shutil
import subprocess
from collections import defaultdict
-from typing import Any, Dict, Iterator, List, Optional, Set, Tuple
+from typing import Any, Dict, Iterator, List, Optional, Set, Tuple, Type, TypeVar
import orjson
import requests
@@ -55,6 +55,41 @@ AddedChannelsT = Dict[str, Tuple[str, int]]
AddedMPIMsT = Dict[str, Tuple[str, int]]
DMMembersT = Dict[str, Tuple[str, str]]
SlackToZulipRecipientT = Dict[str, int]
+# Generic type for SlackBotEmail class
+SlackBotEmailT = TypeVar("SlackBotEmailT", bound="SlackBotEmail")
+
+
+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: Type[SlackBotEmailT], 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 = slack_bot_name.replace("Bot", "").replace(" ", "") + f"-bot@{domain_name}"
+
+ if email in cls.duplicate_email_count:
+ email_prefix, email_suffix = email.split("@")
+ email_prefix += cls.duplicate_email_count[email]
+ email = "@".join([email_prefix, email_suffix])
+ # Increment the duplicate email count
+ cls.duplicate_email_count[email] += 1
+ else:
+ cls.duplicate_email_count[email] = 1
+
+ cls.assigned_email[slack_bot_id] = email
+ return email
def rm_tree(path: str) -> None:
@@ -367,13 +402,7 @@ def get_user_email(user: ZerverFieldsT, domain_name: str) -> str:
if user["is_mirror_dummy"]:
return "{}@{}.slack.com".format(user["name"], user["team_domain"])
if "bot_id" in user["profile"]:
- 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.replace("Bot", "").replace(" ", "") + f"-bot@{domain_name}"
+ return SlackBotEmail.get_email(user["profile"], domain_name)
if get_user_full_name(user).lower() == "slackbot":
return f"imported-slackbot-bot@{domain_name}"
raise AssertionError(f"Could not find email address for Slack user {user}")