about summary refs log tree commit diff
path: root/app/models/account_filter.rb
diff options
context:
space:
mode:
authoralpaca-tc <alpaca-tc@alpaca.tc>2017-05-17 10:00:34 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-05-17 03:00:34 +0200
commite89e4355eb2ab74f7ec93313508c3afb72539855 (patch)
tree5fc9845728edcc62c3dc72fc4d97c30c42c5766c /app/models/account_filter.rb
parentabe0d9421f2bc6227360f9420aeac2cefbcdc023 (diff)
Add filter to AccountFilter (#2968)
Diffstat (limited to 'app/models/account_filter.rb')
-rw-r--r--app/models/account_filter.rb38
1 files changed, 30 insertions, 8 deletions
diff --git a/app/models/account_filter.rb b/app/models/account_filter.rb
index a8d8c8837..186b38cd7 100644
--- a/app/models/account_filter.rb
+++ b/app/models/account_filter.rb
@@ -10,27 +10,49 @@ class AccountFilter
   def results
     scope = Account.alphabetic
     params.each do |key, value|
-      scope = scope.merge scope_for(key, value)
+      scope.merge!(scope_for(key, value)) if value.present?
     end
     scope
   end
 
+  private
+
   def scope_for(key, value)
-    case key
-    when /local/
+    accounts = Account.arel_table
+
+    case key.to_s
+    when 'local'
       Account.local
-    when /remote/
+    when 'remote'
       Account.remote
-    when /by_domain/
+    when 'by_domain'
       Account.where(domain: value)
-    when /silenced/
+    when 'silenced'
       Account.silenced
-    when /recent/
+    when 'recent'
       Account.recent
-    when /suspended/
+    when '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
       raise "Unknown filter: #{key}"
     end
   end
+
+  def valid_ip?(value)
+    IPAddr.new(value)
+    true
+  rescue IPAddr::InvalidAddressError
+    false
+  end
 end