Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
event_queue: Remove internal fields being leaked to the API.
Shanu7f196967ad2c
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 721127d3d56..e0fd0bbc3ec 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -705,50 +705,6 @@ paths:The user's [message flags][message-flags] for the message.items:type: string- email_notified:- type: boolean- deprecated: true- description: |- Whether the user receiving the event has already been- notified via email.-- **Deprecated**: This field is present only due to a bug and- will be removed from the API.- push_notified:- type: boolean- deprecated: true- description: |- Whether the user receiving the event has already been- notified via mobile notification.-- **Deprecated**: This field is present only due to a bug and- will be removed from the API.- stream_push_notify:- type: boolean- deprecated: true- description: |- Whether the user receiving the event has to be notified- via mobile notification for stream message.-- **Deprecated**: This field is present only due to a bug and- will be removed from the API.- stream_email_notify:- type: boolean- deprecated: true- description: |- Whether the user receiving the event has to be notified- via email for stream message.-- **Deprecated**: This field is present only due to a bug and- will be removed from the API.- wildcard_mention_notify:- type: boolean- deprecated: true- description: |- Whether the user has to be notified due to wildcard mention.-- **Deprecated**: This field is present only due to a bug and- will be removed from the API.additionalProperties: falseexample:{@@ -2820,13 +2776,6 @@ paths:type: stringdescription: |The ID of the registered queue.- handler_id:- type: integer- description: |- An internal field that should not be present in these API responses.-- **Deprecated**: This will be removed in a future release.- deprecated: trueexample:{"queue_id": "1375801870:2942",diff --git a/zerver/tests/test_event_queue.py b/zerver/tests/test_event_queue.pyindex 7004f4c705a..b3f31fd05c7 100644--- a/zerver/tests/test_event_queue.py+++ b/zerver/tests/test_event_queue.py@@ -798,6 +798,43 @@ class FileReloadLogicTest(ZulipTestCase):)+class PruneInternalDataTest(ZulipTestCase):+ def test_prune_internal_data(self) -> None:+ user_profile = self.example_user("hamlet")+ queue_data = dict(+ all_public_streams=True,+ apply_markdown=True,+ client_gravatar=True,+ client_type_name="website",+ event_types=["message"],+ last_connection_time=time.time(),+ queue_timeout=600,+ realm_id=user_profile.realm.id,+ user_profile_id=user_profile.id,+ )+ client = allocate_client_descriptor(queue_data)+ self.assertTrue(client.event_queue.empty())++ self.send_stream_message(+ self.example_user("iago"), "Denmark", content="@**King Hamlet** what's up?"+ )+ self.send_stream_message(+ self.example_user("iago"), "Denmark", content="@**all** what's up?"+ )+ self.send_personal_message(self.example_user("iago"), user_profile)++ events = client.event_queue.contents()+ self.assertEqual(len(events), 3)+ self.assertFalse("internal_data" in events[0])+ self.assertFalse("internal_data" in events[1])+ self.assertFalse("internal_data" in events[2])++ events = client.event_queue.contents(include_internal_data=True)+ self.assertTrue("internal_data" in events[0])+ self.assertTrue("internal_data" in events[1])+ self.assertTrue("internal_data" in events[2])++class EventQueueTest(ZulipTestCase):def get_client_descriptor(self) -> ClientDescriptor:hamlet = self.example_user("hamlet")diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.pyindex d0a2e6ae20b..ad4fe6e0136 100644--- a/zerver/tornado/event_queue.py+++ b/zerver/tornado/event_queue.py@@ -344,7 +344,7 @@ class EventQueue:self.newest_pruned_id = self.queue[0]["id"]self.pop()- def contents(self) -> List[Dict[str, Any]]:+ def contents(self, include_internal_data: bool = False) -> List[Dict[str, Any]]:contents: List[Dict[str, Any]] = []virtual_id_map: Dict[str, Dict[str, Any]] = {}for event_type in self.virtual_events:@@ -365,7 +365,21 @@ class EventQueue:self.virtual_events = {}self.queue = deque(contents)- return contents++ if include_internal_data:+ return contents+ return prune_internal_data(contents)+++def prune_internal_data(events: List[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """Prunes the internal_data data structures, which are not intended to+ be exposed to API clients.+ """+ events = copy.deepcopy(events)+ for event in events:+ if event["type"] == "message" and "internal_data" in event:+ del event["internal_data"]+ return events# maps queue ids to client descriptors@@ -630,7 +644,6 @@ def fetch_events(query: Mapping[str, Any]) -> Dict[str, Any]:if not client.event_queue.empty() or dont_block:response: Dict[str, Any] = dict(events=client.event_queue.contents(),- handler_id=handler_id,)if orig_queue_id is None:response["queue_id"] = queue_id@@ -702,7 +715,7 @@ def missedmessage_hook(if not last_for_client:return- for event in client.event_queue.contents():+ for event in client.event_queue.contents(include_internal_data=True):if event["type"] != "message":continueassert "flags" in event@@ -712,10 +725,10 @@ def missedmessage_hook(mentioned = "mentioned" in flags and "read" not in flagsprivate_message = event["message"]["type"] == "private"# stream_push_notify is set in process_message_event.- stream_push_notify = event.get("stream_push_notify", False)- stream_email_notify = event.get("stream_email_notify", False)+ stream_push_notify = event.get("internal_data", {}).get("stream_push_notify", False)+ stream_email_notify = event.get("internal_data", {}).get("stream_email_notify", False)wildcard_mention_notify = (- event.get("wildcard_mention_notify", False)+ event.get("internal_data", {}).get("wildcard_mention_notify", False)and "read" not in flagsand "wildcard_mentioned" in flags)@@ -732,8 +745,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("push_notified", False),- email_notified=event.get("email_notified", False),+ push_notified=event.get("internal_data", {}).get("push_notified", False),+ email_notified=event.get("internal_data", {}).get("email_notified", False),)maybe_enqueue_notifications(user_profile_id,@@ -937,7 +950,9 @@ def process_message_event()always_push_notify = user_data.get("always_push_notify", False)stream_name = event_template.get("stream_name")- result = maybe_enqueue_notifications(++ result: Dict[str, Any] = {}+ result["internal_data"] = maybe_enqueue_notifications(user_profile_id,message_id,private_message,@@ -950,9 +965,9 @@ def process_message_event(idle,{},)- result["stream_push_notify"] = stream_push_notify- result["stream_email_notify"] = stream_email_notify- result["wildcard_mention_notify"] = wildcard_mention_notify+ result["internal_data"]["stream_push_notify"] = stream_push_notify+ result["internal_data"]["stream_email_notify"] = stream_email_notify+ result["internal_data"]["wildcard_mention_notify"] = wildcard_mention_notifyextra_user_data[user_profile_id] = resultfor client_data in send_to_clients.values():