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.

events: Handle device events for devices missing from initial state.

Prakhar Pratyush1d60c72172da
diff --git a/zerver/lib/events.py b/zerver/lib/events.py
index 4921ae73ce2..c8160edcaa3 100644
--- a/zerver/lib/events.py
+++ b/zerver/lib/events.py
@@ -2051,8 +2051,10 @@ def apply_event(
"push_registration_error_code": None,
}
elif event["op"] == "remove":
- del state["devices"][str(event["device_id"])]
+ state["devices"].pop(str(event["device_id"]), None)
elif event["op"] == "update":
+ if str(event["device_id"]) not in state["devices"]:
+ return
if "push_key_id" in event:
state["devices"][str(event["device_id"])]["push_key_id"] = event["push_key_id"]
if "push_token_id" in event:
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 8b556b24899..6755574b611 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -4359,6 +4359,30 @@ class NormalActionsTest(BaseAction):
self.client_post("/json/mobile_push/register", payload)
check_device_update("events[0]", events[0])
+ def test_apply_device_event_missing_from_state(self) -> None:
+ # A `device/remove` (or preceding `device/update`) event can
+ # arrive for a device_id that `get_devices` no longer returns,
+ # because the queue was created before the initial state was fetched.
+ # Test to verify `apply_events` handles such events.
+ state: dict[str, Any] = {"devices": {}}
+ events = [
+ {"type": "device", "op": "remove", "device_id": 999, "id": 0},
+ {"type": "device", "op": "update", "device_id": 999, "push_key_id": 1, "id": 1},
+ ]
+ apply_events(
+ self.user_profile,
+ state=state,
+ events=events,
+ fetch_event_types=None,
+ client_gravatar=True,
+ slim_presence=False,
+ include_subscribers=True,
+ linkifier_url_template=True,
+ user_list_incomplete=False,
+ include_deactivated_groups=False,
+ )
+ self.assertEqual(state["devices"], {})
+
def test_notify_realm_export_on_failure(self) -> None:
self.set_user_role(self.user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR)
self.login_user(self.user_profile)