From f9ef7d353276e5c108cec076fbb3326da3651abc Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 7 Sep 2023 14:18:17 +0200 Subject: [PATCH 1/2] Simplify `Account.by_recent_status` and `Account.by_recent_sign_in` scopes --- app/models/account.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index 244f3da83da66..40b9bba0cb642 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -127,8 +127,8 @@ class Account < ApplicationRecord 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 :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_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_status, -> { order(Arel.sql('account_stats.last_status_at DESC NULLS LAST')) } + 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 :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) } From a83454732dc90a1acf334c766478909cdeacbbe7 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 7 Sep 2023 14:58:37 +0200 Subject: [PATCH 2/2] Fix tests --- .../api/v1/directories_controller_spec.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/spec/controllers/api/v1/directories_controller_spec.rb b/spec/controllers/api/v1/directories_controller_spec.rb index 5e21802e7a3f4..dddbf40e4e2fa 100644 --- a/spec/controllers/api/v1/directories_controller_spec.rb +++ b/spec/controllers/api/v1/directories_controller_spec.rb @@ -55,7 +55,7 @@ Fabricate(:account_domain_block, account: user.account, domain: 'test.example') 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( :account, domain: nil, @@ -75,8 +75,7 @@ expect(response).to have_http_status(200) 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.second[:id]).to include(local_discoverable_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) end end @@ -97,16 +96,15 @@ context 'when ordered by active' do it 'returns accounts in order of most recent status activity' do - status_old = Fabricate(:status) - travel_to 10.seconds.from_now - status_new = Fabricate(:status) + old_stat = Fabricate(:account_stat, last_status_at: 1.day.ago) + new_stat = Fabricate(:account_stat, last_status_at: 1.minute.ago) get :show, params: { order: 'active' } expect(response).to have_http_status(200) 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.second[:id]).to include(status_old.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(old_stat.account_id.to_s) end end