2017-04-13 11:04:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AccountFilter
|
2020-01-20 14:55:03 +00:00
|
|
|
KEYS = %i(
|
|
|
|
local
|
|
|
|
remote
|
|
|
|
by_domain
|
|
|
|
active
|
|
|
|
pending
|
2022-05-16 12:57:18 +00:00
|
|
|
soft_silenced
|
|
|
|
hard_silenced
|
2020-01-20 14:55:03 +00:00
|
|
|
suspended
|
|
|
|
username
|
|
|
|
display_name
|
|
|
|
email
|
|
|
|
ip
|
|
|
|
staff
|
2020-03-08 14:39:13 +00:00
|
|
|
order
|
2020-01-20 14:55:03 +00:00
|
|
|
).freeze
|
|
|
|
|
2017-04-13 11:04:23 +00:00
|
|
|
attr_reader :params
|
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@params = params
|
2018-11-26 14:53:27 +00:00
|
|
|
set_defaults!
|
2017-04-13 11:04:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def results
|
2020-03-08 14:39:13 +00:00
|
|
|
scope = Account.includes(:user).reorder(nil)
|
2017-10-05 21:42:05 +00:00
|
|
|
|
2017-04-13 11:04:23 +00:00
|
|
|
params.each do |key, value|
|
2018-11-26 14:53:27 +00:00
|
|
|
scope.merge!(scope_for(key, value.to_s.strip)) if value.present?
|
2017-04-13 11:04:23 +00:00
|
|
|
end
|
2017-10-05 21:42:05 +00:00
|
|
|
|
2017-04-13 11:04:23 +00:00
|
|
|
scope
|
|
|
|
end
|
|
|
|
|
2017-05-17 01:00:34 +00:00
|
|
|
private
|
|
|
|
|
2018-11-26 14:53:27 +00:00
|
|
|
def set_defaults!
|
|
|
|
params['local'] = '1' if params['remote'].blank?
|
2022-05-16 12:57:18 +00:00
|
|
|
params['active'] = '1' if params['suspended'].blank? && params['soft_silenced'].blank? && params['hard_silenced'].blank? && params['pending'].blank?
|
2020-03-08 14:39:13 +00:00
|
|
|
params['order'] = 'recent' if params['order'].blank?
|
2018-11-26 14:53:27 +00:00
|
|
|
end
|
|
|
|
|
2017-04-13 11:04:23 +00:00
|
|
|
def scope_for(key, value)
|
2017-05-17 01:00:34 +00:00
|
|
|
case key.to_s
|
|
|
|
when 'local'
|
2020-12-15 16:23:58 +00:00
|
|
|
Account.local.without_instance_actor
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'remote'
|
2017-04-13 11:04:23 +00:00
|
|
|
Account.remote
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'by_domain'
|
2017-04-13 11:04:23 +00:00
|
|
|
Account.where(domain: value)
|
2018-11-26 14:53:27 +00:00
|
|
|
when 'active'
|
|
|
|
Account.without_suspended
|
2019-03-14 04:28:30 +00:00
|
|
|
when 'pending'
|
2020-03-08 14:39:13 +00:00
|
|
|
accounts_with_users.merge(User.pending)
|
2019-06-20 00:52:34 +00:00
|
|
|
when 'disabled'
|
2020-03-08 14:39:13 +00:00
|
|
|
accounts_with_users.merge(User.disabled)
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'silenced'
|
2017-04-13 11:04:23 +00:00
|
|
|
Account.silenced
|
2022-05-16 12:57:18 +00:00
|
|
|
when 'soft_silenced'
|
|
|
|
Account.soft_silenced
|
|
|
|
when 'hard_silenced'
|
|
|
|
Account.hard_silenced
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'suspended'
|
2017-04-13 11:04:23 +00:00
|
|
|
Account.suspended
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'username'
|
2017-05-22 19:50:58 +00:00
|
|
|
Account.matches_username(value)
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'display_name'
|
2017-05-22 19:50:58 +00:00
|
|
|
Account.matches_display_name(value)
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'email'
|
2020-03-08 14:39:13 +00:00
|
|
|
accounts_with_users.merge(User.matches_email(value))
|
2017-05-17 01:00:34 +00:00
|
|
|
when 'ip'
|
2020-01-20 16:00:41 +00:00
|
|
|
valid_ip?(value) ? accounts_with_users.merge(User.matches_ip(value)) : Account.none
|
2017-12-13 11:15:10 +00:00
|
|
|
when 'staff'
|
2020-03-08 14:39:13 +00:00
|
|
|
accounts_with_users.merge(User.staff)
|
|
|
|
when 'order'
|
|
|
|
order_scope(value)
|
2017-04-13 11:04:23 +00:00
|
|
|
else
|
|
|
|
raise "Unknown filter: #{key}"
|
|
|
|
end
|
|
|
|
end
|
2017-05-17 01:00:34 +00:00
|
|
|
|
2020-03-08 14:39:13 +00:00
|
|
|
def order_scope(value)
|
|
|
|
case value
|
|
|
|
when 'active'
|
|
|
|
params['remote'] ? Account.joins(:account_stat).by_recent_status : Account.joins(:user).by_recent_sign_in
|
|
|
|
when 'recent'
|
|
|
|
Account.recent
|
|
|
|
when 'alphabetic'
|
|
|
|
Account.alphabetic
|
|
|
|
else
|
|
|
|
raise "Unknown order: #{value}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-22 19:50:58 +00:00
|
|
|
def accounts_with_users
|
|
|
|
Account.joins(:user)
|
|
|
|
end
|
|
|
|
|
2017-05-17 01:00:34 +00:00
|
|
|
def valid_ip?(value)
|
2018-11-26 14:53:27 +00:00
|
|
|
IPAddr.new(value) && true
|
2017-05-17 01:00:34 +00:00
|
|
|
rescue IPAddr::InvalidAddressError
|
|
|
|
false
|
|
|
|
end
|
2017-04-13 11:04:23 +00:00
|
|
|
end
|