mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
notifications: Simplify how triggers are passed around.
This removes the utterly unnecessary `triggers` dict (which always was a dict with exactly one value True) in favor of a single field, 'trigger'. Inspired by Kunal Gupta's work in #6659.
This commit is contained in:
@@ -388,14 +388,14 @@ def get_alert_from_message(message):
|
||||
Determine what alert string to display based on the missed messages.
|
||||
"""
|
||||
sender_str = message.sender.full_name
|
||||
if message.recipient.type == Recipient.HUDDLE and message.triggers['private_message']:
|
||||
if message.recipient.type == Recipient.HUDDLE and message.trigger == 'private_message':
|
||||
return "New private group message from %s" % (sender_str,)
|
||||
elif message.recipient.type == Recipient.PERSONAL and message.triggers['private_message']:
|
||||
elif message.recipient.type == Recipient.PERSONAL and message.trigger == 'private_message':
|
||||
return "New private message from %s" % (sender_str,)
|
||||
elif message.recipient.type == Recipient.STREAM and message.triggers['mentioned']:
|
||||
elif message.recipient.type == Recipient.STREAM and message.trigger == 'mentioned':
|
||||
return "New mention from %s" % (sender_str,)
|
||||
elif (message.recipient.type == Recipient.STREAM and
|
||||
(message.triggers['stream_push_notify'] and message.stream_name)):
|
||||
(message.trigger == 'stream_push_notify' and message.stream_name)):
|
||||
return "New stream message from %s in %s" % (sender_str, message.stream_name,)
|
||||
else:
|
||||
return "New Zulip mentions and private messages from %s" % (sender_str,)
|
||||
@@ -503,12 +503,7 @@ def handle_push_notification(user_profile_id, missed_message):
|
||||
umessage = UserMessage.objects.get(user_profile=user_profile,
|
||||
message__id=missed_message['message_id'])
|
||||
message = umessage.message
|
||||
triggers = missed_message['triggers']
|
||||
message.triggers = {
|
||||
'private_message': triggers['private_message'],
|
||||
'mentioned': triggers['mentioned'],
|
||||
'stream_push_notify': triggers['stream_push_notify'],
|
||||
}
|
||||
message.trigger = missed_message['trigger']
|
||||
message.stream_name = missed_message.get('stream_name', None)
|
||||
|
||||
if umessage.flags.read:
|
||||
|
||||
@@ -184,21 +184,13 @@ class EditMessageSideEffectsTest(ZulipTestCase):
|
||||
mobile_event = queue_messages[0]['event']
|
||||
|
||||
self.assertEqual(mobile_event['user_profile_id'], cordelia.id)
|
||||
self.assertEqual(mobile_event['triggers'], dict(
|
||||
stream_push_notify=False,
|
||||
private_message=False,
|
||||
mentioned=True,
|
||||
))
|
||||
self.assertEqual(mobile_event['trigger'], 'mentioned')
|
||||
|
||||
self.assertEqual(queue_messages[1]['queue_name'], 'missedmessage_emails')
|
||||
email_event = queue_messages[1]['event']
|
||||
|
||||
self.assertEqual(email_event['user_profile_id'], cordelia.id)
|
||||
self.assertEqual(email_event['triggers'], dict(
|
||||
stream_push_notify=False,
|
||||
private_message=False,
|
||||
mentioned=True,
|
||||
))
|
||||
self.assertEqual(email_event['trigger'], 'mentioned')
|
||||
|
||||
def test_second_mention_is_ignored(self):
|
||||
# type: () -> None
|
||||
|
||||
@@ -342,11 +342,7 @@ class HandlePushNotificationTest(PushNotificationTest):
|
||||
|
||||
missed_message = {
|
||||
'message_id': message.id,
|
||||
'triggers': {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
},
|
||||
'trigger': 'private_message',
|
||||
}
|
||||
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \
|
||||
mock.patch('zerver.lib.push_notifications.requests.request',
|
||||
@@ -401,11 +397,7 @@ class HandlePushNotificationTest(PushNotificationTest):
|
||||
missed_message = {
|
||||
'user_profile_id': self.user_profile.id,
|
||||
'message_id': message.id,
|
||||
'triggers': {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
},
|
||||
'trigger': 'private_message',
|
||||
}
|
||||
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \
|
||||
mock.patch('zerver.lib.push_notifications.requests.request',
|
||||
@@ -452,11 +444,7 @@ class HandlePushNotificationTest(PushNotificationTest):
|
||||
|
||||
missed_message = {
|
||||
'message_id': message.id,
|
||||
'triggers': {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
},
|
||||
'trigger': 'private_message',
|
||||
}
|
||||
apn.handle_push_notification(user_profile.id, missed_message)
|
||||
|
||||
@@ -471,11 +459,7 @@ class HandlePushNotificationTest(PushNotificationTest):
|
||||
|
||||
missed_message = {
|
||||
'message_id': message.id,
|
||||
'triggers': {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
},
|
||||
'trigger': 'private_message',
|
||||
}
|
||||
with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=True), \
|
||||
mock.patch('zerver.lib.push_notifications.get_apns_payload',
|
||||
@@ -514,11 +498,7 @@ class HandlePushNotificationTest(PushNotificationTest):
|
||||
|
||||
missed_message = {
|
||||
'message_id': message.id,
|
||||
'triggers': {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
},
|
||||
'trigger': 'private_message',
|
||||
}
|
||||
with mock.patch('zerver.lib.push_notifications.get_apns_payload',
|
||||
return_value={'apns': True}), \
|
||||
@@ -629,44 +609,28 @@ class TestGetAlertFromMessage(PushNotificationTest):
|
||||
def test_get_alert_from_private_group_message(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.HUDDLE)
|
||||
message.triggers = {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'private_message'
|
||||
alert = apn.get_alert_from_message(message)
|
||||
self.assertEqual(alert, "New private group message from King Hamlet")
|
||||
|
||||
def test_get_alert_from_private_message(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.PERSONAL)
|
||||
message.triggers = {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'private_message'
|
||||
alert = apn.get_alert_from_message(message)
|
||||
self.assertEqual(alert, "New private message from King Hamlet")
|
||||
|
||||
def test_get_alert_from_mention(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.STREAM)
|
||||
message.triggers = {
|
||||
'private_message': False,
|
||||
'mentioned': True,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'mentioned'
|
||||
alert = apn.get_alert_from_message(message)
|
||||
self.assertEqual(alert, "New mention from King Hamlet")
|
||||
|
||||
def test_get_alert_from_stream_message(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.STREAM)
|
||||
message.triggers = {
|
||||
'private_message': False,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': True,
|
||||
}
|
||||
message.trigger = 'stream_push_notify'
|
||||
message.stream_name = 'Denmark'
|
||||
alert = apn.get_alert_from_message(message)
|
||||
self.assertEqual(alert, "New stream message from King Hamlet in Denmark")
|
||||
@@ -674,11 +638,7 @@ class TestGetAlertFromMessage(PushNotificationTest):
|
||||
def test_get_alert_from_other_message(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(0)
|
||||
message.triggers = {
|
||||
'private_message': False,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'stream_push_notify'
|
||||
alert = apn.get_alert_from_message(message)
|
||||
alert = apn.get_alert_from_message(self.get_message(0))
|
||||
self.assertEqual(alert,
|
||||
@@ -689,11 +649,7 @@ class TestGetAPNsPayload(PushNotificationTest):
|
||||
def test_get_apns_payload(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.HUDDLE)
|
||||
message.triggers = {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'private_message'
|
||||
payload = apn.get_apns_payload(message)
|
||||
expected = {
|
||||
'alert': {
|
||||
@@ -717,11 +673,7 @@ class TestGetGCMPayload(PushNotificationTest):
|
||||
message.content = 'a' * 210
|
||||
message.rendered_content = 'a' * 210
|
||||
message.save()
|
||||
message.triggers = {
|
||||
'private_message': False,
|
||||
'mentioned': True,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'mentioned'
|
||||
|
||||
user_profile = self.example_user('hamlet')
|
||||
payload = apn.get_gcm_payload(user_profile, message)
|
||||
@@ -745,11 +697,7 @@ class TestGetGCMPayload(PushNotificationTest):
|
||||
def test_get_gcm_payload_personal(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.PERSONAL, 1)
|
||||
message.triggers = {
|
||||
'private_message': True,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': False,
|
||||
}
|
||||
message.trigger = 'private_message'
|
||||
user_profile = self.example_user('hamlet')
|
||||
payload = apn.get_gcm_payload(user_profile, message)
|
||||
expected = {
|
||||
@@ -770,11 +718,7 @@ class TestGetGCMPayload(PushNotificationTest):
|
||||
def test_get_gcm_payload_stream_notifications(self):
|
||||
# type: () -> None
|
||||
message = self.get_message(Recipient.STREAM, 1)
|
||||
message.triggers = {
|
||||
'private_message': False,
|
||||
'mentioned': False,
|
||||
'stream_push_notify': True,
|
||||
}
|
||||
message.trigger = 'stream_push_notify'
|
||||
message.stream_name = 'Denmark'
|
||||
user_profile = self.example_user('hamlet')
|
||||
payload = apn.get_gcm_payload(user_profile, message)
|
||||
|
||||
@@ -702,11 +702,14 @@ def maybe_enqueue_notifications(user_profile_id, message_id, private_message,
|
||||
|
||||
if (idle or always_push_notify) and (private_message or mentioned or stream_push_notify):
|
||||
notice = build_offline_notification(user_profile_id, message_id)
|
||||
notice['triggers'] = {
|
||||
'private_message': private_message,
|
||||
'mentioned': mentioned,
|
||||
'stream_push_notify': stream_push_notify,
|
||||
}
|
||||
if private_message:
|
||||
notice['trigger'] = 'private_message'
|
||||
elif mentioned:
|
||||
notice['trigger'] = 'mentioned'
|
||||
elif stream_push_notify:
|
||||
notice['trigger'] = 'stream_push_notify'
|
||||
else:
|
||||
raise AssertionError("Unknown notification trigger!")
|
||||
notice['stream_name'] = stream_name
|
||||
if not already_notified.get("push_notified"):
|
||||
queue_json_publish("missedmessage_mobile_notifications", notice, lambda notice: None)
|
||||
|
||||
Reference in New Issue
Block a user