From 759d7540a759575aa5fbe94a13616ea09fcce2f9 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Mon, 26 Nov 2018 12:34:38 -0500 Subject: [PATCH] Add guard to stop negative notifications (#1200) --- app/models/notification.rb | 4 +++- spec/models/notification_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 617fedcf5..f39956e1e 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -88,6 +88,7 @@ class Notification < ApplicationRecord def send_reaction_notification(notifiable) return if notifiable.user_id == notifiable.reactable.user_id + return if notifiable.points.negative? aggregated_reaction_siblings = notifiable.reactable.reactions. select{|r| r.user_id != notifiable.reactable.user_id}. map { |r| {category: r.category, created_at: r.created_at, user: user_data(r.user)} } @@ -106,7 +107,7 @@ class Notification < ApplicationRecord } } if aggregated_reaction_siblings.size.zero? - Notification.where(notifiable_type: notifiable.reactable.class.name, notifiable_id: notifiable.reactable.id, action: "Reaction").destroy_all + notification = Notification.where(notifiable_type: notifiable.reactable.class.name, notifiable_id: notifiable.reactable.id, action: "Reaction").destroy_all else previous_siblings_size = 0 notification = Notification.find_or_create_by(notifiable_type: notifiable.reactable.class.name, notifiable_id: notifiable.reactable.id, action: "Reaction") @@ -119,6 +120,7 @@ class Notification < ApplicationRecord end notification.save! end + notification end handle_asynchronously :send_reaction_notification diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 9180eeb2c..bb403f2cc 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -23,6 +23,29 @@ RSpec.describe Notification, type: :model do it "is given notifiable_at upon creation" do expect(Notification.last.notified_at).not_to eq nil end + + it "creates positive reaction notification" do + reaction = Reaction.create!( + user_id: user2.id, + reactable_id: article.id, + reactable_type: "Article", + category: "like", + ) + notification = Notification.send_reaction_notification_without_delay(reaction) + expect(notification).to be_valid + end + + it "does not create negative notification" do + user2.add_role(:trusted) + reaction = Reaction.create!( + user_id: user2.id, + reactable_id: article.id, + reactable_type: "Article", + category: "vomit", + ) + notification = Notification.send_reaction_notification_without_delay(reaction) + expect(notification).to eq nil + end end # describe "#send_to_followers" do