Skip to content

Commit

Permalink
Move followable by logic to suggestion class (mastodon#28710)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Jan 12, 2024
1 parent 608f66f commit a90c134
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
1 change: 0 additions & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class Account < ApplicationRecord
scope :without_unapproved, -> { left_outer_joins(:user).merge(User.approved.confirmed).or(remote) }
scope :searchable, -> { without_unapproved.without_suspended.where(moved_to_account_id: nil) }
scope :discoverable, -> { searchable.without_silenced.where(discoverable: true).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, -> { includes(:account_stat).merge(AccountStat.order('last_status_at DESC NULLS LAST')).references(:account_stat) }
scope :by_recent_activity, -> { left_joins(:user, :account_stat).order(coalesced_activity_timestamps.desc).order(id: :desc) }
scope :popular, -> { order('account_stats.followers_count desc') }
Expand Down
32 changes: 26 additions & 6 deletions app/models/account_suggestions/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@ def get(_account, **kwargs)
protected

def base_account_scope(account)
Account.searchable
.followable_by(account)
.not_excluded_by_account(account)
.not_domain_blocked_by_account(account)
.where.not(id: account.id)
.joins("LEFT OUTER JOIN follow_recommendation_mutes ON follow_recommendation_mutes.target_account_id = accounts.id AND follow_recommendation_mutes.account_id = #{account.id}").where(follow_recommendation_mutes: { target_account_id: nil })
Account
.searchable
.where.not(follows_sql, id: account.id)
.where.not(follow_requests_sql, id: account.id)
.not_excluded_by_account(account)
.not_domain_blocked_by_account(account)
.where.not(id: account.id)
.where.not(follow_recommendation_mutes_sql, id: account.id)
end

def follows_sql
<<~SQL.squish
EXISTS (SELECT 1 FROM follows WHERE follows.target_account_id = accounts.id AND follows.account_id = :id)
SQL
end

def follow_requests_sql
<<~SQL.squish
EXISTS (SELECT 1 FROM follow_requests WHERE follow_requests.target_account_id = accounts.id AND follow_requests.account_id = :id)
SQL
end

def follow_recommendation_mutes_sql
<<~SQL.squish
EXISTS (SELECT 1 FROM follow_recommendation_mutes WHERE follow_recommendation_mutes.target_account_id = accounts.id AND follow_recommendation_mutes.account_id = :id)
SQL
end
end

0 comments on commit a90c134

Please sign in to comment.