From e749deb1367e2c7bbe96790e6310630cc13d7fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Wed, 27 Sep 2017 19:43:28 +0200 Subject: [PATCH] onboarding: Add welcome-bot response to initial user message. Fixes #6030. --- zerver/lib/actions.py | 17 ++++++++++++ zerver/tests/test_signup.py | 2 +- zerver/tests/test_tutorial.py | 51 ++++++++++++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 71a458b068..27cc3ad55b 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -702,6 +702,17 @@ def create_mirror_user_if_needed(realm, email, email_to_fullname): except IntegrityError: return get_user(email, realm) +def send_welcome_bot_response(message): + # type: (MutableMapping[str, Any]) -> None + welcome_bot = get_system_bot(settings.WELCOME_BOT) + human_recipient = get_recipient(Recipient.PERSONAL, message['message'].sender.id) + if Message.objects.filter(sender=welcome_bot, recipient=human_recipient).count() < 2: + internal_send_private_message( + message['realm'], welcome_bot, message['message'].sender, + "Congratulations on your first reply! :tada:\n\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!") + def render_incoming_message(message, content, user_ids, realm): # type: (Message, Text, Set[int], Realm) -> Text realm_alert_words = alert_words_in_realm(realm) @@ -1084,6 +1095,12 @@ def do_send_messages(messages_maybe_none): lambda x: None ) + if message['message'].recipient.type == Recipient.PERSONAL: + welcome_bot_id = get_user_profile_by_email(settings.WELCOME_BOT).id + if (welcome_bot_id in message['active_user_ids'] and + welcome_bot_id != message['message'].sender_id): + send_welcome_bot_response(message) + for queue_name, events in message['message'].service_queue_events.items(): for event in events: queue_json_publish( diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 8b150dc514..619e22969d 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -305,7 +305,7 @@ class LoginTest(ZulipTestCase): with queries_captured() as queries: self.register(self.nonreg_email('test'), "test") # Ensure the number of queries we make is not O(streams) - self.assert_length(queries, 68) + self.assert_length(queries, 69) user_profile = self.nonreg_user('test') self.assertEqual(get_session_dict_user(self.client.session), user_profile.id) self.assertFalse(user_profile.enable_stream_desktop_notifications) diff --git a/zerver/tests/test_tutorial.py b/zerver/tests/test_tutorial.py index 7584342660..3b8f25040e 100644 --- a/zerver/tests/test_tutorial.py +++ b/zerver/tests/test_tutorial.py @@ -1,12 +1,23 @@ # -*- coding: utf-8 -*- +from zerver.lib.test_classes import ZulipTestCase +from zerver.lib.test_helpers import message_stream_count, most_recent_message +from zerver.models import get_realm, get_user, Recipient, UserProfile + from typing import Any, Dict import ujson -from zerver.lib.test_classes import ZulipTestCase -from zerver.models import UserProfile - class TutorialTests(ZulipTestCase): + def setUp(self): + # type: () -> None + # This emulates the welcome message sent by the welcome bot to hamlet@zulip.com + # This is only a quick fix - ideally, we would have this message sent by the initialization + # code in populate_db.py + user_email = 'hamlet@zulip.com' + bot_email = 'welcome-bot@zulip.com' + content = 'Shortened welcome message.' + self.send_message(bot_email, user_email, Recipient.PERSONAL, content) + def test_tutorial_status(self): # type: () -> None email = self.example_email('hamlet') @@ -22,3 +33,37 @@ class TutorialTests(ZulipTestCase): self.assert_json_success(result) user = self.example_user('hamlet') self.assertEqual(user.tutorial_status, expected_db_status) + + def test_single_response_to_pm(self): + # type: () -> None + realm = get_realm('zulip') + user_email = 'hamlet@zulip.com' + bot_email = 'welcome-bot@zulip.com' + content = 'whatever' + self.login(user_email) + self.send_message(user_email, bot_email, Recipient.PERSONAL, content) + user = get_user(user_email, realm) + user_messages = message_stream_count(user) + expected_response = ("Congratulations on your first reply! :tada:\n\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!") + self.assertEqual(most_recent_message(user).content, expected_response) + # Welcome bot shouldn't respond to further PMs. + self.send_message(user_email, bot_email, Recipient.PERSONAL, content) + self.assertEqual(message_stream_count(user), user_messages+1) + + def test_no_response_to_group_pm(self): + # type: () -> None + realm = get_realm('zulip') # Assume realm is always 'zulip' + user1_email = self.example_email('hamlet') + user2_email = self.example_email('cordelia') + bot_email = self.example_email('welcome_bot') + content = "whatever" + self.login(user1_email) + self.send_message(user1_email, [bot_email, user2_email], Recipient.PERSONAL, content) + user1 = get_user(user1_email, realm) + user1_messages = message_stream_count(user1) + self.assertEqual(most_recent_message(user1).content, content) + # Welcome bot should still respond to initial PM after group PM. + self.send_message(user1_email, bot_email, Recipient.PERSONAL, content) + self.assertEqual(message_stream_count(user1), user1_messages+2)