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.

event_queue: Extract local variables.

Abhijeet Prasad Bodasc6a31dcd9f6d
diff --git a/zerver/tests/test_event_queue.py b/zerver/tests/test_event_queue.py
index f88bd13f51d..91f8bf1d4dc 100644
--- a/zerver/tests/test_event_queue.py
+++ b/zerver/tests/test_event_queue.py
@@ -200,6 +200,8 @@ class MissedMessageNotificationsTest(ZulipTestCase):
user_profile.enable_online_push_notifications = False
user_profile.save()
+ iago = self.example_user("iago")
+
# Fetch the Denmark stream for testing
stream = get_stream("Denmark", user_profile.realm)
sub = Subscription.objects.get(
@@ -268,7 +270,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
client_descriptor = allocate_event_queue()
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
# To test the missed_message hook, we first need to send a message
- msg_id = self.send_stream_message(self.example_user("iago"), "Denmark")
+ msg_id = self.send_stream_message(iago, "Denmark")
# Verify that nothing happens if you call it as not the
# "last client descriptor", in which case the function
@@ -294,7 +296,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
# Test the hook with a private message; this should trigger notifications
client_descriptor = allocate_event_queue()
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_personal_message(self.example_user("iago"), user_profile)
+ msg_id = self.send_personal_message(iago, user_profile)
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -333,9 +335,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
# Test the hook with a wildcard mention; this should trigger notifications
client_descriptor = allocate_event_queue()
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="@**all** what's up?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="@**all** what's up?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -379,9 +379,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
change_subscription_properties(user_profile, stream, sub, {"is_muted": True})
client_descriptor = allocate_event_queue()
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="@**all** what's up?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="@**all** what's up?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -402,9 +400,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
user_profile.save()
client_descriptor = allocate_event_queue()
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="@**all** what's up?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="@**all** what's up?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -429,9 +425,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
sub.save()
client_descriptor = allocate_event_queue()
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="@**all** what's up?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="@**all** what's up?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -455,9 +449,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
change_subscription_properties(user_profile, stream, sub, {"push_notifications": True})
client_descriptor = allocate_event_queue()
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="what's up everyone?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="what's up everyone?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -480,9 +472,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
user_profile, stream, sub, {"push_notifications": False, "email_notifications": True}
)
self.assertTrue(client_descriptor.event_queue.empty())
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="what's up everyone?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="what's up everyone?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
@@ -509,7 +499,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
self.assertTrue(client_descriptor.event_queue.empty())
do_mute_topic(user_profile, stream, "mutingtest")
msg_id = self.send_stream_message(
- self.example_user("iago"),
+ iago,
"Denmark",
content="what's up everyone?",
topic_name="mutingtest",
@@ -537,9 +527,7 @@ class MissedMessageNotificationsTest(ZulipTestCase):
self.assertTrue(client_descriptor.event_queue.empty())
change_subscription_properties(user_profile, stream, sub, {"is_muted": True})
- msg_id = self.send_stream_message(
- self.example_user("iago"), "Denmark", content="what's up everyone?"
- )
+ msg_id = self.send_stream_message(iago, "Denmark", content="what's up everyone?")
with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:
missedmessage_hook(user_profile.id, client_descriptor, True)
mock_enqueue.assert_called_once()
diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py
index 2e15c4803ef..59474e0c177 100644
--- a/zerver/tornado/event_queue.py
+++ b/zerver/tornado/event_queue.py
@@ -733,6 +733,9 @@ def missedmessage_hook(
for event in client.event_queue.contents(include_internal_data=True):
if event["type"] != "message":
continue
+
+ internal_data = event.get("internal_data", {})
+
assert "flags" in event
flags = event["flags"]
@@ -740,10 +743,10 @@ def missedmessage_hook(
mentioned = "mentioned" in flags and "read" not in flags
private_message = event["message"]["type"] == "private"
# stream_push_notify is set in process_message_event.
- stream_push_notify = event.get("internal_data", {}).get("stream_push_notify", False)
- stream_email_notify = event.get("internal_data", {}).get("stream_email_notify", False)
+ stream_push_notify = internal_data.get("stream_push_notify", False)
+ stream_email_notify = internal_data.get("stream_email_notify", False)
wildcard_mention_notify = (
- event.get("internal_data", {}).get("wildcard_mention_notify", False)
+ internal_data.get("wildcard_mention_notify", False)
and "read" not in flags
and "wildcard_mentioned" in flags
)
@@ -760,8 +763,8 @@ def missedmessage_hook(
message_id = event["message"]["id"]
# Pass on the information on whether a push or email notification was already sent.
already_notified = dict(
- push_notified=event.get("internal_data", {}).get("push_notified", False),
- email_notified=event.get("internal_data", {}).get("email_notified", False),
+ push_notified=internal_data.get("push_notified", False),
+ email_notified=internal_data.get("email_notified", False),
)
maybe_enqueue_notifications(
user_profile_id,