Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
custom_profile_fields: Send event on dropdown choice deletion.
Sairam Bisoyic8caec322b05
diff --git a/zerver/actions/custom_profile_fields.py b/zerver/actions/custom_profile_fields.pyindex 3366a270011..81064559c05 100644--- a/zerver/actions/custom_profile_fields.py+++ b/zerver/actions/custom_profile_fields.py@@ -107,8 +107,27 @@ def remove_custom_profile_field_value_if_required(new_values = set(field_data.keys())removed_values = old_values - new_values- if removed_values:- CustomProfileFieldValue.objects.filter(field=field, value__in=removed_values).delete()+ if not removed_values:+ return++ values_to_delete = CustomProfileFieldValue.objects.filter(+ field=field, value__in=removed_values+ ).select_related("user_profile")++ updated_users = [field_value.user_profile for field_value in values_to_delete]++ values_to_delete.delete()++ for user_profile in updated_users:+ notify_user_update_custom_profile_data(+ user_profile,+ {+ "id": field.id,+ "value": None,+ "rendered_value": None,+ "type": field.field_type,+ },+ )@transaction.atomic(durable=True)diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 6755574b611..2ae5c3ceb1b 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -1756,6 +1756,40 @@ class NormalActionsTest(BaseAction):do_remove_realm_custom_profile_field(realm, field)check_custom_profile_fields("events[0]", events[0])+ # Test that deleting a dropdown choice correctly dispatches a realm_user+ # update event to clear the profile data for users who had that choice selected.+ hamlet = self.example_user("hamlet")++ field = CustomProfileField.objects.get(name="Favorite editor", realm=hamlet.realm)+ existing_choices = orjson.loads(field.field_data)+ self.assertEqual(set(existing_choices.keys()), {"0", "1"})++ custom_field_value: ProfileDataElementUpdateDict = {+ "id": field.id,+ "value": "0",+ }+ self.set_user_custom_profile_data(hamlet, [custom_field_value])++ # Remove the option with value "0"+ new_choices: dict[str, Any] = {"1": {"text": "Emacs", "order": "1"}}++ with self.verify_action(num_events=2) as events:+ try_update_realm_custom_profile_field(+ realm=self.user_profile.realm,+ field=field,+ name=field.name,+ field_data=new_choices,+ )++ check_realm_user_update("events[0]", events[0], "custom_profile_field")+ check_custom_profile_fields("events[1]", events[1])++ self.assertEqual(events[0]["person"]["user_id"], hamlet.id)++ custom_field_payload = events[0]["person"]["custom_profile_field"]+ self.assertEqual(custom_field_payload["id"], field.id)+ self.assertEqual(custom_field_payload["value"], None)+def test_pronouns_type_support_in_custom_profile_fields_events(self) -> None:realm = self.user_profile.realmfield = CustomProfileField.objects.get(realm=realm, name="Pronouns")