Fix notifications about the new child comments (#2233)

This commit is contained in:
Anna Buianova 2019-03-29 19:51:10 +03:00 committed by Ben Halpern
parent ece498caf8
commit 75a977172c
2 changed files with 33 additions and 7 deletions

View file

@ -51,8 +51,8 @@ class Notification < ApplicationRecord
def send_new_comment_notifications(comment)
return if comment.commentable_type == "PodcastEpisode"
user_ids = comment.ancestors.select(:receive_notifications, :user_id).select(&:receive_notifications).pluck(:user_id).to_set
user_ids.add(comment.commentable.user_id) if user_ids.empty? && comment.commentable.receive_notifications
user_ids = comment.ancestors.select(:user_id).where(receive_notifications: true).pluck(:user_id).to_set
user_ids.add(comment.commentable.user_id) if comment.commentable.receive_notifications
json_data = {
user: user_data(comment.user),
comment: comment_data(comment)

View file

@ -97,6 +97,13 @@ RSpec.describe Notification, type: :model do
expect(user2.notifications.count).to eq 0
end
it "sends a notification to the author of the article about the child comment" do
parent_comment = create(:comment, user: user2, commentable: article)
child_comment = create(:comment, user: user3, commentable: article, ancestry: parent_comment.id.to_s)
Notification.send_new_comment_notifications_without_delay(child_comment)
expect(user.notifications.count).to eq(1)
end
it "sends a notification to the organization" do
org = create(:organization)
user.update(organization: org)
@ -108,22 +115,41 @@ RSpec.describe Notification, type: :model do
end
context "when the author of the article is not subscribed" do
it "does not send a notification to the author of the article" do
let!(:comment) { create(:comment, user: user2, commentable: article) }
before do
article.update(receive_notifications: false)
comment = create(:comment, user: user2, commentable: article)
end
it "does not send a notification to the author of the article" do
Notification.send_new_comment_notifications_without_delay(comment)
expect(user.notifications.count).to eq 0
end
it "doesn't send a notification to the author of the article about the child comment" do
child_comment = create(:comment, user: user3, commentable: article, ancestry: comment.id.to_s)
Notification.send_new_comment_notifications_without_delay(child_comment)
expect(user.notifications.count).to eq 0
end
end
context "when the author of a comment is not subscribed" do
it "does not send a notification to the author of the comment" do
parent_comment = create(:comment, user: user2, commentable: article)
let(:parent_comment) { create(:comment, user: user2, commentable: article) }
let!(:child_comment) { create(:comment, user: user3, commentable: article, ancestry: parent_comment.id.to_s) }
before do
parent_comment.update(receive_notifications: false)
child_comment = create(:comment, user: user, commentable: article, ancestry: parent_comment.id.to_s)
end
it "does not send a notification to the author of the comment" do
Notification.send_new_comment_notifications_without_delay(child_comment)
expect(user2.notifications.count).to eq 0
end
it "sends a notification to the author of the article" do
Notification.send_new_comment_notifications_without_delay(child_comment)
expect(user.notifications.count).to eq(1)
end
end
end