]> cat aescling's git repositories - mastodon.git/commitdiff
Add filter to AccountFilter (#2968)
authoralpaca-tc <alpaca-tc@alpaca.tc>
Wed, 17 May 2017 01:00:34 +0000 (10:00 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Wed, 17 May 2017 01:00:34 +0000 (03:00 +0200)
app/controllers/admin/accounts_controller.rb
app/helpers/admin/filter_helper.rb
app/models/account_filter.rb
app/views/admin/accounts/index.html.haml
config/locales/en.yml
config/locales/ja.yml

index 0e9e52f42a1e8a88d52b3db710a50cc2fdbf8547..4c41c42159cff9569a643ea14e4b730ed7e8ea70 100644 (file)
@@ -23,7 +23,11 @@ module Admin
         :by_domain,
         :silenced,
         :recent,
-        :suspended
+        :suspended,
+        :username,
+        :display_name,
+        :email,
+        :ip
       )
     end
   end
index 1e4a3f0d0fca0df77171034ab01b34faaafe0838..72f851e5a78b624b0dd17a9c54d173cbd94bad79 100644 (file)
@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 
 module Admin::FilterHelper
-  ACCOUNT_FILTERS = %i(local remote by_domain silenced suspended recent).freeze
+  ACCOUNT_FILTERS = %i(local remote by_domain silenced suspended recent username display_name email ip).freeze
   REPORT_FILTERS = %i(resolved account_id target_account_id).freeze
 
   FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS
index a8d8c8837634ccc4d93fd7494206340c19247bc2..186b38cd78188a25ad129f2805ac94cadfa15efb 100644 (file)
@@ -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
index 0cff944ba1134f273c3b0b636f2be0055938be03..c9a5dd17a9fb6f8f5e9b3bf57ebf9aca3ebff53f 100644 (file)
       %li= filter_link_to t('admin.accounts.order.alphabetic'), recent: nil
       %li= filter_link_to t('admin.accounts.order.most_recent'), recent: '1'
 
+= form_tag(admin_accounts_url, { method: 'GET', class: 'simple_form' }) do
+  .fields-group
+    - Admin::FilterHelper::ACCOUNT_FILTERS.each do |key|
+      - if params[key].present?
+        = hidden_field_tag key, params[key]
+
+    - %i(username display_name email ip).each do |key|
+      .input.string.optional
+        .label_input
+          %label.string.optional= t("admin.accounts.#{key}")
+          = text_field_tag key, params[key], class: 'string optional'
+
+    .actions
+      %button.btn= t('admin.accounts.search')
+
 %table.table
   %thead
     %tr
index a2269d3e400905f0274bcb4ebba57503aa1e07d7..a0c47e11e4de7025606262d4c3824d772dad6f0c 100644 (file)
@@ -62,6 +62,7 @@ en:
       feed_url: Feed URL
       followers: Followers
       follows: Follows
+      ip: IP
       location:
         all: All
         local: Local
@@ -86,6 +87,7 @@ en:
       push_subscription_expires: PuSH subscription expires
       reset_password: Reset password
       salmon_url: Salmon URL
+      search: Search
       show:
         created_reports: Reports created by this account
         report: report
index 5e2cc5839f69d439a3aabdf8bd2907de19e5fc11..4bacd12faedf7ad52c14496b5bb6b72158130017 100644 (file)
@@ -62,6 +62,7 @@ ja:
       feed_url: フィードURL
       followers: フォロワー数
       follows: フォロー数
+      ip: IP
       location:
         all: すべて
         local: ローカル
@@ -86,6 +87,7 @@ ja:
       push_subscription_expires: PuSH購読期限切れ
       reset_password: パスワード再設定
       salmon_url: Salmon URL
+      search: 検索
       show:
         created_reports: このアカウントで作られたレポート
         report: レポート