diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 39b2b5ab94..04ec3ef25d 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -5465,6 +5465,14 @@ def do_update_user_custom_profile_data(user_profile: UserProfile, field_value, created = CustomProfileFieldValue.objects.get_or_create( user_profile=user_profile, field_id=field['id']) + + if not created and field_value.value == str(field['value']): + # If the field value isn't actually being changed to a different one, + # and always_notify is disabled, we have nothing to do here for this field. + # Note: field_value.value is a TextField() so we need to cast field['value'] + # to a string for the comparison in this if. + continue + field_value.value = field['value'] if field_value.field.is_renderable(): field_value.rendered_value = render_stream_description(str(field['value'])) diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py index a1d1cc45ac..1c5097dd34 100644 --- a/zerver/tests/test_custom_profile_data.py +++ b/zerver/tests/test_custom_profile_data.py @@ -12,6 +12,8 @@ from zerver.models import CustomProfileField, \ from zerver.lib.external_accounts import DEFAULT_EXTERNAL_ACCOUNTS import ujson +import mock + class CustomProfileFieldTestCase(ZulipTestCase): def setUp(self) -> None: self.realm = get_realm("zulip") @@ -595,6 +597,23 @@ class UpdateCustomProfileFieldTest(CustomProfileFieldTestCase): self.assertIsNotNone(rendered_value) self.assertEqual("

beware of jealousy...

", rendered_value) + def test_do_update_value_not_changed(self) -> None: + iago = self.example_user("iago") + self.login(iago.email) + realm = get_realm("zulip") + + # Set field value: + field = CustomProfileField.objects.get(name="Mentor", realm=realm) + data = [{'id': field.id, + 'value': [self.example_user("aaron").id]}] # type: List[Dict[str, Union[int, str, List[int]]]] + do_update_user_custom_profile_data(iago, data) + + with mock.patch("zerver.lib.actions.notify_user_update_custom_profile_data") as mock_notify: + # Attempting to "update" the field value, when it wouldn't actually change, + # if always_notify is disabled, shouldn't trigger notify. + do_update_user_custom_profile_data(iago, data) + mock_notify.assert_not_called() + class ListCustomProfileFieldTest(CustomProfileFieldTestCase): def test_list(self) -> None: self.login(self.example_email("iago"))