2020-11-24 12:31:28 +01:00
|
|
|
from typing import Dict, List
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-08-02 05:20:50 +02:00
|
|
|
from django.conf import settings
|
2020-11-14 09:10:24 +01:00
|
|
|
from django.db import transaction
|
2020-03-28 10:02:45 +01:00
|
|
|
from django.db.models import Count
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2017-08-02 05:20:50 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.actions import (
|
|
|
|
create_users,
|
|
|
|
do_add_reaction,
|
|
|
|
do_send_messages,
|
|
|
|
internal_prep_stream_message_by_name,
|
|
|
|
internal_send_private_message,
|
|
|
|
)
|
2019-08-15 20:31:01 +02:00
|
|
|
from zerver.lib.emoji import emoji_name_to_emoji_code
|
2020-11-24 12:31:28 +01:00
|
|
|
from zerver.lib.message import SendMessageRequest
|
2019-02-24 22:22:32 +01:00
|
|
|
from zerver.models import Message, Realm, UserProfile, get_system_bot
|
2017-08-02 05:20:50 +02:00
|
|
|
|
|
|
|
|
2020-03-28 10:02:45 +01:00
|
|
|
def missing_any_realm_internal_bots() -> bool:
|
2021-02-12 08:19:30 +01:00
|
|
|
bot_emails = [
|
2021-02-12 08:20:45 +01:00
|
|
|
bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,)
|
2021-02-12 08:19:30 +01:00
|
|
|
for bot in settings.REALM_INTERNAL_BOTS
|
|
|
|
]
|
|
|
|
bot_counts = dict(
|
2021-02-12 08:20:45 +01:00
|
|
|
UserProfile.objects.filter(email__in=bot_emails).values_list("email").annotate(Count("id"))
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-03-28 10:02:45 +01:00
|
|
|
realm_count = Realm.objects.count()
|
|
|
|
return any(bot_counts.get(email, 0) < realm_count for email in bot_emails)
|
|
|
|
|
2021-02-12 08:19:30 +01: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.
|
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
internal_bots = [
|
2021-02-12 08:20:45 +01:00
|
|
|
(bot["name"], bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,))
|
2021-02-12 08:19:30 +01:00
|
|
|
for bot in settings.REALM_INTERNAL_BOTS
|
|
|
|
]
|
2018-01-12 09:10:43 +01:00
|
|
|
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],
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
bot_owner__isnull=True,
|
2018-01-12 09:10:43 +01:00
|
|
|
)
|
|
|
|
for bot in bots:
|
|
|
|
bot.bot_owner = bot
|
|
|
|
bot.save()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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"
|
2020-04-10 16:49:18 +02:00
|
|
|
organization_setup_text = (
|
2021-02-12 08:19:30 +01:00
|
|
|
"* "
|
|
|
|
+ _("[Read the guide]({help_url}) for getting your organization started with Zulip")
|
|
|
|
+ "\n"
|
2020-04-10 16:49:18 +02:00
|
|
|
).format(help_url=help_url)
|
2017-08-18 03:44:29 +02:00
|
|
|
|
2017-08-02 05:20:50 +02:00
|
|
|
content = (
|
2021-02-12 08:19:30 +01:00
|
|
|
_("Hello, and welcome to Zulip!") + "\n"
|
2020-04-10 16:49:18 +02:00
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
+ _("This is a private message from me, Welcome Bot.")
|
|
|
|
+ " "
|
|
|
|
+ _("Here are some tips to get you started:")
|
|
|
|
+ "\n"
|
|
|
|
"* " + _("Download our [Desktop and mobile apps]({apps_url})") + "\n"
|
|
|
|
"* "
|
|
|
|
+ _("Customize your account and notifications on your [Settings page]({settings_url})")
|
|
|
|
+ "\n"
|
|
|
|
"* " + _("Type `?` to check out Zulip's keyboard shortcuts") + "\n"
|
2020-04-10 16:49:18 +02:00
|
|
|
"{organization_setup_text}"
|
2021-02-12 08:19:30 +01:00
|
|
|
"\n" + _("The most important shortcut is `r` to reply.") + "\n"
|
2020-04-10 16:49:18 +02:00
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
+ _("Practice sending a few messages by replying to this conversation.")
|
|
|
|
+ " "
|
|
|
|
+ _(
|
|
|
|
"If you're not into keyboards, that's okay too; "
|
|
|
|
"clicking anywhere on this message will also do the trick!"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
content = content.format(
|
|
|
|
apps_url="/apps", settings_url="#settings", organization_setup_text=organization_setup_text
|
2020-04-10 16:49:18 +02:00
|
|
|
)
|
|
|
|
|
2021-07-26 17:04:16 +02:00
|
|
|
internal_send_private_message(
|
|
|
|
get_system_bot(settings.WELCOME_BOT, user.realm_id), user, content
|
|
|
|
)
|
2017-08-02 05:20:50 +02:00
|
|
|
|
|
|
|
|
2020-11-24 12:31:28 +01:00
|
|
|
def send_welcome_bot_response(send_request: SendMessageRequest) -> None:
|
2021-07-26 17:04:16 +02:00
|
|
|
welcome_bot = get_system_bot(settings.WELCOME_BOT, send_request.message.sender.realm_id)
|
2020-11-24 12:31:28 +01:00
|
|
|
human_recipient_id = send_request.message.sender.recipient_id
|
2020-09-03 20:28:29 +02:00
|
|
|
if Message.objects.filter(sender=welcome_bot, recipient_id=human_recipient_id).count() < 2:
|
|
|
|
content = (
|
2021-02-12 08:19:30 +01:00
|
|
|
_("Congratulations on your first reply!") + " "
|
2020-09-03 20:28:29 +02:00
|
|
|
":tada:"
|
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
"\n"
|
|
|
|
+ _(
|
|
|
|
"Feel free to continue using this space to practice your new messaging "
|
|
|
|
"skills. Or, try clicking on some of the stream names to your left!"
|
|
|
|
)
|
2020-09-03 20:28:29 +02:00
|
|
|
)
|
2021-02-18 19:58:04 +01:00
|
|
|
internal_send_private_message(welcome_bot, send_request.message.sender, content)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-09-03 20:28:29 +02:00
|
|
|
|
2020-11-14 09:10:24 +01:00
|
|
|
@transaction.atomic
|
2017-11-05 11:15:10 +01:00
|
|
|
def send_initial_realm_messages(realm: Realm) -> None:
|
2021-07-26 17:04:16 +02:00
|
|
|
welcome_bot = get_system_bot(settings.WELCOME_BOT, realm.id)
|
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
|
2020-04-10 18:50:22 +02:00
|
|
|
content_of_private_streams_topic = (
|
2021-02-12 08:19:30 +01:00
|
|
|
_("This is a private stream, as indicated by the lock icon next to the stream name.")
|
|
|
|
+ " "
|
|
|
|
+ _("Private streams are only visible to stream members.")
|
|
|
|
+ "\n"
|
2020-04-10 18:50:22 +02:00
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
+ _(
|
|
|
|
"To manage this stream, go to [Stream settings]({stream_settings_url}) "
|
|
|
|
"and click on `{initial_private_stream_name}`."
|
|
|
|
)
|
|
|
|
).format(
|
|
|
|
stream_settings_url="#streams/subscribed",
|
|
|
|
initial_private_stream_name=Realm.INITIAL_PRIVATE_STREAM_NAME,
|
|
|
|
)
|
2020-04-10 18:50:22 +02:00
|
|
|
|
|
|
|
content1_of_topic_demonstration_topic = (
|
2021-02-12 08:19:30 +01:00
|
|
|
_(
|
|
|
|
"This is a message on stream #**{default_notification_stream_name}** with the "
|
|
|
|
"topic `topic demonstration`."
|
|
|
|
)
|
2020-04-10 18:50:22 +02:00
|
|
|
).format(default_notification_stream_name=Realm.DEFAULT_NOTIFICATION_STREAM_NAME)
|
|
|
|
|
|
|
|
content2_of_topic_demonstration_topic = (
|
2021-02-12 08:19:30 +01:00
|
|
|
_("Topics are a lightweight tool to keep conversations organized.")
|
|
|
|
+ " "
|
|
|
|
+ _("You can learn more about topics at [Streams and topics]({about_topics_help_url}).")
|
2020-04-10 18:50:22 +02:00
|
|
|
).format(about_topics_help_url="/help/about-streams-and-topics")
|
|
|
|
|
|
|
|
content_of_swimming_turtles_topic = (
|
2021-02-12 08:19:30 +01:00
|
|
|
_(
|
|
|
|
"This is a message on stream #**{default_notification_stream_name}** with the "
|
|
|
|
"topic `swimming turtles`."
|
|
|
|
)
|
|
|
|
+ "\n"
|
2020-04-10 18:50:22 +02:00
|
|
|
"\n"
|
|
|
|
"[](/static/images/cute/turtle.png)"
|
|
|
|
"\n"
|
2021-02-12 08:19:30 +01:00
|
|
|
"\n"
|
|
|
|
+ _(
|
|
|
|
"[Start a new topic]({start_topic_help_url}) any time you're not replying to a \
|
|
|
|
previous message."
|
|
|
|
)
|
|
|
|
).format(
|
|
|
|
default_notification_stream_name=Realm.DEFAULT_NOTIFICATION_STREAM_NAME,
|
|
|
|
start_topic_help_url="/help/start-a-new-topic",
|
|
|
|
)
|
2020-04-10 18:50:22 +02:00
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
welcome_messages: List[Dict[str, str]] = [
|
2021-02-12 08:19:30 +01:00
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"stream": Realm.INITIAL_PRIVATE_STREAM_NAME,
|
|
|
|
"topic": "private streams",
|
|
|
|
"content": content_of_private_streams_topic,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"stream": Realm.DEFAULT_NOTIFICATION_STREAM_NAME,
|
|
|
|
"topic": "topic demonstration",
|
|
|
|
"content": content1_of_topic_demonstration_topic,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"stream": Realm.DEFAULT_NOTIFICATION_STREAM_NAME,
|
|
|
|
"topic": "topic demonstration",
|
|
|
|
"content": content2_of_topic_demonstration_topic,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"stream": realm.DEFAULT_NOTIFICATION_STREAM_NAME,
|
|
|
|
"topic": "swimming turtles",
|
|
|
|
"content": content_of_swimming_turtles_topic,
|
2021-02-12 08:19:30 +01:00
|
|
|
},
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
]
|
2020-04-10 18:50:22 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
messages = [
|
|
|
|
internal_prep_stream_message_by_name(
|
|
|
|
realm,
|
|
|
|
welcome_bot,
|
2021-02-12 08:20:45 +01:00
|
|
|
message["stream"],
|
|
|
|
message["topic"],
|
|
|
|
message["content"],
|
2021-02-12 08:19:30 +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.
|
2020-11-14 09:10:24 +01:00
|
|
|
turtle_message = Message.objects.select_for_update().get(
|
|
|
|
id__in=message_ids, content__icontains="cute/turtle.png"
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
(emoji_code, reaction_type) = emoji_name_to_emoji_code(realm, "turtle")
|
|
|
|
do_add_reaction(welcome_bot, turtle_message, "turtle", emoji_code, reaction_type)
|