Deleting hidden comment notification using after_update (#9823)

* Deleting comment notification using after_update

* Using conditional callback to delete notification

* Adding tests for notification deletion
This commit is contained in:
Rafi 2020-08-31 14:31:25 +05:30 committed by GitHub
parent 5e67beaaa4
commit 84012d400d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View file

@ -206,10 +206,6 @@ class CommentsController < ApplicationController
@comment.hidden_by_commentable_user = true
@comment&.commentable&.update_column(:any_comments_hidden, true)
Notification.destroy_by(user_id: current_user.id,
notifiable_type: "Comment",
notifiable_id: params[:comment_id])
if @comment.save
render json: { hidden: "true" }, status: :ok
else

View file

@ -28,8 +28,8 @@ class Comment < ApplicationRecord
before_create :adjust_comment_parent_based_on_depth
after_create :after_create_checks
after_create :notify_slack_channel_about_warned_users
after_update :remove_notifications, if: :deleted
after_update :update_descendant_notifications, if: :deleted
after_update :remove_notifications, if: :remove_notifications?
before_destroy :before_destroy_actions
after_destroy :after_destroy_actions
@ -134,6 +134,10 @@ class Comment < ApplicationRecord
private
def remove_notifications?
deleted? || hidden_by_commentable_user?
end
def update_notifications
Notification.update_notifications(self)
end

View file

@ -417,6 +417,14 @@ RSpec.describe Comment, type: :model do
expect(comment.notifications).to be_empty
end
it "deletes the comment's notifications when hidden_by_commentable_user is set to true" do
create(:notification, notifiable: comment, user: user)
sidekiq_perform_enqueued_jobs do
comment.update(hidden_by_commentable_user: true)
end
expect(comment.notifications).to be_empty
end
it "updates the notifications of the descendants with [deleted]" do
comment = create(:comment, commentable: article)
child_comment = create(:comment, parent: comment, commentable: article, user: user)