diff --git a/app/jobs/collection/clean_inaccessible_data_job.rb b/app/jobs/collection/clean_inaccessible_data_job.rb new file mode 100644 index 000000000..16ff1b90f --- /dev/null +++ b/app/jobs/collection/clean_inaccessible_data_job.rb @@ -0,0 +1,5 @@ +class Collection::CleanInaccessibleDataJob < ApplicationJob + def perform(user, collection) + collection.clean_inaccessible_data_for(user) + end +end diff --git a/app/jobs/collection/clean_inaccessible_notifications_job.rb b/app/jobs/collection/clean_inaccessible_notifications_job.rb deleted file mode 100644 index e332e46ab..000000000 --- a/app/jobs/collection/clean_inaccessible_notifications_job.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Collection::CleanInaccessibleNotificationsJob < ApplicationJob - def perform(user, collection) - collection.clean_inaccessible_notifications_for(user) - end -end diff --git a/app/models/access.rb b/app/models/access.rb index 214e021f2..88303bd15 100644 --- a/app/models/access.rb +++ b/app/models/access.rb @@ -6,7 +6,7 @@ class Access < ApplicationRecord scope :ordered_by_recently_accessed, -> { order(accessed_at: :desc) } - after_destroy_commit :clean_inaccessible_notifications_later + after_destroy_commit :clean_inaccessible_data_later def accessed touch :accessed_at unless recently_accessed? @@ -17,7 +17,7 @@ class Access < ApplicationRecord accessed_at&.> 5.minutes.ago end - def clean_inaccessible_notifications_later - Collection::CleanInaccessibleNotificationsJob.perform_later(user, collection) + def clean_inaccessible_data_later + Collection::CleanInaccessibleDataJob.perform_later(user, collection) end end diff --git a/app/models/collection/accessible.rb b/app/models/collection/accessible.rb index 40c4a7c2d..264d03af8 100644 --- a/app/models/collection/accessible.rb +++ b/app/models/collection/accessible.rb @@ -40,18 +40,23 @@ module Collection::Accessible access_for(user).present? end - def clean_inaccessible_notifications_for(user) + def clean_inaccessible_data_for(user) return if accessible_to?(user) - user.notifications.find_each do |notification| - if notification.card&.collection == self - notification.destroy - end - end + clean_inaccessible_records user.notifications + clean_inaccessible_records user.mentions end private def grant_access_to_everyone accesses.grant_to(User.all) if all_access_previously_changed?(to: true) end + + def clean_inaccessible_records(records) + records.find_each do |record| + if record.card&.collection == self + record.destroy + end + end + end end diff --git a/test/models/access_test.rb b/test/models/access_test.rb index 6cbdc1ed5..1bd1af0ae 100644 --- a/test/models/access_test.rb +++ b/test/models/access_test.rb @@ -28,7 +28,7 @@ class AccessTest < ActiveSupport::TestCase kevin_access = accesses(:writebook_kevin) - perform_enqueued_jobs only: Collection::CleanInaccessibleNotificationsJob do + perform_enqueued_jobs only: Collection::CleanInaccessibleDataJob do kevin_access.destroy end @@ -38,4 +38,28 @@ class AccessTest < ActiveSupport::TestCase assert_empty remaining_notifications end + + test "mentions are destroyed when access is lost" do + david = users(:david) + collection = collections(:writebook) + + assert david.mentions.count > 0 + + mentions_to_be_destroyed = david.mentions.select do |mention| + mention.card&.collection == collection + end + assert mentions_to_be_destroyed.any? + + david_access = accesses(:writebook_david) + + perform_enqueued_jobs only: Collection::CleanInaccessibleDataJob do + david_access.destroy + end + + remaining_mentions = david.mentions.reload.select do |mention| + mention.card&.collection == collection + end + + assert_empty remaining_mentions + end end