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.

import: Send channel Welcome Bot messages to imported organizations.

Mateusz Mandera2a6c3a7ab8cc
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py
index 7cee57b8492..0b6a59edbb1 100644
--- a/zerver/lib/import_realm.py
+++ b/zerver/lib/import_realm.py
@@ -39,7 +39,11 @@ from zerver.lib.markdown import version as markdown_version
from zerver.lib.message import get_last_message_id
from zerver.lib.migration_status import MigrationStatusJson, parse_migration_status
from zerver.lib.mime_types import guess_type
-from zerver.lib.onboarding import send_initial_direct_message
+from zerver.lib.onboarding import (
+ OnboardingMessageTypeEnum,
+ send_initial_direct_message,
+ send_initial_realm_messages,
+)
from zerver.lib.partial import partial
from zerver.lib.push_notifications import sends_notifications_directly
from zerver.lib.remote_server import maybe_enqueue_audit_log_upload
@@ -1906,12 +1910,31 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
send_zulip_update_announcements_to_realm(realm, skip_delay=False)
# 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)
+ #
+ # Such exports use the default general-type channel such as "general" for
+ # announcements - which happens to be also the channel we want for the initial messages.
+ send_zulip_initial_messages_after_import(
+ realm, target_channel=realm.zulip_update_announcements_stream
+ )
return realm
-def send_zulip_initial_messages_after_import(realm: Realm) -> None:
+def send_zulip_initial_messages_after_import(realm: Realm, target_channel: Stream | None) -> None:
+ if target_channel is not None:
+ send_initial_realm_messages(
+ realm,
+ override_channel_name_map={
+ OnboardingMessageTypeEnum.moving_messages: target_channel.name,
+ OnboardingMessageTypeEnum.welcome_to_zulip: target_channel.name,
+ OnboardingMessageTypeEnum.experiments: target_channel.name,
+ OnboardingMessageTypeEnum.greetings: target_channel.name,
+ # We shouldn't encourage people to start random threads in their organization's #general-type channel,
+ # so we skip sending these initial messages at all.
+ OnboardingMessageTypeEnum.start_conversation: None,
+ },
+ )
+
BATCH_SIZE = 1000
lower_bound_id = 0
diff --git a/zerver/lib/onboarding.py b/zerver/lib/onboarding.py
index efc9bb5e170..1787acd451c 100644
--- a/zerver/lib/onboarding.py
+++ b/zerver/lib/onboarding.py
@@ -1,3 +1,5 @@
+from enum import Enum
+
from django.conf import settings
from django.db import transaction
from django.db.models import Count
@@ -240,8 +242,40 @@ def send_welcome_bot_response(send_request: SendMessageRequest) -> None:
)
+class OnboardingMessageTypeEnum(Enum):
+ moving_messages = 1
+ welcome_to_zulip = 2
+ start_conversation = 3
+ experiments = 4
+ greetings = 5
+
+
@transaction.atomic(savepoint=False)
-def send_initial_realm_messages(realm: Realm) -> None:
+def send_initial_realm_messages(
+ realm: Realm,
+ override_channel_name_map: dict[OnboardingMessageTypeEnum, str | None] | None = None,
+) -> None:
+ """
+ override_channel_name_map allows the caller to customize to which channels to send
+ specific categories of the initial messages. In this override dict, every category
+ from OnboardingMessageTypeEnum must be specified.
+
+ The caller can disable the sending of a specific category by mapping it to None
+ in override_channel_name_map.
+ """
+
+ if override_channel_name_map is not None:
+ channel_name_map = override_channel_name_map
+ else:
+ channel_name_map = {
+ OnboardingMessageTypeEnum.moving_messages: str(Realm.ZULIP_DISCUSSION_CHANNEL_NAME),
+ OnboardingMessageTypeEnum.welcome_to_zulip: str(Realm.ZULIP_DISCUSSION_CHANNEL_NAME),
+ OnboardingMessageTypeEnum.start_conversation: str(Realm.ZULIP_SANDBOX_CHANNEL_NAME),
+ OnboardingMessageTypeEnum.experiments: str(Realm.ZULIP_SANDBOX_CHANNEL_NAME),
+ OnboardingMessageTypeEnum.greetings: str(Realm.DEFAULT_NOTIFICATION_STREAM_NAME),
+ }
+ assert set(channel_name_map.keys()) == set(OnboardingMessageTypeEnum)
+
# Sends the initial messages for a new organization.
#
# Technical note: Each stream created in the realm creation
@@ -278,7 +312,7 @@ For example, this message is in the “{topic_name}” topic in the
#**{zulip_discussion_channel_name}** channel, as you can see in the left sidebar
and above.
""").format(
- zulip_discussion_channel_name=str(Realm.ZULIP_DISCUSSION_CHANNEL_NAME),
+ zulip_discussion_channel_name=channel_name_map[OnboardingMessageTypeEnum.welcome_to_zulip],
topic_name=_("welcome to Zulip!"),
)
@@ -330,7 +364,7 @@ Link to a conversation: #**{zulip_discussion_channel_name}>{topic_name}**
```
""")
).format(
- zulip_discussion_channel_name=str(Realm.ZULIP_DISCUSSION_CHANNEL_NAME),
+ zulip_discussion_channel_name=channel_name_map[OnboardingMessageTypeEnum.welcome_to_zulip],
topic_name=_("welcome to Zulip!"),
)
@@ -352,66 +386,85 @@ This **greetings** topic is a great place to say “hi” :wave: to your teammat
# Initial messages are configured below.
# Advertising moving messages.
- welcome_messages += [
- {
- "channel_name": str(Realm.ZULIP_DISCUSSION_CHANNEL_NAME),
- "topic_name": _("moving messages"),
- "content": content,
- }
- for content in [
- content1_of_moving_messages_topic_name,
- content2_of_moving_messages_topic_name,
+ if (
+ moving_messages_channel_name := channel_name_map[OnboardingMessageTypeEnum.moving_messages]
+ ) is not None:
+ welcome_messages += [
+ {
+ "channel_name": moving_messages_channel_name,
+ "topic_name": _("moving messages"),
+ "content": content,
+ }
+ for content in [
+ content1_of_moving_messages_topic_name,
+ content2_of_moving_messages_topic_name,
+ ]
]
- ]
# Suggestion to test messaging features.
# Dependency on knowing how to send messages.
- welcome_messages += [
- {
- "channel_name": str(Realm.ZULIP_SANDBOX_CHANNEL_NAME),
- "topic_name": _("experiments"),
- "content": content,
- }
- for content in [content1_of_experiments_topic_name, content2_of_experiments_topic_name]
- ]
+ if (
+ experiments_channel_name := channel_name_map[OnboardingMessageTypeEnum.experiments]
+ ) is not None:
+ welcome_messages += [
+ {
+ "channel_name": experiments_channel_name,
+ "topic_name": _("experiments"),
+ "content": content,
+ }
+ for content in [content1_of_experiments_topic_name, content2_of_experiments_topic_name]
+ ]
- # Suggestion to start your first new conversation.
- welcome_messages += [
- {
- "channel_name": str(Realm.ZULIP_SANDBOX_CHANNEL_NAME),
- "topic_name": _("start a conversation"),
- "content": content,
- }
- for content in [
- content1_of_start_conversation_topic_name,
- content2_of_start_conversation_topic_name,
- content3_of_start_conversation_topic_name,
+ if (
+ start_conversation_channel_name := channel_name_map[
+ OnboardingMessageTypeEnum.start_conversation
+ ]
+ ) is not None:
+ # Suggestion to start your first new conversation.
+ welcome_messages += [
+ {
+ "channel_name": start_conversation_channel_name,
+ "topic_name": _("start a conversation"),
+ "content": content,
+ }
+ for content in [
+ content1_of_start_conversation_topic_name,
+ content2_of_start_conversation_topic_name,
+ content3_of_start_conversation_topic_name,
+ ]
]
- ]
# Suggestion to send first message as a hi to your team.
- welcome_messages += [
- {
- "channel_name": str(Realm.DEFAULT_NOTIFICATION_STREAM_NAME),
- "topic_name": _("greetings"),
- "content": content,
- }
- for content in [content1_of_greetings_topic_name, content2_of_greetings_topic_name]
- ]
+ if (
+ greetings_channel_name := channel_name_map[OnboardingMessageTypeEnum.greetings]
+ ) is not None:
+ welcome_messages += [
+ {
+ "channel_name": greetings_channel_name,
+ "topic_name": _("greetings"),
+ "content": content,
+ }
+ for content in [content1_of_greetings_topic_name, content2_of_greetings_topic_name]
+ ]
- # Main welcome message, this should be last.
- welcome_messages += [
- {
- "channel_name": str(Realm.ZULIP_DISCUSSION_CHANNEL_NAME),
- "topic_name": _("welcome to Zulip!"),
- "content": content,
- }
- for content in [
- content1_of_welcome_to_zulip_topic_name,
- content2_of_welcome_to_zulip_topic_name,
- content3_of_welcome_to_zulip_topic_name,
+ if (
+ welcome_to_zulip_channel_name := channel_name_map[
+ OnboardingMessageTypeEnum.welcome_to_zulip
+ ]
+ ) is not None:
+ # Main welcome message, this should be last.
+ welcome_messages += [
+ {
+ "channel_name": welcome_to_zulip_channel_name,
+ "topic_name": _("welcome to Zulip!"),
+ "content": content,
+ }
+ for content in [
+ content1_of_welcome_to_zulip_topic_name,
+ content2_of_welcome_to_zulip_topic_name,
+ content3_of_welcome_to_zulip_topic_name,
+ ]
]
- ]
# End of message declarations; now we actually send them.
diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.py
index 293cac09d6f..80f4ac7bb6a 100644
--- a/zerver/tests/test_mattermost_importer.py
+++ b/zerver/tests/test_mattermost_importer.py
@@ -894,11 +894,11 @@ class MatterMostImporter(ZulipTestCase):
messages = Message.objects.filter(realm=realm)
for message in messages:
self.assertIsNotNone(message.rendered_content)
- self.assert_length(messages, 15)
+ self.assert_length(messages, 24)
stream_messages = messages.filter(recipient__type=Recipient.STREAM).order_by("date_sent")
stream_recipients = stream_messages.values_list("recipient", flat=True)
- self.assert_length(stream_messages, 4)
+ self.assert_length(stream_messages, 13)
self.assert_length(set(stream_recipients), 2)
self.assertEqual(stream_messages[0].sender.email, "[email protected]")
self.assertEqual(stream_messages[0].content, "ron joined the channel.\n\n")
diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.py
index 236c3993262..05c7c846ded 100644
--- a/zerver/tests/test_rocketchat_importer.py
+++ b/zerver/tests/test_rocketchat_importer.py
@@ -1043,11 +1043,11 @@ 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, 49)
+ self.assert_length(messages, 58)
stream_messages = messages.filter(recipient__type=Recipient.STREAM).order_by("date_sent")
stream_recipients = stream_messages.values_list("recipient", flat=True)
- self.assert_length(stream_messages, 35)
+ self.assert_length(stream_messages, 44)
self.assert_length(set(stream_recipients), 5)
self.assertEqual(stream_messages[0].sender.email, "[email protected]")
self.assertEqual(stream_messages[0].content, "Hey everyone, how's it going??")
diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py
index 2ab6b15d083..5ea5e27ac76 100644
--- a/zerver/tests/test_slack_importer.py
+++ b/zerver/tests/test_slack_importer.py
@@ -1967,7 +1967,7 @@ by Pieter
},
)
- self.assertEqual(Message.objects.filter(realm=realm).count(), 87)
+ self.assertEqual(Message.objects.filter(realm=realm).count(), 96)
# All auth backends are enabled initially.
self.assertTrue(all(realm.authentication_methods_dict().values()))