2017-08-02 05:20:50 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.actions import set_default_streams, \
|
2019-02-09 03:01:35 +01:00
|
|
|
internal_prep_stream_message_by_name, internal_send_private_message, \
|
2019-02-02 23:53:55 +01:00
|
|
|
create_streams_if_needed, do_send_messages, \
|
2018-05-23 05:40:28 +02:00
|
|
|
do_add_reaction_legacy, create_users, missing_any_realm_internal_bots
|
2018-11-11 20:35:31 +01:00
|
|
|
from zerver.lib.topic import get_turtle_message
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.models import Realm, UserProfile, get_system_bot
|
2017-08-02 05:20:50 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
from typing import Any, Dict, List, Mapping
|
2017-08-02 05:20:50 +02:00
|
|
|
|
2018-01-12 09:10:43 +01:00
|
|
|
def setup_realm_internal_bots(realm: Realm) -> None:
|
2018-02-07 03:59:57 +01:00
|
|
|
"""Create this realm's internal bots.
|
|
|
|
|
|
|
|
This function is idempotent; it does nothing for a bot that
|
|
|
|
already exists.
|
|
|
|
"""
|
2018-01-12 09:10:43 +01:00
|
|
|
internal_bots = [(bot['name'], bot['email_template'] % (settings.INTERNAL_BOT_DOMAIN,))
|
|
|
|
for bot in settings.REALM_INTERNAL_BOTS]
|
|
|
|
create_users(realm, internal_bots, bot_type=UserProfile.DEFAULT_BOT)
|
|
|
|
bots = UserProfile.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
email__in=[bot_info[1] for bot_info in internal_bots],
|
|
|
|
bot_owner__isnull=True
|
|
|
|
)
|
|
|
|
for bot in bots:
|
|
|
|
bot.bot_owner = bot
|
|
|
|
bot.save()
|
|
|
|
|
2018-05-23 05:40:28 +02:00
|
|
|
def create_if_missing_realm_internal_bots() -> None:
|
|
|
|
"""This checks if there is any realm internal bot missing.
|
|
|
|
|
|
|
|
If that is the case, it creates the missing realm internal bots.
|
|
|
|
"""
|
|
|
|
if missing_any_realm_internal_bots():
|
|
|
|
for realm in Realm.objects.all():
|
|
|
|
setup_realm_internal_bots(realm)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def send_initial_pms(user: UserProfile) -> None:
|
2017-08-18 03:44:29 +02:00
|
|
|
organization_setup_text = ""
|
|
|
|
if user.is_realm_admin:
|
2017-11-10 03:34:13 +01:00
|
|
|
help_url = user.realm.uri + "/help/getting-your-organization-started-with-zulip"
|
|
|
|
organization_setup_text = ("* [Read the guide](%s) for getting your organization "
|
|
|
|
"started with Zulip\n" % (help_url,))
|
2017-08-18 03:44:29 +02:00
|
|
|
|
2017-08-02 05:20:50 +02:00
|
|
|
content = (
|
|
|
|
"Hello, and welcome to Zulip!\n\nThis is a private message from me, Welcome Bot. "
|
|
|
|
"Here are some tips to get you started:\n"
|
|
|
|
"* Download our [Desktop and mobile apps](/apps)\n"
|
2017-08-18 03:44:29 +02:00
|
|
|
"* Customize your account and notifications on your [Settings page](#settings)\n"
|
|
|
|
"* Type `?` to check out Zulip's keyboard shortcuts\n"
|
|
|
|
"%s"
|
|
|
|
"\n"
|
|
|
|
"The most important shortcut is `r` to reply.\n\n"
|
2017-08-02 05:20:50 +02:00
|
|
|
"Practice sending a few messages by replying to this conversation. If you're not into "
|
2017-08-18 03:44:29 +02:00
|
|
|
"keyboards, that's okay too; clicking anywhere on this message will also do the trick!") \
|
|
|
|
% (organization_setup_text,)
|
2017-08-02 05:20:50 +02:00
|
|
|
|
|
|
|
internal_send_private_message(user.realm, get_system_bot(settings.WELCOME_BOT),
|
2017-08-18 05:02:02 +02:00
|
|
|
user, content)
|
2017-08-02 05:20:50 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def setup_initial_streams(realm: Realm) -> None:
|
2017-08-02 05:20:50 +02:00
|
|
|
stream_dicts = [
|
2019-02-23 23:14:31 +01:00
|
|
|
{'name': "announce"},
|
2017-08-02 05:20:50 +02:00
|
|
|
{'name': "new members",
|
|
|
|
'description': "For welcoming and onboarding new members. If you haven't yet, "
|
|
|
|
"introduce yourself in a new thread using your name as the topic!"},
|
|
|
|
{'name': "zulip",
|
|
|
|
'description': "For discussing Zulip, Zulip tips and tricks, and asking "
|
|
|
|
"questions about how Zulip works"}] # type: List[Mapping[str, Any]]
|
|
|
|
create_streams_if_needed(realm, stream_dicts)
|
|
|
|
set_default_streams(realm, {stream['name']: {} for stream in stream_dicts})
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def send_initial_realm_messages(realm: Realm) -> None:
|
2017-08-02 06:02:06 +02:00
|
|
|
welcome_bot = get_system_bot(settings.WELCOME_BOT)
|
2017-08-02 05:20:50 +02:00
|
|
|
# Make sure each stream created in the realm creation process has at least one message below
|
|
|
|
# Order corresponds to the ordering of the streams on the left sidebar, to make the initial Home
|
|
|
|
# view slightly less overwhelming
|
|
|
|
welcome_messages = [
|
2019-02-23 23:14:31 +01:00
|
|
|
{'stream': "announce",
|
2017-08-02 05:20:50 +02:00
|
|
|
'topic': "welcome",
|
|
|
|
'content': "This is a message on stream `%s` with the topic `welcome`. We'll use this stream "
|
|
|
|
"for system-generated notifications." % (Realm.DEFAULT_NOTIFICATION_STREAM_NAME,)},
|
2017-10-04 02:07:44 +02:00
|
|
|
{'stream': Realm.INITIAL_PRIVATE_STREAM_NAME,
|
2017-08-02 05:20:50 +02:00
|
|
|
'topic': "private streams",
|
|
|
|
'content': "This is a private stream. Only admins and people you invite "
|
|
|
|
"to the stream will be able to see that this stream exists."},
|
|
|
|
{'stream': "general",
|
|
|
|
'topic': "welcome",
|
|
|
|
'content': "Welcome to #**general**."},
|
|
|
|
{'stream': "new members",
|
|
|
|
'topic': "onboarding",
|
|
|
|
'content': "A #**new members** stream is great for onboarding new members.\n\nIf you're "
|
|
|
|
"reading this and aren't the first person here, introduce yourself in a new thread "
|
|
|
|
"using your name as the topic! Type `c` or click on `New Topic` at the bottom of the "
|
|
|
|
"screen to start a new topic."},
|
|
|
|
{'stream': "zulip",
|
|
|
|
'topic': "topic demonstration",
|
|
|
|
'content': "Here is a message in one topic. Replies to this message will go to this topic."},
|
|
|
|
{'stream': "zulip",
|
|
|
|
'topic': "topic demonstration",
|
2017-08-02 05:28:09 +02:00
|
|
|
'content': "A second message in this topic. With [turtles](/static/images/cute/turtle.png)!"},
|
2017-08-02 05:20:50 +02:00
|
|
|
{'stream': "zulip",
|
|
|
|
'topic': "second topic",
|
|
|
|
'content': "This is a message in a second topic.\n\nTopics are similar to email subjects, "
|
|
|
|
"in that each conversation should get its own topic. Keep them short, though; one "
|
|
|
|
"or two words will do it!"},
|
2018-05-11 01:40:23 +02:00
|
|
|
] # type: List[Dict[str, str]]
|
2019-02-09 03:01:35 +01:00
|
|
|
messages = [internal_prep_stream_message_by_name(
|
|
|
|
realm, welcome_bot, message['stream'],
|
|
|
|
message['topic'], message['content']
|
2019-02-07 02:05:34 +01:00
|
|
|
) for message in welcome_messages]
|
2017-08-02 06:02:06 +02:00
|
|
|
message_ids = do_send_messages(messages)
|
|
|
|
|
|
|
|
# We find the one of our just-sent messages with turtle.png in it,
|
|
|
|
# and react to it. This is a bit hacky, but works and is kinda a
|
|
|
|
# 1-off thing.
|
2018-11-11 20:35:31 +01:00
|
|
|
turtle_message = get_turtle_message(message_ids=message_ids)
|
2017-10-16 22:02:20 +02:00
|
|
|
do_add_reaction_legacy(welcome_bot, turtle_message, 'turtle')
|