diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py index 5f2a7182c6..e32dfe12c3 100644 --- a/zerver/lib/event_schema.py +++ b/zerver/lib/event_schema.py @@ -1068,7 +1068,7 @@ check_realm_user_remove = make_checker(realm_user_remove_event) custom_profile_field_type = DictType( required_keys=[ ("id", int), - ("value", str), + ("value", OptionalType(str)), ], optional_keys=[ ("rendered_value", str), diff --git a/zerver/lib/events.py b/zerver/lib/events.py index d333cea497..b93a9e66a6 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -956,15 +956,17 @@ def apply_event( p["is_billing_admin"] = person["is_billing_admin"] if "custom_profile_field" in person: - custom_field_id = person["custom_profile_field"]["id"] + custom_field_id = str(person["custom_profile_field"]["id"]) custom_field_new_value = person["custom_profile_field"]["value"] - if "rendered_value" in person["custom_profile_field"]: - p["profile_data"][str(custom_field_id)] = { + if custom_field_new_value is None and "profile_data" in p: + p["profile_data"].pop(custom_field_id, None) + elif "rendered_value" in person["custom_profile_field"]: + p["profile_data"][custom_field_id] = { "value": custom_field_new_value, "rendered_value": person["custom_profile_field"]["rendered_value"], } else: - p["profile_data"][str(custom_field_id)] = { + p["profile_data"][custom_field_id] = { "value": custom_field_new_value, } diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 42a6d190de..19a85c8aff 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -24,6 +24,7 @@ from zerver.actions.bots import ( ) from zerver.actions.create_user import do_create_user, do_reactivate_user from zerver.actions.custom_profile_fields import ( + check_remove_custom_profile_field_value, do_remove_realm_custom_profile_field, do_update_user_custom_profile_data_if_changed, try_add_realm_custom_profile_field, @@ -1126,6 +1127,13 @@ class NormalActionsTest(BaseAction): check_realm_user_update("events[0]", events[0], "custom_profile_field") self.assertEqual(events[0]["person"]["custom_profile_field"].keys(), {"id", "value"}) + # Test event for removing custom profile data + events = self.verify_action( + lambda: check_remove_custom_profile_field_value(self.user_profile, field_id) + ) + check_realm_user_update("events[0]", events[0], "custom_profile_field") + self.assertEqual(events[0]["person"]["custom_profile_field"].keys(), {"id", "value"}) + def test_presence_events(self) -> None: events = self.verify_action( lambda: do_update_user_presence(