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.

ms_teams_importer: Convert deleted users messages.

PieterCK9a000277271b
diff --git a/starlight_help/src/content/docs/import-from-microsoft-teams.mdx b/starlight_help/src/content/docs/import-from-microsoft-teams.mdx
index ecf0c5750fe..c169e358e06 100644
--- a/starlight_help/src/content/docs/import-from-microsoft-teams.mdx
+++ b/starlight_help/src/content/docs/import-from-microsoft-teams.mdx
@@ -215,7 +215,6 @@ in mind about the import process:
are not imported.
* Message reactions are not imported.
* Message thread replies will be combined to the main topic.
-* Messages from deleted users are not imported.
* Messages in private channels are not imported.
* Microsoft tenant's user roles are mapped to Zulip's [user
roles](/help/user-roles) in the following way:
diff --git a/zerver/data_import/microsoft_teams.py b/zerver/data_import/microsoft_teams.py
index 98196e2770a..ebac8cd0dee 100644
--- a/zerver/data_import/microsoft_teams.py
+++ b/zerver/data_import/microsoft_teams.py
@@ -4,6 +4,7 @@ from collections import defaultdict
from collections.abc import Iterable
from dataclasses import dataclass
from datetime import datetime
+from email.headerregistry import Address
from typing import Any, Literal, TypeAlias
from urllib.parse import SplitResult
@@ -284,6 +285,41 @@ def get_user_email(user: MicrosoftTeamsFieldsT) -> str:
raise AssertionError(f"Could not find email address for Microsoft Teams user {user}")
+def create_is_mirror_dummy_user(
+ microsoft_team_user_id: str,
+ microsoft_teams_user_id_to_zulip_user_id: MicrosoftTeamsUserIdToZulipUserIdT,
+ realm: dict[str, Any],
+ realm_id: int,
+ domain_name: str,
+) -> None:
+ zulip_user_id = NEXT_ID("user")
+ user_full_name = f"Deleted Teams user {microsoft_team_user_id}"
+ email = Address(username=microsoft_team_user_id, domain=domain_name).addr_spec
+ user_profile_dict = build_user_profile(
+ avatar_source=UserProfile.AVATAR_FROM_GRAVATAR,
+ date_joined=int(timezone_now().timestamp()),
+ delivery_email=email,
+ email=email,
+ full_name=user_full_name,
+ id=zulip_user_id,
+ is_active=False,
+ role=UserProfile.ROLE_MEMBER,
+ is_mirror_dummy=True,
+ realm_id=realm_id,
+ short_name=user_full_name,
+ timezone="UTC",
+ )
+ realm["zerver_userprofile"].append(user_profile_dict)
+ microsoft_teams_user_id_to_zulip_user_id[microsoft_team_user_id] = zulip_user_id
+
+ recipient_id = NEXT_ID("recipient")
+ subscription_id = NEXT_ID("subscription")
+ recipient = build_recipient(zulip_user_id, recipient_id, Recipient.PERSONAL)
+ sub = build_subscription(recipient_id, zulip_user_id, subscription_id)
+ realm["zerver_recipient"].append(recipient)
+ realm["zerver_subscription"].append(sub)
+
+
def convert_users(
microsoft_teams_user_role_data: MicrosoftTeamsUserRoleData,
realm: dict[str, Any],
@@ -374,18 +410,17 @@ def is_microsoft_teams_event_message(message: MicrosoftTeamsFieldsT) -> bool:
def process_messages(
added_teams: dict[str, TeamMetadata],
+ domain_name: str,
channel_metadata: None | dict[str, ChannelMetadata],
is_private: bool,
messages: list[MicrosoftTeamsFieldsT],
microsoft_teams_user_id_to_zulip_user_id: MicrosoftTeamsUserIdToZulipUserIdT,
+ realm: dict[str, Any],
realm_id: int,
subscriber_map: dict[int, set[int]],
) -> tuple[list[ZerverFieldsT], list[ZerverFieldsT]]:
zerver_usermessage: list[ZerverFieldsT] = []
zerver_messages: list[ZerverFieldsT] = []
- total_user_messages = 0
- total_skipped_user_messages = 0
- skipped_deleted_users_message = 0
for message in messages:
if is_microsoft_teams_event_message(message):
@@ -427,10 +462,13 @@ def process_messages(
microsoft_teams_sender_id: str = get_microsoft_teams_sender_id_from_message(message)
if microsoft_teams_sender_id not in microsoft_teams_user_id_to_zulip_user_id:
- # TODO: Deleted users are not included in the exported user list but their
- # messages are still exported and point to the deactivated user as the sender.
- skipped_deleted_users_message += 1
- continue
+ create_is_mirror_dummy_user(
+ microsoft_teams_sender_id,
+ microsoft_teams_user_id_to_zulip_user_id,
+ realm,
+ realm_id,
+ domain_name,
+ )
message_id = NEXT_ID("message")
zulip_message = build_message(
@@ -460,20 +498,12 @@ def process_messages(
is_private=is_direct_message_type,
)
- total_user_messages += num_created
- total_skipped_user_messages += num_skipped
-
logging.debug(
"Created %s UserMessages; deferred %s due to long-term idle",
- total_user_messages,
- total_skipped_user_messages,
+ num_created,
+ num_skipped,
)
- logging.info(
- "Unable to convert %s message(s) from deleted users.",
- skipped_deleted_users_message,
- )
-
return (
zerver_messages,
zerver_usermessage,
@@ -501,6 +531,7 @@ def get_batched_export_message_data(
def convert_messages(
added_teams: dict[str, TeamMetadata],
+ domain_name: str,
microsoft_teams_user_id_to_zulip_user_id: MicrosoftTeamsUserIdToZulipUserIdT,
output_dir: str,
realm_id: int,
@@ -542,10 +573,12 @@ def convert_messages(
(zerver_messages, zerver_usermessage) = process_messages(
added_teams=added_teams,
channel_metadata=microsoft_teams_channel_metadata,
+ domain_name=domain_name,
is_private=False,
messages=message_chunk,
microsoft_teams_user_id_to_zulip_user_id=microsoft_teams_user_id_to_zulip_user_id,
subscriber_map=subscriber_map,
+ realm=realm,
realm_id=realm_id,
)
@@ -601,6 +634,7 @@ def do_convert_directory(
convert_messages(
added_teams=added_teams,
+ domain_name=domain_name,
microsoft_teams_user_id_to_zulip_user_id=microsoft_teams_user_id_to_zulip_user_id,
output_dir=output_dir,
realm_id=realm_id,
diff --git a/zerver/tests/test_microsoft_teams_importer.py b/zerver/tests/test_microsoft_teams_importer.py
index f10dedfde19..1ffba6f4ea2 100644
--- a/zerver/tests/test_microsoft_teams_importer.py
+++ b/zerver/tests/test_microsoft_teams_importer.py
@@ -69,6 +69,7 @@ MICROSOFT_TEAMS_EXPORT_USER_ROLE_DATA = MicrosoftTeamsUserRoleData(
guest_user_ids={"16741626-4cd8-46cc-bf36-42ecc2b5fdce"},
)
+DELETED_MICROSOFT_TEAMS_USERS = ["9bd4aca7-99cf-4b1b-a16d-e25717dc9414"]
PRIVATE_MICROSOFT_TEAMS_CHANNELS = ["19:[email protected]"]
@@ -282,11 +283,10 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):
)
self.assertSetEqual(imported_user_emails, set(EXPORTED_MICROSOFT_TEAMS_USER_EMAIL.values()))
- # For now the importer doesn't generate any mirror dummy accounts.
mirror_dummy_accounts = self.get_imported_realm_user_field_values(
"id", is_mirror_dummy=True, is_active=False
)
- self.assertListEqual(mirror_dummy_accounts, [])
+ self.assert_length(mirror_dummy_accounts, 1)
imported_realm_owner_emails = set(
self.get_imported_realm_user_field_values("email", role=UserProfile.ROLE_REALM_OWNER)
@@ -301,8 +301,15 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):
raw_exported_users_data = self.get_exported_microsoft_teams_user_data()
raw_exported_user_full_names = [user["DisplayName"] for user in raw_exported_users_data]
+ deleted_user_full_names = [
+ f"Deleted Teams user {user_id}" for user_id in DELETED_MICROSOFT_TEAMS_USERS
+ ]
+
imported_user_full_names = self.get_imported_realm_user_field_values("full_name")
- self.assertEqual(sorted(raw_exported_user_full_names), sorted(imported_user_full_names))
+ self.assertEqual(
+ sorted(raw_exported_user_full_names + deleted_user_full_names),
+ sorted(imported_user_full_names),
+ )
def test_imported_channels(self) -> None:
self.do_import_realm_fixture()
@@ -351,6 +358,7 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):
convertable_exported_message_datetimes: list[float] = []
exported_sender_messages_map: dict[str, list[float]] = defaultdict(list)
private_channel_message_exists: bool
+ deleted_user_message_exists: bool
for message in exported_team_messages:
if is_microsoft_teams_event_message(message):
@@ -360,14 +368,18 @@ class MicrosoftTeamsImporterIntegrationTest(MicrosoftTeamsImportTestCase):
continue
sender_id = get_microsoft_teams_sender_id_from_message(message)
if sender_id not in self.exported_user_data_map:
- continue
- sender_email = self.exported_user_data_map[sender_id]["Mail"]
+ assert sender_id in DELETED_MICROSOFT_TEAMS_USERS
+ sender_email = f"{sender_id}@zulip.example.com"
+ deleted_user_message_exists = True
+ else:
+ sender_email = self.exported_user_data_map[sender_id]["Mail"]
convertable_exported_messages.append(message)
message_datetime = get_timestamp_from_message(message)
convertable_exported_message_datetimes.append(message_datetime)
exported_sender_messages_map[sender_email].append(message_datetime)
self.assertTrue(private_channel_message_exists)
+ self.assertTrue(deleted_user_message_exists)
imported_channel_messages = (
Message.objects.filter(