Simplify `Account.by_recent_status` and `Account.by_recent_sign_in` scopes (#26840)

pull/26842/head
Claire 2023-09-07 15:38:11 +02:00 committed by GitHub
parent b9e2eb5184
commit 355e3fb529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 10 deletions

View File

@ -127,8 +127,8 @@ class Account < ApplicationRecord
scope :searchable, -> { without_unapproved.without_suspended.where(moved_to_account_id: nil) } scope :searchable, -> { without_unapproved.without_suspended.where(moved_to_account_id: nil) }
scope :discoverable, -> { searchable.without_silenced.where(discoverable: true).left_outer_joins(:account_stat) } scope :discoverable, -> { searchable.without_silenced.where(discoverable: true).left_outer_joins(:account_stat) }
scope :followable_by, ->(account) { joins(arel_table.join(Follow.arel_table, Arel::Nodes::OuterJoin).on(arel_table[:id].eq(Follow.arel_table[:target_account_id]).and(Follow.arel_table[:account_id].eq(account.id))).join_sources).where(Follow.arel_table[:id].eq(nil)).joins(arel_table.join(FollowRequest.arel_table, Arel::Nodes::OuterJoin).on(arel_table[:id].eq(FollowRequest.arel_table[:target_account_id]).and(FollowRequest.arel_table[:account_id].eq(account.id))).join_sources).where(FollowRequest.arel_table[:id].eq(nil)) } scope :followable_by, ->(account) { joins(arel_table.join(Follow.arel_table, Arel::Nodes::OuterJoin).on(arel_table[:id].eq(Follow.arel_table[:target_account_id]).and(Follow.arel_table[:account_id].eq(account.id))).join_sources).where(Follow.arel_table[:id].eq(nil)).joins(arel_table.join(FollowRequest.arel_table, Arel::Nodes::OuterJoin).on(arel_table[:id].eq(FollowRequest.arel_table[:target_account_id]).and(FollowRequest.arel_table[:account_id].eq(account.id))).join_sources).where(FollowRequest.arel_table[:id].eq(nil)) }
scope :by_recent_status, -> { order(Arel.sql('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc, accounts.id desc')) } scope :by_recent_status, -> { order(Arel.sql('account_stats.last_status_at DESC NULLS LAST')) }
scope :by_recent_sign_in, -> { order(Arel.sql('(case when users.current_sign_in_at is null then 1 else 0 end) asc, users.current_sign_in_at desc, accounts.id desc')) } scope :by_recent_sign_in, -> { order(Arel.sql('users.current_sign_in_at DESC NULLS LAST')) }
scope :popular, -> { order('account_stats.followers_count desc') } scope :popular, -> { order('account_stats.followers_count desc') }
scope :by_domain_and_subdomains, ->(domain) { where(domain: Instance.by_domain_and_subdomains(domain).select(:domain)) } scope :by_domain_and_subdomains, ->(domain) { where(domain: Instance.by_domain_and_subdomains(domain).select(:domain)) }
scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) } scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) }

View File

@ -55,7 +55,7 @@ describe Api::V1::DirectoriesController do
Fabricate(:account_domain_block, account: user.account, domain: 'test.example') Fabricate(:account_domain_block, account: user.account, domain: 'test.example')
end end
it 'returns only the local discoverable account' do it 'returns the local discoverable account and the remote discoverable account' do
local_discoverable_account = Fabricate( local_discoverable_account = Fabricate(
:account, :account,
domain: nil, domain: nil,
@ -75,8 +75,7 @@ describe Api::V1::DirectoriesController do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(body_as_json.size).to eq(2) expect(body_as_json.size).to eq(2)
expect(body_as_json.first[:id]).to include(eligible_remote_account.id.to_s) expect(body_as_json.pluck(:id)).to contain_exactly(eligible_remote_account.id.to_s, local_discoverable_account.id.to_s)
expect(body_as_json.second[:id]).to include(local_discoverable_account.id.to_s)
end end
end end
@ -97,16 +96,15 @@ describe Api::V1::DirectoriesController do
context 'when ordered by active' do context 'when ordered by active' do
it 'returns accounts in order of most recent status activity' do it 'returns accounts in order of most recent status activity' do
status_old = Fabricate(:status) old_stat = Fabricate(:account_stat, last_status_at: 1.day.ago)
travel_to 10.seconds.from_now new_stat = Fabricate(:account_stat, last_status_at: 1.minute.ago)
status_new = Fabricate(:status)
get :show, params: { order: 'active' } get :show, params: { order: 'active' }
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(body_as_json.size).to eq(2) expect(body_as_json.size).to eq(2)
expect(body_as_json.first[:id]).to include(status_new.account.id.to_s) expect(body_as_json.first[:id]).to include(new_stat.account_id.to_s)
expect(body_as_json.second[:id]).to include(status_old.account.id.to_s) expect(body_as_json.second[:id]).to include(old_stat.account_id.to_s)
end end
end end