diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb index 964d4e279a1..0b518036b10 100644 --- a/app/models/follow_request.rb +++ b/app/models/follow_request.rb @@ -37,7 +37,7 @@ class FollowRequest < ApplicationRecord if account.local? ListAccount.where(follow_request: self).update_all(follow_request_id: nil, follow_id: follow.id) MergeWorker.perform_async(target_account.id, account.id, 'home') - MergeWorker.push_bulk(List.where(account: account).joins(:list_accounts).where(list_accounts: { account_id: target_account.id }).pluck(:id)) do |list_id| + MergeWorker.push_bulk(account.owned_lists.with_list_account(target_account).pluck(:id)) do |list_id| [target_account.id, list_id, 'list'] end end diff --git a/app/models/list.rb b/app/models/list.rb index 76c116ce244..8fd1953ab31 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -32,6 +32,8 @@ class List < ApplicationRecord before_destroy :clean_feed_manager + scope :with_list_account, ->(account) { joins(:list_accounts).where(list_accounts: { account: }) } + private def validate_account_lists_limit diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index 2928c85390a..5ff1b63503c 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -82,7 +82,7 @@ class FollowService < BaseService LocalNotificationWorker.perform_async(@target_account.id, follow.id, follow.class.name, 'follow') MergeWorker.perform_async(@target_account.id, @source_account.id, 'home') - MergeWorker.push_bulk(List.where(account: @source_account).joins(:list_accounts).where(list_accounts: { account_id: @target_account.id }).pluck(:id)) do |list_id| + MergeWorker.push_bulk(@source_account.owned_lists.with_list_account(@target_account).pluck(:id)) do |list_id| [@target_account.id, list_id, 'list'] end diff --git a/app/services/unfollow_service.rb b/app/services/unfollow_service.rb index b3f2cd66f67..1ea8af69926 100644 --- a/app/services/unfollow_service.rb +++ b/app/services/unfollow_service.rb @@ -34,7 +34,7 @@ class UnfollowService < BaseService unless @options[:skip_unmerge] UnmergeWorker.perform_async(@target_account.id, @source_account.id, 'home') - UnmergeWorker.push_bulk(List.where(account: @source_account).joins(:list_accounts).where(list_accounts: { account_id: @target_account.id }).pluck(:list_id)) do |list_id| + UnmergeWorker.push_bulk(@source_account.owned_lists.with_list_account(@target_account).pluck(:list_id)) do |list_id| [@target_account.id, list_id, 'list'] end end diff --git a/app/services/unmute_service.rb b/app/services/unmute_service.rb index 9262961f7d7..0a9604bae2e 100644 --- a/app/services/unmute_service.rb +++ b/app/services/unmute_service.rb @@ -9,7 +9,7 @@ class UnmuteService < BaseService if account.following?(target_account) MergeWorker.perform_async(target_account.id, account.id, 'home') - MergeWorker.push_bulk(List.where(account: account).joins(:list_accounts).where(list_accounts: { account_id: target_account.id }).pluck(:id)) do |list_id| + MergeWorker.push_bulk(account.owned_lists.with_list_account(target_account).pluck(:id)) do |list_id| [target_account.id, list_id, 'list'] end end diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index 48c273d3ecb..e2d91835ec7 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -28,4 +28,26 @@ RSpec.describe List do end end end + + describe 'Scopes' do + describe '.with_list_account' do + let(:alice) { Fabricate :account } + let(:bob) { Fabricate :account } + let(:list) { Fabricate :list } + let(:other_list) { Fabricate :list } + + before do + Fabricate :follow, account: list.account, target_account: alice + Fabricate :follow, account: other_list.account, target_account: bob + Fabricate :list_account, list: list, account: alice + Fabricate :list_account, list: other_list, account: bob + end + + it 'returns lists connected to the account' do + expect(described_class.with_list_account(alice)) + .to include(list) + .and not_include(other_list) + end + end + end end