about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authornullkal <nullkal@nil.nu>2017-09-13 19:30:07 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-09-13 12:30:07 +0200
commitda77f65c4684a8a9ee25c3e18f6f09824c765c2d (patch)
treee006c2dbdebdb23d55c4b2e57ff66c7585fb0215 /app/models
parent9e2ff3ef71e917ba88a9062814f3e828f9199009 (diff)
Add instance search feature (#4925)
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb1
-rw-r--r--app/models/instance_filter.rb28
2 files changed, 29 insertions, 0 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 864b3cb6e..f175c7081 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -104,6 +104,7 @@ class Account < ApplicationRecord
   scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
   scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) }
   scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
+  scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
 
   delegate :email,
            :current_sign_in_ip,
diff --git a/app/models/instance_filter.rb b/app/models/instance_filter.rb
new file mode 100644
index 000000000..5073cf1fa
--- /dev/null
+++ b/app/models/instance_filter.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+class InstanceFilter
+  attr_reader :params
+
+  def initialize(params)
+    @params = params
+  end
+
+  def results
+    scope = Account.remote.by_domain_accounts
+    params.each do |key, value|
+      scope.merge!(scope_for(key, value)) if value.present?
+    end
+    scope
+  end
+
+  private
+
+  def scope_for(key, value)
+    case key.to_s
+    when 'domain_name'
+      Account.matches_domain(value)
+    else
+      raise "Unknown filter: #{key}"
+    end
+  end
+end