diff --git a/app/services/notifications/reactions/send.rb b/app/services/notifications/reactions/send.rb index 66fbf9341..6ff3e6e5f 100644 --- a/app/services/notifications/reactions/send.rb +++ b/app/services/notifications/reactions/send.rb @@ -54,11 +54,9 @@ module Notifications json_data = reaction_json_data(recent_reaction, aggregated_reaction_siblings) - previous_siblings_size = 0 notification = Notification.find_or_initialize_by(notification_params) - old_json_data = notification.json_data - previous_siblings_size = old_json_data.dig("reaction", "aggregated_siblings")&.size if old_json_data + previous_siblings_size = notification.json_data&.dig("reaction", "aggregated_siblings")&.size || 0 notification.json_data = json_data notification.notified_at = Time.current diff --git a/spec/services/notifications/reactions/send_spec.rb b/spec/services/notifications/reactions/send_spec.rb index 7f0158058..f6270eec8 100644 --- a/spec/services/notifications/reactions/send_spec.rb +++ b/spec/services/notifications/reactions/send_spec.rb @@ -170,6 +170,23 @@ RSpec.describe Notifications::Reactions::Send, type: :service do end end + # regression test for #16627 and #16570 + context "when a found reaction has unexpected json data" do + let(:existing_notification) do + create( + :notification, + json_data: { user: {}, article: {} }, + action: "Reaction", + user: user, + ) + end + + it "does not raise error" do + reaction = create(:reaction, reactable: existing_notification.notifiable) + expect { described_class.call(reaction_data(reaction), user) }.not_to raise_error + end + end + context "when a receiver is an organization" do let(:organization) { create(:organization) }