Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
mattermost_importer: Build group dm recipient as we convert huddle data.
PieterCK86a8ed679668
diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.pyindex 4dcbf983eeb..eba0b41d59f 100644--- a/zerver/data_import/import_util.py+++ b/zerver/data_import/import_util.py@@ -5,12 +5,12 @@ import secretsimport shutilimport subprocessfrom collections import defaultdict-from collections.abc import Callable, Iterable, Iterator, Mapping+from collections.abc import Callable, Hashable, Iterable, Iterator, Mappingfrom collections.abc import Set as AbstractSetfrom dataclasses import dataclassfrom email.errors import HeaderDefectfrom email.headerregistry import Address-from typing import Any, Protocol, TypeAlias, TypeVar+from typing import Any, Generic, Protocol, TypeAlias, TypeVarfrom urllib.parse import SplitResultimport orjson@@ -93,10 +93,14 @@ class UploadFileRequest:kwargs: dict[str, Any]-class SubscriberHandler:+GroupDMKey = TypeVar("GroupDMKey", bound=Hashable)+++class SubscriberHandler(Generic[GroupDMKey]):def __init__(self) -> None:self.stream_info: dict[int, set[int]] = {}self.direct_message_group_info: dict[int, set[int]] = {}+ self.group_dm_key_to_zulip_recipient_id: dict[GroupDMKey, int] = {}def set_info(self,@@ -111,6 +115,16 @@ class SubscriberHandler:else:raise AssertionError("stream_id or direct_message_group_id is required")+ def add_group_dm_key_to_zulip_recipient_id(+ self, key: GroupDMKey, group_recipient_id: int+ ) -> None:+ # TODO: Currently only Mattermost importer uses this. Maybe refactor this into+ # self.set_info() once other importers starts using this method too.+ self.group_dm_key_to_zulip_recipient_id[key] = group_recipient_id++ def get_zulip_recipient_id(self, key: GroupDMKey) -> int | None:+ return self.group_dm_key_to_zulip_recipient_id.get(key)+def get_users(self, stream_id: int | None = None, direct_message_group_id: int | None = None) -> set[int]:diff --git a/zerver/data_import/mattermost.py b/zerver/data_import/mattermost.pyindex 3eba02c59d9..a4dff930c06 100644--- a/zerver/data_import/mattermost.py+++ b/zerver/data_import/mattermost.py@@ -28,7 +28,6 @@ from zerver.data_import.import_util import (build_realm,build_realm_emoji,build_recipient,- build_recipients,build_stream,build_stream_subscriptions,build_user_profile,@@ -183,7 +182,7 @@ def convert_channel_data(realm: ZerverFieldsT,channel_data: list[ZerverFieldsT],user_data_map: dict[str, dict[str, Any]],- subscriber_handler: SubscriberHandler,+ subscriber_handler: SubscriberHandler[frozenset[str]],stream_id_mapper: IdMapper[str],user_id_mapper: IdMapper[str],realm_id: int,@@ -309,9 +308,10 @@ def convert_channel_data(def convert_direct_message_group_data(direct_message_group_data: list[ZerverFieldsT],user_data_map: dict[str, dict[str, Any]],- subscriber_handler: SubscriberHandler,+ subscriber_handler: SubscriberHandler[frozenset[str]],direct_message_group_id_mapper: IdMapper[frozenset[str]],user_id_mapper: IdMapper[str],+ realm: ZerverFieldsT,realm_id: int,team_name: str,) -> list[ZerverFieldsT]:@@ -342,6 +342,20 @@ def convert_direct_message_group_data(users=direct_message_group_user_ids,direct_message_group_id=direct_message_group_id,)++ recipient_id = NEXT_ID("recipient")+ recipient = Recipient(+ type_id=direct_message_group_id,+ id=recipient_id,+ type=Recipient.DIRECT_MESSAGE_GROUP,+ )+ recipient_dict = model_to_dict(recipient)+ realm["zerver_recipient"].append(recipient_dict)++ subscriber_handler.add_group_dm_key_to_zulip_recipient_id(+ key=direct_message_group_members,+ group_recipient_id=recipient_id,+ )zerver_direct_message_group.append(direct_message_group_dict)return zerver_direct_message_group@@ -972,7 +986,7 @@ def do_convert_data(mattermost_data_dir: str, output_dir: str, masking_content:team_name = team["name"]user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[frozenset[str]]()user_id_mapper = IdMapper[str]()stream_id_mapper = IdMapper[str]()direct_message_group_id_mapper = IdMapper[frozenset[str]]()@@ -1014,20 +1028,12 @@ def do_convert_data(mattermost_data_dir: str, output_dir: str, masking_content:subscriber_handler=subscriber_handler,direct_message_group_id_mapper=direct_message_group_id_mapper,user_id_mapper=user_id_mapper,+ realm=realm,realm_id=realm_id,team_name=team_name,)realm["zerver_huddle"] = zerver_direct_message_group- zerver_recipient = build_recipients(- # We build the users' recipient as we convert user data.- zerver_userprofile=[],- # We build the channels' recipient as we convert channel data.- zerver_stream=[],- zerver_direct_message_group=zerver_direct_message_group,- )- realm["zerver_recipient"] += zerver_recipient-stream_subscriptions = build_stream_subscriptions(get_users=subscriber_handler.get_users,zerver_recipient=realm["zerver_recipient"],diff --git a/zerver/data_import/rocketchat.py b/zerver/data_import/rocketchat.pyindex 2e86fc772b3..00c4c9b2374 100644--- a/zerver/data_import/rocketchat.py+++ b/zerver/data_import/rocketchat.py@@ -219,7 +219,7 @@ def convert_stream_subscription_data(zerver_stream: list[ZerverFieldsT],stream_id_mapper: IdMapper[str],user_id_mapper: IdMapper[str],- subscriber_handler: SubscriberHandler,+ subscriber_handler: SubscriberHandler[str],) -> None:stream_members_map: dict[int, set[int]] = {}@@ -249,7 +249,7 @@ def convert_direct_message_group_data(direct_message_group_id_to_direct_message_group_map: dict[str, dict[str, Any]],direct_message_group_id_mapper: IdMapper[str],user_id_mapper: IdMapper[str],- subscriber_handler: SubscriberHandler,+ subscriber_handler: SubscriberHandler[str],) -> list[ZerverFieldsT]:zerver_direct_message_group: list[ZerverFieldsT] = []@@ -1089,7 +1089,7 @@ def do_convert_data(rocketchat_data_dir: str, output_dir: str) -> None:username_to_user_id_map: dict[str, str] = map_username_to_user_id(user_id_to_user_map)user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[str]()user_id_mapper = IdMapper[str]()stream_id_mapper = IdMapper[str]()direct_message_group_id_mapper = IdMapper[str]()diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.pyindex 8a25af69d51..980e60b186a 100644--- a/zerver/tests/test_mattermost_importer.py+++ b/zerver/tests/test_mattermost_importer.py@@ -262,7 +262,7 @@ class MatterMostImporter(ZulipTestCase):reset_mirror_dummy_users(username_to_user)user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[frozenset[str]]()stream_id_mapper = IdMapper[str]()user_id_mapper = IdMapper[str]()team_name = "gryffindor"@@ -430,7 +430,7 @@ class MatterMostImporter(ZulipTestCase):reset_mirror_dummy_users(username_to_user)user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[frozenset[str]]()direct_message_group_id_mapper = IdMapper[frozenset[str]]()user_id_mapper = IdMapper[str]()team_name = "gryffindor"@@ -452,6 +452,7 @@ class MatterMostImporter(ZulipTestCase):subscriber_handler=subscriber_handler,direct_message_group_id_mapper=direct_message_group_id_mapper,user_id_mapper=user_id_mapper,+ realm=realm,realm_id=3,team_name=team_name,)@@ -483,7 +484,7 @@ class MatterMostImporter(ZulipTestCase):reset_mirror_dummy_users(username_to_user)user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[frozenset[str]]()direct_message_group_id_mapper = IdMapper[frozenset[str]]()user_id_mapper = IdMapper[str]()team_name = "gryffindor"@@ -505,6 +506,7 @@ class MatterMostImporter(ZulipTestCase):subscriber_handler=subscriber_handler,direct_message_group_id_mapper=direct_message_group_id_mapper,user_id_mapper=user_id_mapper,+ realm=realm,realm_id=3,team_name=team_name,)diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.pyindex 7f6c351fdd7..7be5b7774a4 100644--- a/zerver/tests/test_rocketchat_importer.py+++ b/zerver/tests/test_rocketchat_importer.py@@ -349,7 +349,7 @@ class RocketChatImporter(ZulipTestCase):domain_name = "zulip.com"user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[str]()user_id_mapper = IdMapper[str]()stream_id_mapper = IdMapper[str]()@@ -458,7 +458,7 @@ class RocketChatImporter(ZulipTestCase):domain_name = "zulip.com"user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[str]()user_id_mapper = IdMapper[str]()direct_message_group_id_mapper = IdMapper[str]()@@ -516,7 +516,7 @@ class RocketChatImporter(ZulipTestCase):domain_name = "zulip.com"user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[str]()user_id_mapper = IdMapper[str]()direct_message_group_id_mapper = IdMapper[str]()@@ -622,7 +622,7 @@ class RocketChatImporter(ZulipTestCase):domain_name = "zulip.com"user_handler = UserHandler()- subscriber_handler = SubscriberHandler()+ subscriber_handler = SubscriberHandler[str]()user_id_mapper = IdMapper[str]()stream_id_mapper = IdMapper[str]()direct_message_group_id_mapper = IdMapper[str]()