From 84012d400dc501a7ad70327f0cc882f17e8c73f3 Mon Sep 17 00:00:00 2001 From: Rafi Date: Mon, 31 Aug 2020 14:31:25 +0530 Subject: [PATCH] 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 --- app/controllers/comments_controller.rb | 4 ---- app/models/comment.rb | 6 +++++- spec/models/comment_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 11c6d4355..fa560d953 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 5530d5647..23f07f38d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 63e626f1a..4cabe05dc 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -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)