create-realm: Update notification message sent to admin realm.

When a new realm is created, a notification message is sent to
the realm configured as the settings.SYSTEM_BOT_REALM if there
is a "signups" stream that exists in that realm. This is used
for Zulip Cloud, but is an undocumented feature.

The topic of the message has been the subdomain of the new realm,
and the message content has been "Signups enabled" translated
into the default language of the new realm.

In order to make these messages more explicitly for Zulip Cloud,
the settings.CORPORATE_ENABLED is checked before sending these
messages.

To make these messages more useful, the topic for these
notifications is changed to be "new organizations". The content
of these messages is updated to have the new realm name (with a
link to the admin realm's activity support page for the realm),
subdomain (with a link to the realm), and organization type.
This commit is contained in:
Lauryn Menard 2023-03-10 16:03:18 +01:00 committed by Tim Abbott
parent 36475daba7
commit ffcdc13819
2 changed files with 41 additions and 21 deletions

View File

@ -5,7 +5,6 @@ from typing import Any, Dict, Optional
from django.conf import settings
from django.db import transaction
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from zerver.actions.message_send import internal_send_stream_message
from zerver.actions.realm_settings import (
@ -24,10 +23,14 @@ from zerver.models import (
RealmUserDefault,
Stream,
UserProfile,
get_org_type_display_name,
get_realm,
get_system_bot,
)
if settings.CORPORATE_ENABLED:
from corporate.lib.support import get_support_url
def do_change_realm_subdomain(
realm: Realm,
@ -254,25 +257,36 @@ def do_create_realm(
# We use acting_user=None for setting the initial plan type.
do_change_realm_plan_type(realm, Realm.PLAN_TYPE_LIMITED, acting_user=None)
admin_realm = get_realm(settings.SYSTEM_BOT_REALM)
sender = get_system_bot(settings.NOTIFICATION_BOT, admin_realm.id)
# Send a notification to the admin realm
signup_message = _("Signups enabled")
# Send a notification to the admin realm when a new organization registers.
if settings.CORPORATE_ENABLED:
admin_realm = get_realm(settings.SYSTEM_BOT_REALM)
sender = get_system_bot(settings.NOTIFICATION_BOT, admin_realm.id)
try:
signups_stream = get_signups_stream(admin_realm)
topic = realm.display_subdomain
support_url = get_support_url(realm)
organization_type = get_org_type_display_name(realm.org_type)
internal_send_stream_message(
sender,
signups_stream,
topic,
signup_message,
message = "[{name}]({support_link}) ([{subdomain}]({realm_link})). Organization type: {type}".format(
name=realm.name,
subdomain=realm.display_subdomain,
realm_link=realm.uri,
support_link=support_url,
type=organization_type,
)
except Stream.DoesNotExist: # nocoverage
# If the signups stream hasn't been created in the admin
# realm, don't auto-create it to send to it; just do nothing.
pass
topic = "new organizations"
try:
signups_stream = get_signups_stream(admin_realm)
internal_send_stream_message(
sender,
signups_stream,
topic,
message,
)
except Stream.DoesNotExist: # nocoverage
# If the signups stream hasn't been created in the admin
# realm, don't auto-create it to send to it; just do nothing.
pass
setup_realm_internal_bots(realm)
return realm

View File

@ -1251,6 +1251,7 @@ class RealmCreationTest(ZulipTestCase):
signups_stream, _ = create_stream_if_needed(notification_bot.realm, "signups")
string_id = "zuliptest"
org_name = "Zulip Test"
# Make sure the realm does not exist
with self.assertRaises(Realm.DoesNotExist):
get_realm(string_id)
@ -1276,7 +1277,9 @@ class RealmCreationTest(ZulipTestCase):
result = self.client_get(confirmation_url)
self.assertEqual(result.status_code, 200)
result = self.submit_reg_form_for_user(email, password, realm_subdomain=string_id)
result = self.submit_reg_form_for_user(
email, password, realm_subdomain=string_id, realm_name=org_name
)
self.assertEqual(result.status_code, 302)
self.assertTrue(
result["Location"].startswith("http://zuliptest.testserver/accounts/login/subdomain/")
@ -1307,12 +1310,15 @@ class RealmCreationTest(ZulipTestCase):
self.assert_length(messages, message_count)
self.assertIn(text, messages[0].content)
# Check signup messages
# Check admin organization's signups stream messages
recipient = signups_stream.recipient
messages = Message.objects.filter(recipient=recipient).order_by("id")
self.assert_length(messages, 1)
self.assertIn("Signups enabled", messages[0].content)
self.assertEqual("zuliptest", messages[0].topic_name())
# Check organization name, subdomain and organization type are in message content
self.assertIn("Zulip Test", messages[0].content)
self.assertIn("zuliptest", messages[0].content)
self.assertIn("Organization type: Business", messages[0].content)
self.assertEqual("new organizations", messages[0].topic_name())
realm_creation_audit_log = RealmAuditLog.objects.get(
realm=realm, event_type=RealmAuditLog.REALM_CREATED