Clean watchers when losing access to board

https://app.fizzy.do/5986089/cards/2814
This commit is contained in:
Jorge Manrubia
2025-11-06 22:01:43 +01:00
parent 2ec4c297e2
commit 3a831e390f
6 changed files with 48 additions and 2 deletions
+5
View File
@@ -45,6 +45,7 @@ module Board::Accessible
mentions_for_user(user).destroy_all
notifications_for_user(user).destroy_all
watches_for(user).destroy_all
end
def watchers
@@ -89,4 +90,8 @@ module Board::Accessible
(notifications.source_type = 'Event' AND events.eventable_type = 'Comment' AND event_comment_cards.board_id = ?)",
id, id)
end
def watches_for(user)
Watch.where(card: cards, user: user)
end
end
@@ -0,0 +1,5 @@
class AddUserAndCardIndexToWatches < ActiveRecord::Migration[8.2]
def change
add_index :watches, %i[ user_id card_id ]
end
end
Generated
+2 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.2].define(version: 2025_11_05_122933) do
ActiveRecord::Schema[8.2].define(version: 2025_11_06_154151) do
create_table "accesses", force: :cascade do |t|
t.datetime "accessed_at"
t.integer "board_id", null: false
@@ -404,6 +404,7 @@ ActiveRecord::Schema[8.2].define(version: 2025_11_05_122933) do
t.integer "user_id", null: false
t.boolean "watching", default: true, null: false
t.index ["card_id"], name: "index_watches_on_card_id"
t.index ["user_id", "card_id"], name: "index_watches_on_user_id_and_card_id"
t.index ["user_id"], name: "index_watches_on_user_id"
end
+18 -1
View File
@@ -2760,6 +2760,23 @@ indexes:
nulls_not_distinct:
comment:
valid: true
- !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition
table: watches
name: index_watches_on_user_id_and_card_id
unique: false
columns:
- user_id
- card_id
lengths: {}
orders: {}
opclasses: {}
where:
type:
using:
include:
nulls_not_distinct:
comment:
valid: true
webhook_delinquency_trackers:
- !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition
table: webhook_delinquency_trackers
@@ -2843,4 +2860,4 @@ indexes:
nulls_not_distinct:
comment:
valid: true
version: 20251105122933
version: 20251106154151
+16
View File
@@ -64,4 +64,20 @@ class AccessTest < ActiveSupport::TestCase
assert_empty remaining_mentions
end
test "watches are destroyed when access is lost" do
kevin = users(:kevin)
board = boards(:writebook)
card = board.cards.first
assert card.watched_by?(kevin)
kevin_access = accesses(:writebook_kevin)
perform_enqueued_jobs only: Board::CleanInaccessibleDataJob do
kevin_access.destroy
end
assert_not card.watched_by?(kevin)
end
end
+2
View File
@@ -38,4 +38,6 @@ class Board::AccessibleTest < ActiveSupport::TestCase
boards(:writebook).access_for(users(:kevin)).access_only!
assert_not_includes boards(:writebook).reload.watchers, users(:kevin)
end
# NOTE: The tests for clearing inaccessible data are in +AccessTest+
end