Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message_send: Split get_active_presence_idle_user_ids in two.

Aman Agrawalccd6e6587297
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.py
index 7cbd5d88b35..2fd933b15ec 100644
--- a/zerver/actions/message_send.py
+++ b/zerver/actions/message_send.py
@@ -975,32 +975,28 @@ def filter_presence_idle_user_ids(user_ids: set[int]) -> list[int]:
return sorted(idle_user_ids)
-def get_active_presence_idle_user_ids(
+def get_notifiable_if_idle_user_ids(
realm: Realm,
sender_id: int,
user_notifications_data_list: list[UserMessageNotificationsData],
-) -> list[int]:
+) -> set[int]:
"""
- Given a list of active_user_ids, we build up a subset
- of those users who fit these criteria:
-
- * They are likely to receive push or email notifications.
- * They are no longer "present" according to the
- UserPresence table.
+ Returns the users who would get a push or email notification for
+ the message if they were idle.
"""
if realm.presence_disabled: # nocoverage
- return []
+ return set()
user_ids = set()
for user_notifications_data in user_notifications_data_list:
# We only need to know the presence idle state for a user if this message would be notifiable
- # for them if they were indeed idle. Only including those users in the calculation below is a
+ # for them if they were indeed idle. Only including those users in the presence query is a
# very important optimization for open communities with many inactive users.
if user_notifications_data.is_notifiable(sender_id, idle=True):
user_ids.add(user_notifications_data.user_id)
- return filter_presence_idle_user_ids(user_ids)
+ return user_ids
def get_message_url_and_link(
@@ -1333,10 +1329,12 @@ def do_send_messages(
for user_id in send_request.active_user_ids
]
- presence_idle_user_ids = get_active_presence_idle_user_ids(
- realm=send_request.realm,
- sender_id=sender.id,
- user_notifications_data_list=user_notifications_data_list,
+ presence_idle_user_ids = filter_presence_idle_user_ids(
+ get_notifiable_if_idle_user_ids(
+ realm=send_request.realm,
+ sender_id=sender.id,
+ user_notifications_data_list=user_notifications_data_list,
+ )
)
if send_request.recipients_for_user_creation_events is not None:
diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py
index 8066452ec53..30531e531c0 100644
--- a/zerver/tests/test_messages.py
+++ b/zerver/tests/test_messages.py
@@ -2,7 +2,10 @@ from datetime import timedelta
from django.utils.timezone import now as timezone_now
-from zerver.actions.message_send import get_active_presence_idle_user_ids
+from zerver.actions.message_send import (
+ filter_presence_idle_user_ids,
+ get_notifiable_if_idle_user_ids,
+)
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import Message, UserPresence, UserProfile
from zerver.models.recipients import (
@@ -25,14 +28,16 @@ class MissedMessageTest(ZulipTestCase):
hamlet_notifications_data = self.create_user_notifications_data_object(user_id=hamlet.id)
othello_notifications_data = self.create_user_notifications_data_object(user_id=othello.id)
- def assert_active_presence_idle_user_ids(user_ids: list[int]) -> None:
- presence_idle_user_ids = get_active_presence_idle_user_ids(
- realm=realm,
- sender_id=sender.id,
- user_notifications_data_list=[
- hamlet_notifications_data,
- othello_notifications_data,
- ],
+ def assert_presence_idle_user_ids(user_ids: list[int]) -> None:
+ presence_idle_user_ids = filter_presence_idle_user_ids(
+ get_notifiable_if_idle_user_ids(
+ realm=realm,
+ sender_id=sender.id,
+ user_notifications_data_list=[
+ hamlet_notifications_data,
+ othello_notifications_data,
+ ],
+ )
)
self.assertEqual(sorted(user_ids), sorted(presence_idle_user_ids))
@@ -47,33 +52,33 @@ class MissedMessageTest(ZulipTestCase):
hamlet_notifications_data.dm_push_notify = True
othello_notifications_data.dm_push_notify = True
- assert_active_presence_idle_user_ids([hamlet.id, othello.id])
+ assert_presence_idle_user_ids([hamlet.id, othello.id])
# We have already thoroughly tested the `is_notifiable` function elsewhere,
# so we needn't test all cases here. This test exists mainly to avoid a bug
- # which existed earlier with `get_active_presence_idle_user_ids` only looking
- # at `private_message` and the `mentioned` flag, not stream level notifications.
+ # which existed earlier, where we only looked at `private_message` and the
+ # `mentioned` flag, not stream level notifications.
# Simulate Hamlet has turned on notifications for the stream, and test that he's
# in the list.
hamlet_notifications_data.dm_push_notify = False
othello_notifications_data.dm_push_notify = False
hamlet_notifications_data.stream_email_notify = True
- assert_active_presence_idle_user_ids([hamlet.id])
+ assert_presence_idle_user_ids([hamlet.id])
# Hamlet is idle (and is supposed to receive stream notifications), so he should be in the list.
set_presence(hamlet, "iPhone", ago=5000)
- assert_active_presence_idle_user_ids([hamlet.id])
+ assert_presence_idle_user_ids([hamlet.id])
# If Hamlet is active, don't include him in the `presence_idle` list.
set_presence(hamlet, "website", ago=15)
- assert_active_presence_idle_user_ids([])
+ assert_presence_idle_user_ids([])
# Hamlet is active now, so only Othello should be in the list for a group
# direct message.
hamlet_notifications_data.stream_email_notify = False
hamlet_notifications_data.dm_push_notify = False
othello_notifications_data.dm_push_notify = True
- assert_active_presence_idle_user_ids([othello.id])
+ assert_presence_idle_user_ids([othello.id])
class TestBulkGetDirectMessageGroupUserIds(ZulipTestCase):