From d1cda29fb4a3d57c232d00f23ee8d2ccf08b71e5 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 20 Nov 2017 19:35:26 -0800 Subject: [PATCH] API: Add stream email notification setting plumbing. This is all the plumbing that makes it possible to enable the stream_email_notifications setting via the Zulip API. The flag still doesn't do anything yet, but this is a nice checkpoint along the way to implementing this feature. --- zerver/lib/actions.py | 24 ++++++++++++++++++++++-- zerver/tests/test_events.py | 1 + zerver/tests/test_subs.py | 10 ++++++++++ zerver/views/streams.py | 1 + 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index b623f26877..f2f0d559a7 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -917,6 +917,7 @@ RecipientInfoResult = TypedDict('RecipientInfoResult', { 'active_user_ids': Set[int], 'push_notify_user_ids': Set[int], 'stream_push_user_ids': Set[int], + 'stream_email_user_ids': Set[int], 'um_eligible_user_ids': Set[int], 'long_term_idle_user_ids': Set[int], 'default_bot_user_ids': Set[int], @@ -928,6 +929,7 @@ def get_recipient_info(recipient: Recipient, stream_topic: Optional[StreamTopicTarget], possibly_mentioned_user_ids: Optional[Set[int]]=None) -> RecipientInfoResult: stream_push_user_ids = set() # type: Set[int] + stream_email_user_ids = set() # type: Set[int] if recipient.type == Recipient.PERSONAL: # The sender and recipient may be the same id, so @@ -944,6 +946,7 @@ def get_recipient_info(recipient: Recipient, subscription_rows = stream_topic.get_active_subscriptions().values( 'user_profile_id', 'push_notifications', + 'email_notifications', 'in_home_view', ).order_by('user_profile_id') @@ -952,12 +955,21 @@ def get_recipient_info(recipient: Recipient, for row in subscription_rows ] + user_ids_muting_topic = stream_topic.user_ids_muting_topic() + stream_push_user_ids = { row['user_profile_id'] for row in subscription_rows # Note: muting a stream overrides stream_push_notify if row['push_notifications'] and row['in_home_view'] - } - stream_topic.user_ids_muting_topic() + } - user_ids_muting_topic + + stream_email_user_ids = { + row['user_profile_id'] + for row in subscription_rows + # Note: muting a stream overrides stream_email_notify + if row['email_notifications'] and row['in_home_view'] + } - user_ids_muting_topic elif recipient.type == Recipient.HUDDLE: message_to_user_ids = get_huddle_user_ids(recipient) @@ -1056,6 +1068,7 @@ def get_recipient_info(recipient: Recipient, active_user_ids=active_user_ids, push_notify_user_ids=push_notify_user_ids, stream_push_user_ids=stream_push_user_ids, + stream_email_user_ids=stream_email_user_ids, um_eligible_user_ids=um_eligible_user_ids, long_term_idle_user_ids=long_term_idle_user_ids, default_bot_user_ids=default_bot_user_ids, @@ -1193,6 +1206,7 @@ def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str, message['active_user_ids'] = info['active_user_ids'] message['push_notify_user_ids'] = info['push_notify_user_ids'] message['stream_push_user_ids'] = info['stream_push_user_ids'] + message['stream_email_user_ids'] = info['stream_email_user_ids'] message['um_eligible_user_ids'] = info['um_eligible_user_ids'] message['long_term_idle_user_ids'] = info['long_term_idle_user_ids'] message['default_bot_user_ids'] = info['default_bot_user_ids'] @@ -1316,6 +1330,7 @@ def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str, flags=user_flags.get(user_id, []), always_push_notify=(user_id in message['push_notify_user_ids']), stream_push_notify=(user_id in message['stream_push_user_ids']), + stream_email_notify=(user_id in message['stream_email_user_ids']), ) for user_id in user_ids ] @@ -2387,6 +2402,7 @@ def notify_subscriptions_added(user_profile: UserProfile, desktop_notifications=subscription.desktop_notifications, audible_notifications=subscription.audible_notifications, push_notifications=subscription.push_notifications, + email_notifications=subscription.email_notifications, description=stream.description, pin_to_top=subscription.pin_to_top, is_old_stream=is_old_stream(stream.date_created), @@ -2494,6 +2510,7 @@ def bulk_add_subscriptions(streams: Iterable[Stream], desktop_notifications=user_profile.enable_stream_desktop_notifications, audible_notifications=user_profile.enable_stream_sounds, push_notifications=user_profile.enable_stream_push_notifications, + email_notifications=user_profile.enable_stream_email_notifications, ) subs_by_user[user_profile.id].append(sub_to_add) subs_to_add.append((sub_to_add, stream)) @@ -3811,6 +3828,7 @@ def do_update_message(user_profile: UserProfile, message: Message, topic_name: O event['push_notify_user_ids'] = list(info['push_notify_user_ids']) event['stream_push_user_ids'] = list(info['stream_push_user_ids']) + event['stream_email_user_ids'] = list(info['stream_email_user_ids']) event['prior_mention_user_ids'] = list(prior_mention_user_ids) event['mention_user_ids'] = list(mention_user_ids) event['presence_idle_user_ids'] = filter_presence_idle_user_ids(info['active_user_ids']) @@ -4033,7 +4051,8 @@ def gather_subscriptions_helper(user_profile: UserProfile, include_subscribers: bool=True) -> SubHelperT: sub_dicts = get_stream_subscriptions_for_user(user_profile).values( "recipient_id", "in_home_view", "color", "desktop_notifications", - "audible_notifications", "push_notifications", "active", "pin_to_top" + "audible_notifications", "push_notifications", "email_notifications", + "active", "pin_to_top" ).order_by("recipient_id") sub_dicts = list(sub_dicts) @@ -4113,6 +4132,7 @@ def gather_subscriptions_helper(user_profile: UserProfile, 'desktop_notifications': sub["desktop_notifications"], 'audible_notifications': sub["audible_notifications"], 'push_notifications': sub["push_notifications"], + 'email_notifications': sub["email_notifications"], 'pin_to_top': sub["pin_to_top"], 'stream_id': stream["id"], 'description': stream["description"], diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index ac26afbb8b..e5f8936c46 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -674,6 +674,7 @@ class EventsRegisterTest(ZulipTestCase): ('mention_user_ids', check_list(check_int)), ('presence_idle_user_ids', check_list(check_int)), ('stream_push_user_ids', check_list(check_int)), + ('stream_email_user_ids', check_list(check_int)), ('push_notify_user_ids', check_list(check_int)), ('orig_content', check_string), ('orig_rendered_content', check_string), diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 9d621c8a2d..a336c3f882 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -1434,6 +1434,14 @@ class SubscriptionPropertiesTest(ZulipTestCase): self.assert_json_error(result, '%s is not a boolean' % (property_name,)) + property_name = "email_notifications" + result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties", + {"subscription_data": ujson.dumps([{"property": property_name, + "value": "bad", + "stream_id": subs[0]["stream_id"]}])}) + self.assert_json_error(result, + '%s is not a boolean' % (property_name,)) + property_name = "color" result = self.api_post(test_email, "/api/v1/users/me/subscriptions/properties", {"subscription_data": ujson.dumps([{"property": property_name, @@ -2537,6 +2545,7 @@ class SubscriptionAPITest(ZulipTestCase): user_profile.enable_stream_desktop_notifications = True user_profile.enable_stream_push_notifications = True user_profile.enable_stream_sounds = True + user_profile.enable_stream_email_notifications = True user_profile.save() current_stream = self.get_streams(invitee_email, invitee_realm)[0] invite_streams = self.make_random_stream_names([current_stream]) @@ -2551,6 +2560,7 @@ class SubscriptionAPITest(ZulipTestCase): self.assertTrue(subscription.desktop_notifications) self.assertTrue(subscription.push_notifications) self.assertTrue(subscription.audible_notifications) + self.assertTrue(subscription.email_notifications) def test_subscriptions_add_notification_default_false(self) -> None: """ diff --git a/zerver/views/streams.py b/zerver/views/streams.py index d8afd1236a..09baaba567 100644 --- a/zerver/views/streams.py +++ b/zerver/views/streams.py @@ -503,6 +503,7 @@ def update_subscription_properties_backend( "desktop_notifications": check_bool, "audible_notifications": check_bool, "push_notifications": check_bool, + "email_notifications": check_bool, "pin_to_top": check_bool} response_data = []