Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
import: Send Welcome Bot messages to users from non-Zulip exports.
Mateusz Mandera35ed45ce4415
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex d4c00363b74..81a1f596a35 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -34,6 +34,7 @@ from zerver.lib.markdown import version as markdown_versionfrom zerver.lib.message import get_last_message_idfrom zerver.lib.migration_status import MigrationStatusJson, parse_migration_statusfrom zerver.lib.mime_types import guess_type+from zerver.lib.onboarding import send_initial_direct_messagefrom zerver.lib.partial import partialfrom zerver.lib.push_notifications import sends_notifications_directlyfrom zerver.lib.remote_server import maybe_enqueue_audit_log_upload@@ -1860,18 +1861,37 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea# Realm object is reactivated.maybe_enqueue_audit_log_upload(realm)- # If the export was NOT generated by another zulip server, the- # 'zulip_update_announcements_level' is set to None by default.- # Set it to the latest level to avoid receiving older update messages.is_realm_imported_from_other_zulip_server = import_source == "zulip"if not is_realm_imported_from_other_zulip_server:+ # If the export was NOT generated by another zulip server, the+ # 'zulip_update_announcements_level' is set to None by default.+ # Set it to the latest level to avoid receiving older update messages.send_zulip_update_announcements_to_realm(realm, skip_delay=False, realm_imported_from_other_product=True)+ # Exports from others tools naturally don't have the initial Welcome Bot+ # messages for users, so we need to create them.+ send_zulip_initial_messages_after_import(realm)return realm+def send_zulip_initial_messages_after_import(realm: Realm) -> None:+ BATCH_SIZE = 1000+ lower_bound_id = 0++ # We use batching to avoid fetching a large number of UserProfile objects+ # in one go, if the realm is huge.+ while user_profiles_batch := list(+ UserProfile.objects.filter(+ realm=realm, is_active=True, is_bot=False, id__gt=lower_bound_id+ ).order_by("id")[:BATCH_SIZE]+ ):+ for user_profile in user_profiles_batch:+ send_initial_direct_message(user_profile)+ lower_bound_id = user_profiles_batch[-1].id++def update_message_foreign_keys(import_dir: Path, sort_by_date: bool) -> None:old_id_list = get_incoming_message_ids(import_dir=import_dir,diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.pyindex 66281bfcaf6..bf97a148d29 100644--- a/zerver/tests/test_mattermost_importer.py+++ b/zerver/tests/test_mattermost_importer.py@@ -877,7 +877,7 @@ class MatterMostImporter(ZulipTestCase):messages = Message.objects.filter(realm=realm)for message in messages:self.assertIsNotNone(message.rendered_content)- self.assert_length(messages, 12)+ self.assert_length(messages, 15)stream_messages = messages.filter(recipient__type=Recipient.STREAM).order_by("date_sent")stream_recipients = stream_messages.values_list("recipient", flat=True)@@ -911,8 +911,8 @@ class MatterMostImporter(ZulipTestCase):"date_sent")personal_recipients = personal_messages.values_list("recipient", flat=True)- self.assert_length(personal_messages, 5)- self.assert_length(set(personal_recipients), 3)+ self.assert_length(personal_messages, 8)+ self.assert_length(set(personal_recipients), 4)self.assertEqual(personal_messages[0].sender.email, "[email protected]")self.assertRegex(personal_messages[0].content, "hey harry\n\n\\[harry-ron.jpg\\]\\(.*\\)")self.assertTrue(personal_messages[0].has_attachment)diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.pyindex e382c1a15d1..cde445e1cac 100644--- a/zerver/tests/test_rocketchat_importer.py+++ b/zerver/tests/test_rocketchat_importer.py@@ -1030,7 +1030,7 @@ class RocketChatImporter(ZulipTestCase):self.assertIsNotNone(message.rendered_content)# After removing user_joined, added_user, discussion_created, etc.# messages. (Total messages were 66.)- self.assert_length(messages, 44)+ self.assert_length(messages, 49)stream_messages = messages.filter(recipient__type=Recipient.STREAM).order_by("date_sent")stream_recipients = stream_messages.values_list("recipient", flat=True)@@ -1071,8 +1071,8 @@ class RocketChatImporter(ZulipTestCase):"date_sent")personal_recipients = personal_messages.values_list("recipient", flat=True)- self.assert_length(personal_messages, 4)- self.assert_length(set(personal_recipients), 2)+ self.assert_length(personal_messages, 9)+ self.assert_length(set(personal_recipients), 5)self.assertEqual(personal_messages[0].sender.email, "[email protected]")self.assertEqual(personal_messages[0].content,diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.pyindex e5b60930233..431f8006bc1 100644--- a/zerver/tests/test_slack_importer.py+++ b/zerver/tests/test_slack_importer.py@@ -1812,7 +1812,7 @@ by Pieter},)- self.assertEqual(Message.objects.filter(realm=realm).count(), 83)+ self.assertEqual(Message.objects.filter(realm=realm).count(), 87)# All auth backends are enabled initially.self.assertTrue(all(realm.authentication_methods_dict().values()))