2020-01-20 14:55:03 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RelationshipFilter
|
|
|
|
KEYS = %i(
|
|
|
|
relationship
|
|
|
|
status
|
|
|
|
by_domain
|
|
|
|
activity
|
|
|
|
order
|
2020-01-23 19:33:20 +00:00
|
|
|
location
|
2020-01-20 14:55:03 +00:00
|
|
|
).freeze
|
2020-01-23 19:33:20 +00:00
|
|
|
|
2023-05-04 03:33:55 +00:00
|
|
|
IGNORED_PARAMS = %w(relationship page).freeze
|
|
|
|
|
2020-01-23 19:33:20 +00:00
|
|
|
attr_reader :params, :account
|
|
|
|
|
|
|
|
def initialize(account, params)
|
|
|
|
@account = account
|
|
|
|
@params = params
|
|
|
|
|
|
|
|
set_defaults!
|
|
|
|
end
|
|
|
|
|
|
|
|
def results
|
2020-01-23 23:20:23 +00:00
|
|
|
scope = scope_for('relationship', params['relationship'].to_s.strip)
|
2020-01-23 19:33:20 +00:00
|
|
|
|
|
|
|
params.each do |key, value|
|
2023-05-04 03:33:55 +00:00
|
|
|
next if IGNORED_PARAMS.include?(key)
|
2020-01-23 19:33:20 +00:00
|
|
|
|
2020-01-23 23:20:23 +00:00
|
|
|
scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present?
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
scope
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_defaults!
|
|
|
|
params['relationship'] = 'following' if params['relationship'].blank?
|
|
|
|
params['order'] = 'recent' if params['order'].blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def scope_for(key, value)
|
2020-01-23 23:20:23 +00:00
|
|
|
case key
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'relationship'
|
|
|
|
relationship_scope(value)
|
|
|
|
when 'by_domain'
|
|
|
|
by_domain_scope(value)
|
|
|
|
when 'location'
|
|
|
|
location_scope(value)
|
|
|
|
when 'status'
|
|
|
|
status_scope(value)
|
|
|
|
when 'order'
|
|
|
|
order_scope(value)
|
|
|
|
when 'activity'
|
|
|
|
activity_scope(value)
|
|
|
|
else
|
2022-11-14 07:06:06 +00:00
|
|
|
raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def relationship_scope(value)
|
2020-01-23 23:20:23 +00:00
|
|
|
case value
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'following'
|
2023-09-25 13:06:43 +00:00
|
|
|
account.following.includes(:account_stat).reorder(nil)
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'followed_by'
|
2023-09-25 13:06:43 +00:00
|
|
|
account.followers.includes(:account_stat).reorder(nil)
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'mutual'
|
2023-09-25 13:06:43 +00:00
|
|
|
account.followers.includes(:account_stat).reorder(nil).merge(Account.where(id: account.following))
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'invited'
|
2023-09-25 13:06:43 +00:00
|
|
|
Account.joins(user: :invite).merge(Invite.where(user: account.user)).includes(:account_stat).reorder(nil)
|
2020-01-23 19:33:20 +00:00
|
|
|
else
|
2022-11-14 07:06:06 +00:00
|
|
|
raise Mastodon::InvalidParameterError, "Unknown relationship: #{value}"
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_domain_scope(value)
|
2020-01-23 23:20:23 +00:00
|
|
|
Account.where(domain: value)
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def location_scope(value)
|
2020-01-23 23:20:23 +00:00
|
|
|
case value
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'local'
|
|
|
|
Account.local
|
|
|
|
when 'remote'
|
|
|
|
Account.remote
|
|
|
|
else
|
2022-11-14 07:06:06 +00:00
|
|
|
raise Mastodon::InvalidParameterError, "Unknown location: #{value}"
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_scope(value)
|
2020-01-23 23:20:23 +00:00
|
|
|
case value
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'moved'
|
|
|
|
Account.where.not(moved_to_account_id: nil)
|
|
|
|
when 'primary'
|
|
|
|
Account.where(moved_to_account_id: nil)
|
|
|
|
else
|
2022-11-14 07:06:06 +00:00
|
|
|
raise Mastodon::InvalidParameterError, "Unknown status: #{value}"
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_scope(value)
|
2020-01-23 23:20:23 +00:00
|
|
|
case value
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'active'
|
|
|
|
Account.by_recent_status
|
|
|
|
when 'recent'
|
2020-01-23 23:20:23 +00:00
|
|
|
params[:relationship] == 'invited' ? Account.recent : Follow.recent
|
2020-01-23 19:33:20 +00:00
|
|
|
else
|
2022-11-14 07:06:06 +00:00
|
|
|
raise Mastodon::InvalidParameterError, "Unknown order: #{value}"
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity_scope(value)
|
2020-01-23 23:20:23 +00:00
|
|
|
case value
|
2020-01-23 19:33:20 +00:00
|
|
|
when 'dormant'
|
2023-10-06 10:58:16 +00:00
|
|
|
Account.joins(:account_stat).where(account_stat: { last_status_at: [nil, ...1.month.ago] })
|
2020-01-23 19:33:20 +00:00
|
|
|
else
|
2022-11-14 07:06:06 +00:00
|
|
|
raise Mastodon::InvalidParameterError, "Unknown activity: #{value}"
|
2020-01-23 19:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
2020-01-20 14:55:03 +00:00
|
|
|
end
|