Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
mattermost_importer: Convert bot users and their messages.
PieterCK6d7d58b19cf1
diff --git a/zerver/data_import/mattermost.py b/zerver/data_import/mattermost.pyindex caf53ecc2a6..e5d36793f13 100644--- a/zerver/data_import/mattermost.py+++ b/zerver/data_import/mattermost.py@@ -13,9 +13,11 @@ from typing import Any, TypeAliasimport orjsonfrom django.conf import settingsfrom django.forms.models import model_to_dict+from django.utils.text import slugifyfrom django.utils.timezone import now as timezone_nowfrom zerver.data_import.import_util import (+ ImportedBotEmail,SubscriberHandler,UploadRecordData,ZerverFieldsT,@@ -34,6 +36,7 @@ from zerver.data_import.import_util import (convert_html_to_text,create_converted_data_files,get_attachment_path_and_content,+ get_domain_name_for_import,make_subscriber_map,make_user_messages,)@@ -97,7 +100,8 @@ def process_user(full_name = "{} {}".format(user_dict["first_name"], user_dict["last_name"])if full_name.strip():return full_name- return user_dict["username"]+ # Bots only have display_name.+ return user_dict.get("display_name") or user_dict["username"]avatar_source = UserProfile.DEFAULT_AVATAR_SOURCEfull_name = get_full_name(user_dict)@@ -115,6 +119,13 @@ def process_user(else:role = UserProfile.ROLE_MEMBER+ if user_dict.get("is_bot"):+ is_bot = True+ bot_type = UserProfile.DEFAULT_BOT+ else:+ is_bot = False+ bot_type = None+if user_dict["is_mirror_dummy"]:is_active = Falseis_mirror_dummy = True@@ -131,6 +142,8 @@ def process_user(id=id,is_active=is_active,role=role,+ is_bot=is_bot,+ bot_type=bot_type,is_mirror_dummy=is_mirror_dummy,realm_id=realm_id,short_name=short_name,@@ -730,8 +743,8 @@ def process_posts(# "channel_members".message_dict["pm_members"] = channel_memberselse: # nocoverage- # DM from unconverted users to themselves or 1-1 DM an unconverted user- # from a converted user.+ # DMs from unconverted users to themselves or 1-1 DM received by an+ # unconverted user.return Noneelse:raise AssertionError("Post without channel or channel_members key.")@@ -937,7 +950,7 @@ def check_user_in_team(user: dict[str, Any], team_name: str) -> bool:return any(team["name"] == team_name for team in user["teams"])-def label_mirror_dummy_users(+def backfill_user_data_from_posts(num_teams: int,team_name: str,mattermost_data: dict[str, Any],@@ -946,25 +959,57 @@ def label_mirror_dummy_users(# This function might looks like a great place to label admin users. But# that won't be fully correct since we are iterating only though posts and# it covers only users that have sent at least one message.++ def backfill_user_data(post: dict[str, Any]) -> None:+ user_data = username_to_user.get(post["user"])+ if user_data is None: # nocoverage+ # This is probably deleted user, we currently don't support converting+ # this type of user yet.+ return+ if user_data.get("is_bot"):+ # The export data doesn't state which teams or channels a bot user+ # is a member of. So, they'll only be assigned to channels and teams+ # where their messages are found.+ if "team" not in post:+ return++ if not (+ bot_team_data := next(+ (team for team in user_data["teams"] if team["name"] == post["team"]), None+ )+ ):+ user_data["teams"].append(+ {+ "name": post["team"],+ "roles": "team_user",+ "channels": [],+ }+ )+ bot_team_data = user_data["teams"][-1]++ if "channel" in post and all(+ channel["name"] != post["channel"] for channel in bot_team_data["channels"]+ ):+ bot_team_data["channels"].append(+ {+ "name": post["channel"],+ "roles": "channel_user",+ }+ )+ return++ if not check_user_in_team(user_data, team_name):+ user_data["is_mirror_dummy"] = True+for post in mattermost_data["post"]["channel_post"]:post_team = post["team"]if post_team == team_name:- user = username_to_user.get(post["user"])- if user is None:- # This is probably deleted user or bot user, we currently don't- # convert those yet.- continue- if not check_user_in_team(user, team_name):- user["is_mirror_dummy"] = True+ backfill_user_data(post)if num_teams == 1:for post in mattermost_data["post"]["direct_post"]:assert "team" not in post- user = username_to_user.get(post["user"])- if user is None:- continue- if not check_user_in_team(user, team_name):- user["is_mirror_dummy"] = True+ backfill_user_data(post)def reset_mirror_dummy_users(username_to_user: dict[str, dict[str, Any]]) -> None:@@ -982,7 +1027,6 @@ def mattermost_data_file_to_dict(mattermost_data_file: str) -> dict[str, Any]:mattermost_data["emoji"] = []mattermost_data["direct_channel"] = []mattermost_data["role"] = []- mattermost_data["bot"] = []with open(mattermost_data_file, "rb") as fp:for line in fp:@@ -992,6 +1036,25 @@ def mattermost_data_file_to_dict(mattermost_data_file: str) -> dict[str, Any]:mattermost_data["post"]["channel_post"].append(row["post"])elif data_type == "direct_post":mattermost_data["post"]["direct_post"].append(row["direct_post"])+ elif data_type == "bot":+ bot_data = row["bot"]+ bot_data["is_bot"] = True+ bot_data["first_name"] = ""+ bot_data["last_name"] = ""+ if "email" not in bot_data:+ bot_username = bot_data["username"]+ bot_data["email"] = ImportedBotEmail.get_email(+ bot_data,+ get_domain_name_for_import(),+ # Mattermost exports don't provide a nice ID, so we have to do+ # with the username field.+ bot_id=slugify(bot_username),+ bot_name_getter=lambda d: d["username"],+ )+ # Exported bot data doesn't include which teams or channels they are a+ # part of. This data will be backfilled later.+ bot_data["teams"] = []+ mattermost_data["user"].append(bot_data)else:mattermost_data[data_type].append(row[data_type])return mattermost_data@@ -1035,7 +1098,7 @@ def do_convert_data(mattermost_data_dir: str, output_dir: str, masking_content:realm_output_dir = os.path.join(output_dir, team_name)reset_mirror_dummy_users(username_to_user)- label_mirror_dummy_users(+ backfill_user_data_from_posts(len(mattermost_data["team"]), team_name, mattermost_data, username_to_user)diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.pyindex cef5d9d9a32..e11c7ccb1cb 100644--- a/zerver/tests/test_mattermost_importer.py+++ b/zerver/tests/test_mattermost_importer.py@@ -10,11 +10,13 @@ from typing import Anyfrom unittest.mock import patchimport orjson+from django.db.models import Qfrom django.test import override_settingsfrom django_stubs_ext import QuerySetAnyfrom zerver.data_import.import_util import SubscriberHandler, UploadRecordData, ZerverFieldsTfrom zerver.data_import.mattermost import (+ backfill_user_data_from_posts,build_reactions,check_user_in_team,convert_channel_data,@@ -23,7 +25,6 @@ from zerver.data_import.mattermost import (create_username_to_user_mapping,do_convert_data,get_mentioned_user_ids,- label_mirror_dummy_users,make_realm,mattermost_data_file_to_dict,process_message_attachments,@@ -39,16 +40,16 @@ from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.thumbnail import THUMBNAIL_ACCEPT_IMAGE_TYPESfrom zerver.models import Attachment, Message, Reaction, Recipient, UserProfilefrom zerver.models.presence import PresenceSequence-from zerver.models.realms import get_realm+from zerver.models.realms import Realm, get_realmfrom zerver.models.streams import Stream, get_streamfrom zerver.models.users import get_userfrom zerver.tests.test_import_export import make_export_output_dirfrom zerver.tests.test_microsoft_teams_importer import get_channel_subscriber_emails+from zproject.computed_settings import CROSS_REALM_BOT_EMAILS@dataclassclass MattermostUserMaps:- exported_bot_usernames: list[str]username_to_email_map: dict[str, str]exported_channel_subscriber_dict: dict[str, set[str]]@@ -112,20 +113,33 @@ class MattermostImportTestBase(ZulipTestCase):def build_user_maps(self,+ team_name: str,mattermost_data: dict[str, Any],) -> MattermostUserMaps:- exported_bot_usernames: list[str] = [user["username"] for user in mattermost_data["bot"]]-+ # Bot user's team and channel data is added here.+ backfill_user_data_from_posts(+ 1,+ team_name,+ mattermost_data,+ create_username_to_user_mapping(mattermost_data["user"]),+ )username_to_email_map: dict[str, str] = {}exported_channel_subscriber_dict: dict[str, set[str]] = defaultdict(set)for user in mattermost_data["user"]:- username_to_email_map[user["username"]] = user["email"]+ if user["teams"] == []:+ # Bots that don't send any messages won't be part of any team/channel+ # and won't get converted.+ continue+ if user.get("is_bot"):+ email = f"{user['username']}[email protected]"+ else:+ email = user["email"]+ username_to_email_map[user["username"]] = emailfor channel in user["teams"][0]["channels"]:- exported_channel_subscriber_dict[channel["name"]].add(user["email"])+ exported_channel_subscriber_dict[channel["name"]].add(email)return MattermostUserMaps(- exported_bot_usernames=exported_bot_usernames,username_to_email_map=username_to_email_map,exported_channel_subscriber_dict=exported_channel_subscriber_dict,)@@ -164,12 +178,16 @@ class MattermostImportTestBase(ZulipTestCase):imported_realm: Any,expected_owner_emails: set[str],expected_guest_emails: set[str],+ expected_bot_user_emails: set[str],+ expected_number_of_imported_users: int,) -> None:imported_user_profiles = UserProfile.objects.filter(realm=imported_realm)- self.assert_length(imported_user_profiles, len(mattermost_data["user"]))+ self.assert_length(imported_user_profiles, expected_number_of_imported_users)imported_users = imported_user_profiles.filter(is_bot=False, is_mirror_dummy=False)- self.assert_length(imported_users, len(mattermost_data["user"]))+ self.assert_length(+ imported_users, expected_number_of_imported_users - len(expected_bot_user_emails)+ )imported_realm_owners = imported_user_profiles.filter(is_bot=False, role=UserProfile.ROLE_REALM_OWNER, is_mirror_dummy=False@@ -187,10 +205,19 @@ class MattermostImportTestBase(ZulipTestCase):expected_guest_emails,)+ imported_bot_users = imported_user_profiles.filter(+ is_bot=True, bot_type=UserProfile.DEFAULT_BOT+ )++ self.assertSetEqual(+ {bot.email for bot in imported_bot_users},+ expected_bot_user_emails,+ )+def assert_channel_conversion(self,mattermost_data: dict[str, Any],- imported_realm: Any,+ imported_realm: Realm,exported_channel_subscriber_dict: dict[str, set[str]],) -> None:for channel_data in mattermost_data["channel"]:@@ -213,63 +240,74 @@ class MattermostImportTestBase(ZulipTestCase):mattermost_data: dict[str, Any],imported_realm: Any,mattermost_channel_id: str,- channel_display_name: str,- exported_bot_usernames: list[str],+ channel_name: str,username_to_email_map: dict[str, str],+ expected_number_of_bot_messages: int,) -> None:- imported_channel = get_stream(channel_display_name, imported_realm)- imported_channel_messages = Message.objects.filter(- recipient=imported_channel.recipient,- realm=imported_realm,- sender__is_bot=False,- ).order_by("id")+ imported_channel = get_stream(channel_name, imported_realm)+ imported_channel_messages = (+ Message.objects.filter(+ Q(sender__is_bot=False) | Q(sender__bot_type=UserProfile.DEFAULT_BOT),+ recipient=imported_channel.recipient,+ realm=imported_realm,+ )+ .exclude(sender__email__in=CROSS_REALM_BOT_EMAILS)+ .order_by("id")+ )importable_messages: list[dict[str, Any]] = []# Get all exported channel messages that in theory we should be able to# convert and import. We can't import bot users and their messages just yet.for m in mattermost_data["post"]["channel_post"]:- if m["channel"] != mattermost_channel_id or m["user"] in exported_bot_usernames:+ if m["channel"] != mattermost_channel_id:continueimportable_messages.append(m)if m["replies"] is not None:- importable_messages += [- r for r in m["replies"] if r["user"] not in exported_bot_usernames- ]+ importable_messages += m["replies"]self.assert_length(imported_channel_messages, len(importable_messages))self.assert_imported_messages_match_exported(importable_messages, imported_channel_messages, username_to_email_map)+ self.assert_length(+ imported_channel_messages.filter(sender__is_bot=True), expected_number_of_bot_messages+ )def assert_direct_messages(self,mattermost_data: dict[str, Any],imported_realm: Any,- excluded_usernames: list[str],username_to_email_map: dict[str, str],+ expected_number_of_bot_messages: int,) -> None:- imported_dms = Message.objects.filter(- recipient__type__in=[Recipient.PERSONAL, Recipient.DIRECT_MESSAGE_GROUP],- realm=imported_realm,- sender__is_bot=False,- ).order_by("id")+ imported_dms = (+ Message.objects.filter(+ Q(sender__is_bot=False) | Q(sender__bot_type=UserProfile.DEFAULT_BOT),+ realm=imported_realm,+ )+ .exclude(+ Q(recipient__type=Recipient.STREAM) | Q(sender__email__in=CROSS_REALM_BOT_EMAILS)+ )+ .order_by("id")+ )importable_dms: list[dict[str, Any]] = []for m in mattermost_data["post"]["direct_post"]:- if m["user"] in excluded_usernames:- continue- # We don't convert deleted or bot users messages yet. So 1-1 direct- # messages sent from importable users to them also won't be converted.- if len(set(m["channel_members"]) - set(excluded_usernames)) < 2:+ # We don't convert deleted users yet. So 1-1 direct messages sent from importable+ # users to them also won't be converted.+ if len(set(m["channel_members"])) < 2: # nocoveragecontinueimportable_dms.append(m)if m["replies"] is not None:- importable_dms += [r for r in m["replies"] if r["user"] not in excluded_usernames]+ importable_dms += m["replies"]self.assert_length(imported_dms, len(importable_dms))self.assert_imported_messages_match_exported(importable_dms, imported_dms, username_to_email_map)+ self.assert_length(+ imported_dms.filter(sender__is_bot=True), expected_number_of_bot_messages+ )def assert_attachments(self,@@ -298,7 +336,7 @@ class MatterMostImporter(MattermostImportTestBase):def test_mattermost_data_file_to_dict(self) -> None:fixture_file_name = self.fixture_file_name("export.json", "mattermost_fixtures")mattermost_data = mattermost_data_file_to_dict(fixture_file_name)- self.assert_length(mattermost_data, 9)+ self.assert_length(mattermost_data, 8)self.assertEqual(mattermost_data["version"], [1])@@ -461,7 +499,7 @@ class MatterMostImporter(MattermostImportTestBase):team_name = "gryffindor"# Snape is a mirror dummy user in Harry's team.- label_mirror_dummy_users(2, team_name, mattermost_data, username_to_user)+ backfill_user_data_from_posts(2, team_name, mattermost_data, username_to_user)user_handler = UserHandler()realm = make_realm(realm_id=0, team={"name": team_name})convert_user_data(@@ -988,13 +1026,13 @@ class MatterMostImporter(MattermostImportTestBase):snape.update(teams=None)self.assertFalse(check_user_in_team(snape, "slytherin"))- def test_label_mirror_dummy_users(self) -> None:+ def test_backfill_user_data_from_posts(self) -> None:fixture_file_name = self.fixture_file_name("export.json", "mattermost_fixtures")mattermost_data = mattermost_data_file_to_dict(fixture_file_name)username_to_user = create_username_to_user_mapping(mattermost_data["user"])reset_mirror_dummy_users(username_to_user)- label_mirror_dummy_users(+ backfill_user_data_from_posts(num_teams=2,team_name="gryffindor",mattermost_data=mattermost_data,@@ -1539,38 +1577,45 @@ class MattermostV1110ImportTest(MattermostImportTestBase):OWNER_EMAILS = {"[email protected]"}GUEST_EMAILS = {"[email protected]"}+ BOT_EMAILS = {"[email protected]"}def test_mattermost_data_file_to_dict(self) -> None:fixture_file = self.fixture_file_name("import.jsonl", self.FIXTURE_DIR)mattermost_data = mattermost_data_file_to_dict(fixture_file)- self.assert_length(mattermost_data, 9)+ self.assert_length(mattermost_data, 8)self.assert_length(mattermost_data["team"], 1)self.assertEqual(mattermost_data["team"][0]["name"], "ad-1")self.assert_length(mattermost_data["channel"], 4)- self.assert_length(mattermost_data["user"], 17)+ self.assert_length(mattermost_data["user"], 20)self.assert_length(mattermost_data["emoji"], 0)self.assert_length(mattermost_data["post"]["channel_post"], 50)self.assert_length(mattermost_data["post"]["direct_post"], 50)self.assert_length(mattermost_data["direct_channel"], 79)self.assert_length(mattermost_data["role"], 23)- self.assert_length(mattermost_data["bot"], 3)+ exported_bot_users = [user for user in mattermost_data["user"] if user.get("is_bot")]+ self.assert_length(exported_bot_users, 3)def test_e2e_export_data_v11_1_0(self) -> None:- mattermost_data, imported_realm = self.run_convert_and_import(- export_file_name="import.jsonl",- fixture_dir=self.FIXTURE_DIR,- team_name=self.TEAM,- subdomain="test-realm",- )- user_map_data = self.build_user_maps(mattermost_data)+ with self.settings(EXTERNAL_HOST="zulip.example.com"):+ mattermost_data, imported_realm = self.run_convert_and_import(+ export_file_name="import.jsonl",+ fixture_dir=self.FIXTURE_DIR,+ team_name=self.TEAM,+ subdomain="test-realm",+ )+ user_map_data = self.build_user_maps(self.TEAM, mattermost_data)with self.subTest("test user conversion"):self.assert_user_conversion(- mattermost_data,- imported_realm,- self.OWNER_EMAILS,- self.GUEST_EMAILS,+ mattermost_data=mattermost_data,+ imported_realm=imported_realm,+ expected_owner_emails=self.OWNER_EMAILS,+ expected_guest_emails=self.GUEST_EMAILS,+ expected_bot_user_emails=self.BOT_EMAILS,+ # Out of the three bots, two (Jira bot and system-bot) never participated in+ # any channel, so they don't get converted.+ expected_number_of_imported_users=len(mattermost_data["user"]) - 2,)with self.subTest("test channel conversion"):@@ -1586,17 +1631,17 @@ class MattermostV1110ImportTest(MattermostImportTestBase):imported_realm,# "sequi-7" is the Mattermost channel ID of "nesciunt"mattermost_channel_id="sequi-7",- channel_display_name="nesciunt",- exported_bot_usernames=user_map_data.exported_bot_usernames,+ channel_name="nesciunt",username_to_email_map=user_map_data.username_to_email_map,+ expected_number_of_bot_messages=2,)with self.subTest("test direct messages"):self.assert_direct_messages(- mattermost_data,- imported_realm,- user_map_data.exported_bot_usernames,- user_map_data.username_to_email_map,+ mattermost_data=mattermost_data,+ imported_realm=imported_realm,+ username_to_email_map=user_map_data.username_to_email_map,+ expected_number_of_bot_messages=2,)with self.subTest("test attachments"):