Add filter to AccountFilter (#2968)

This commit is contained in:
alpaca-tc 2017-05-17 10:00:34 +09:00 committed by Eugen Rochko
parent abe0d9421f
commit e89e4355eb
6 changed files with 55 additions and 10 deletions

View File

@ -23,7 +23,11 @@ module Admin
:by_domain, :by_domain,
:silenced, :silenced,
:recent, :recent,
:suspended :suspended,
:username,
:display_name,
:email,
:ip
) )
end end
end end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
module Admin::FilterHelper 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 REPORT_FILTERS = %i(resolved account_id target_account_id).freeze
FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS

View File

@ -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

View File

@ -20,6 +20,21 @@
%li= filter_link_to t('admin.accounts.order.alphabetic'), recent: nil %li= filter_link_to t('admin.accounts.order.alphabetic'), recent: nil
%li= filter_link_to t('admin.accounts.order.most_recent'), recent: '1' %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 %table.table
%thead %thead
%tr %tr

View File

@ -62,6 +62,7 @@ en:
feed_url: Feed URL feed_url: Feed URL
followers: Followers followers: Followers
follows: Follows follows: Follows
ip: IP
location: location:
all: All all: All
local: Local local: Local
@ -86,6 +87,7 @@ en:
push_subscription_expires: PuSH subscription expires push_subscription_expires: PuSH subscription expires
reset_password: Reset password reset_password: Reset password
salmon_url: Salmon URL salmon_url: Salmon URL
search: Search
show: show:
created_reports: Reports created by this account created_reports: Reports created by this account
report: report report: report

View File

@ -62,6 +62,7 @@ ja:
feed_url: フィードURL feed_url: フィードURL
followers: フォロワー数 followers: フォロワー数
follows: フォロー数 follows: フォロー数
ip: IP
location: location:
all: すべて all: すべて
local: ローカル local: ローカル
@ -86,6 +87,7 @@ ja:
push_subscription_expires: PuSH購読期限切れ push_subscription_expires: PuSH購読期限切れ
reset_password: パスワード再設定 reset_password: パスワード再設定
salmon_url: Salmon URL salmon_url: Salmon URL
search: 検索
show: show:
created_reports: このアカウントで作られたレポート created_reports: このアカウントで作られたレポート
report: レポート report: レポート