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.

push_notifications: Soft reactivate mentioned users.

Zixuan James Lib4feb673f1da
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
index 431f0d1bd10..aec31a8469d 100644
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -24,6 +24,7 @@ from zerver.lib.exceptions import JsonableError
from zerver.lib.message import access_message, huddle_users
from zerver.lib.outgoing_http import OutgoingSession
from zerver.lib.remote_server import send_json_to_push_bouncer, send_to_push_bouncer
+from zerver.lib.soft_deactivation import soft_reactivate_if_personal_notification
from zerver.lib.timestamp import datetime_to_timestamp
from zerver.models import (
AbstractPushDeviceToken,
@@ -1097,12 +1098,18 @@ def handle_push_notification(user_profile_id: int, missed_message: Dict[str, Any
trigger = missed_message["trigger"]
mentioned_user_group_name = None
+ # mentioned_user_group_id will be None if the user is personally mentioned
+ # regardless whether they are a member of the mentioned user group in the
+ # message or not.
mentioned_user_group_id = missed_message.get("mentioned_user_group_id")
if mentioned_user_group_id is not None:
user_group = UserGroup.objects.get(id=mentioned_user_group_id, realm=user_profile.realm)
mentioned_user_group_name = user_group.name
+ # Soft reactivate if pushing to a long_term_idle user that is personally mentioned
+ soft_reactivate_if_personal_notification(user_profile, {trigger}, mentioned_user_group_name)
+
apns_payload = get_message_payload_apns(
user_profile, message, trigger, mentioned_user_group_id, mentioned_user_group_name
)
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index 2db795f67fb..ae404bed8de 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -1483,6 +1483,58 @@ class HandlePushNotificationTest(PushNotificationTest):
mock_send_android.assert_called_with(user_identity, android_devices, {"gcm": True}, {})
mock_push_notifications.assert_called_once()
+ @mock.patch("zerver.lib.push_notifications.push_notifications_enabled", return_value=True)
+ def test_user_push_soft_reactivate_soft_deactivated_user(
+ self, mock_push_notifications: mock.MagicMock
+ ) -> None:
+ othello = self.example_user("othello")
+ cordelia = self.example_user("cordelia")
+ large_user_group = create_user_group(
+ "large_user_group", [self.user_profile, othello, cordelia], get_realm("zulip")
+ )
+
+ # Personal mention in a stream message should soft reactivate the user
+ with self.soft_deactivate_and_check_long_term_idle(self.user_profile, expected=False):
+ mention = f"@**{self.user_profile.full_name}**"
+ stream_mentioned_message_id = self.send_stream_message(othello, "Denmark", mention)
+ handle_push_notification(
+ self.user_profile.id,
+ {"message_id": stream_mentioned_message_id, "trigger": "mentioned"},
+ )
+
+ # Private message should soft reactivate the user
+ with self.soft_deactivate_and_check_long_term_idle(self.user_profile, expected=False):
+ # Soft reactivate the user by sending a personal message
+ personal_message_id = self.send_personal_message(othello, self.user_profile, "Message")
+ handle_push_notification(
+ self.user_profile.id,
+ {"message_id": personal_message_id, "trigger": "private_message"},
+ )
+
+ # Wild card mention should NOT soft reactivate the user
+ with self.soft_deactivate_and_check_long_term_idle(self.user_profile, expected=True):
+ # Soft reactivate the user by sending a personal message
+ mention = "@**all**"
+ stream_mentioned_message_id = self.send_stream_message(othello, "Denmark", mention)
+ handle_push_notification(
+ self.user_profile.id,
+ {"message_id": stream_mentioned_message_id, "trigger": "wildcard_mentioned"},
+ )
+
+ # Group mention should NOT soft reactivate the user
+ with self.soft_deactivate_and_check_long_term_idle(self.user_profile, expected=True):
+ # Soft reactivate the user by sending a personal message
+ mention = "@*large_user_group*"
+ stream_mentioned_message_id = self.send_stream_message(othello, "Denmark", mention)
+ handle_push_notification(
+ self.user_profile.id,
+ {
+ "message_id": stream_mentioned_message_id,
+ "trigger": "mentioned",
+ "mentioned_user_group_id": large_user_group.id,
+ },
+ )
+
@mock.patch("zerver.lib.push_notifications.logger.info")
@mock.patch("zerver.lib.push_notifications.push_notifications_enabled", return_value=True)
def test_user_push_notification_already_active(