|
|
@ -10,27 +10,49 @@ class AccountFilter |
|
|
|
def results |
|
|
|
def results |
|
|
|
scope = Account.alphabetic |
|
|
|
scope = Account.alphabetic |
|
|
|
params.each do |key, value| |
|
|
|
params.each do |key, value| |
|
|
|
scope = scope.merge scope_for(key, value) |
|
|
|
scope.merge!(scope_for(key, value)) if value.present? |
|
|
|
end |
|
|
|
end |
|
|
|
scope |
|
|
|
scope |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
def scope_for(key, value) |
|
|
|
def scope_for(key, value) |
|
|
|
case key |
|
|
|
accounts = Account.arel_table |
|
|
|
when /local/ |
|
|
|
|
|
|
|
|
|
|
|
case key.to_s |
|
|
|
|
|
|
|
when 'local' |
|
|
|
Account.local |
|
|
|
Account.local |
|
|
|
when /remote/ |
|
|
|
when 'remote' |
|
|
|
Account.remote |
|
|
|
Account.remote |
|
|
|
when /by_domain/ |
|
|
|
when 'by_domain' |
|
|
|
Account.where(domain: value) |
|
|
|
Account.where(domain: value) |
|
|
|
when /silenced/ |
|
|
|
when 'silenced' |
|
|
|
Account.silenced |
|
|
|
Account.silenced |
|
|
|
when /recent/ |
|
|
|
when 'recent' |
|
|
|
Account.recent |
|
|
|
Account.recent |
|
|
|
when /suspended/ |
|
|
|
when 'suspended' |
|
|
|
Account.suspended |
|
|
|
Account.suspended |
|
|
|
|
|
|
|
when 'username' |
|
|
|
|
|
|
|
Account.where(accounts[:username].matches("#{value}%")) |
|
|
|
|
|
|
|
when 'display_name' |
|
|
|
|
|
|
|
Account.where(accounts[:display_name].matches("#{value}%")) |
|
|
|
|
|
|
|
when 'email' |
|
|
|
|
|
|
|
users = User.arel_table |
|
|
|
|
|
|
|
Account.joins(:user).merge(User.where(users[:email].matches("#{value}%"))) |
|
|
|
|
|
|
|
when 'ip' |
|
|
|
|
|
|
|
return Account.default_scoped unless valid_ip?(value) |
|
|
|
|
|
|
|
matches_ip = User.where(current_sign_in_ip: value).or(User.where(last_sign_in_ip: value)) |
|
|
|
|
|
|
|
Account.joins(:user).merge(matches_ip) |
|
|
|
else |
|
|
|
else |
|
|
|
raise "Unknown filter: #{key}" |
|
|
|
raise "Unknown filter: #{key}" |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def valid_ip?(value) |
|
|
|
|
|
|
|
IPAddr.new(value) |
|
|
|
|
|
|
|
true |
|
|
|
|
|
|
|
rescue IPAddr::InvalidAddressError |
|
|
|
|
|
|
|
false |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|