From 355e3fb5296ffd433fb484ba7e183ddd97144c78 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 7 Sep 2023 15:38:11 +0200 Subject: [PATCH] Simplify `Account.by_recent_status` and `Account.by_recent_sign_in` scopes (#26840) --- app/models/account.rb | 4 ++-- .../api/v1/directories_controller_spec.rb | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index 244f3da83..40b9bba0c 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) } diff --git a/spec/controllers/api/v1/directories_controller_spec.rb b/spec/controllers/api/v1/directories_controller_spec.rb index 5e21802e7..dddbf40e4 100644 --- a/spec/controllers/api/v1/directories_controller_spec.rb +++ b/spec/controllers/api/v1/directories_controller_spec.rb @@ -55,7 +55,7 @@ describe Api::V1::DirectoriesController do 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 @@ describe Api::V1::DirectoriesController do 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 @@ describe Api::V1::DirectoriesController do 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