Add guard to stop negative notifications (#1200)

This commit is contained in:
Ben Halpern 2018-11-26 12:34:38 -05:00 committed by GitHub
parent 0df793a6fe
commit 759d7540a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -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

View file

@ -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