Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
create_user: Send a group DM to admins for low licenses warning.
Prakhar Pratyush2abc25ac4c21
diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.pyindex 18c1d69464f..364f3cc79f8 100644--- a/zerver/actions/create_user.py+++ b/zerver/actions/create_user.py@@ -11,7 +11,11 @@ from django.utils.translation import override as override_languagefrom analytics.lib.counts import COUNT_STATS, do_increment_logging_statfrom confirmation import settings as confirmation_settingsfrom zerver.actions.invites import notify_invites_changed-from zerver.actions.message_send import internal_send_private_message, internal_send_stream_message+from zerver.actions.message_send import (+ internal_send_huddle_message,+ internal_send_private_message,+ internal_send_stream_message,+)from zerver.actions.streams import bulk_add_subscriptions, send_peer_subscriber_eventsfrom zerver.actions.user_groups import do_send_user_group_members_update_eventfrom zerver.actions.users import change_user_is_active, get_service_dicts_for_bot@@ -81,6 +85,16 @@ def send_message_to_signup_notification_stream(internal_send_stream_message(sender, signup_announcements_stream, topic_name, message)+def send_group_direct_message_to_admins(sender: UserProfile, realm: Realm, content: str) -> None:+ administrators = list(realm.get_human_admin_users())+ internal_send_huddle_message(+ realm,+ sender,+ content,+ recipient_users=administrators,+ )++def notify_new_user(user_profile: UserProfile) -> None:user_count = realm_user_count(user_profile.realm)sender_email = settings.NOTIFICATION_BOT@@ -92,6 +106,7 @@ def notify_new_user(user_profile: UserProfile) -> None:message = _("{user} joined this organization.").format(user=silent_mention_syntax_for_user(user_profile), user_count=user_count)+ send_message_to_signup_notification_stream(sender, user_profile.realm, message)if settings.BILLING_ENABLED:from corporate.lib.registration import generate_licenses_low_warning_message_if_required@@ -102,8 +117,7 @@ def notify_new_user(user_profile: UserProfile) -> None:if licenses_low_warning_message is not None:message += "\n"message += licenses_low_warning_message-- send_message_to_signup_notification_stream(sender, user_profile.realm, message)+ send_group_direct_message_to_admins(sender, user_profile.realm, message)def set_up_streams_for_new_human_user(diff --git a/zerver/tests/test_new_users.py b/zerver/tests/test_new_users.pyindex 3ba6c5c2e25..7eed37c24d6 100644--- a/zerver/tests/test_new_users.py+++ b/zerver/tests/test_new_users.py@@ -16,6 +16,8 @@ from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.timezone import canonicalize_timezonefrom zerver.models import Message, Realm, Recipient, Stream, UserProfilefrom zerver.models.realms import get_realm+from zerver.models.recipients import get_huddle_user_ids+from zerver.models.users import get_system_botfrom zerver.signals import JUST_CREATED_THRESHOLD, get_device_browser, get_device_os@@ -294,10 +296,14 @@ class TestNotifyNewUser(ZulipTestCase):def test_notify_realm_of_new_user_in_manual_license_management(self) -> None:realm = get_realm("zulip")+ admin_user_ids = set(realm.get_human_admin_users().values_list("id", flat=True))+ notification_bot = get_system_bot(settings.NOTIFICATION_BOT, realm.id)+ expected_group_direct_message_user_ids = admin_user_ids | {notification_bot.id}user_count = get_latest_seat_count(realm)+ extra_licenses = 5self.subscribe_realm_to_monthly_plan_on_manual_license_management(- realm, user_count + 5, user_count + 5+ realm, user_count + extra_licenses, user_count + extra_licenses)user_no = 0@@ -316,8 +322,24 @@ class TestNotifyNewUser(ZulipTestCase):notify_new_user(new_user)message = self.get_last_message()- actual_stream = Stream.objects.get(id=message.recipient.type_id)- self.assertEqual(actual_stream, realm.signup_announcements_stream)+ if extra_licenses - user_no > 3:+ # More than 3 licenses remaining. No group DM.+ actual_stream = Stream.objects.get(id=message.recipient.type_id)+ self.assertEqual(actual_stream, realm.signup_announcements_stream)+ else:+ # Stream message+ second_to_last_message = self.get_second_to_last_message()+ actual_stream = Stream.objects.get(id=second_to_last_message.recipient.type_id)+ self.assertEqual(actual_stream, realm.signup_announcements_stream)+ self.assertIn(+ f"@_**new user {user_no}|{new_user.id}** joined this organization.",+ second_to_last_message.content,+ )+ # Group DM+ self.assertEqual(+ set(get_huddle_user_ids(message.recipient)),+ expected_group_direct_message_user_ids,+ )self.assertIn(f"@_**new user {user_no}|{new_user.id}** joined this organization.",message.content,