Generalize and delete mentions too

This commit is contained in:
Jorge Manrubia
2025-07-04 19:08:29 +02:00
parent cbeefc5b74
commit 3fb3e5b48e
5 changed files with 44 additions and 15 deletions
@@ -0,0 +1,5 @@
class Collection::CleanInaccessibleDataJob < ApplicationJob
def perform(user, collection)
collection.clean_inaccessible_data_for(user)
end
end
@@ -1,5 +0,0 @@
class Collection::CleanInaccessibleNotificationsJob < ApplicationJob
def perform(user, collection)
collection.clean_inaccessible_notifications_for(user)
end
end
+3 -3
View File
@@ -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
+11 -6
View File
@@ -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
+25 -1
View File
@@ -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