about summary refs log tree commit diff
path: root/app/models/instance_filter.rb
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2020-12-15 18:19:20 +0100
committerGitHub <noreply@github.com>2020-12-15 18:19:20 +0100
commit92cfcf168cae094ece281b3cb6d0f5b1539a8c25 (patch)
tree070a16d495d962a1f4efb9e196fae18803cf3835 /app/models/instance_filter.rb
parent1978f7265e1e83bda25413da26f53c53110af764 (diff)
parentb0722fbc14cf1cee412c3524c51705c9902bde7f (diff)
Merge pull request #1476 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/models/instance_filter.rb')
-rw-r--r--app/models/instance_filter.rb31
1 files changed, 20 insertions, 11 deletions
diff --git a/app/models/instance_filter.rb b/app/models/instance_filter.rb
index 9c467bc27..0598d8fea 100644
--- a/app/models/instance_filter.rb
+++ b/app/models/instance_filter.rb
@@ -13,18 +13,27 @@ class InstanceFilter
   end
 
   def results
-    if params[:limited].present?
-      scope = DomainBlock
-      scope = scope.matches_domain(params[:by_domain]) if params[:by_domain].present?
-      scope.order(id: :desc)
-    elsif params[:allowed].present?
-      scope = DomainAllow
-      scope = scope.matches_domain(params[:by_domain]) if params[:by_domain].present?
-      scope.order(id: :desc)
+    scope = Instance.includes(:domain_block, :domain_allow).order(accounts_count: :desc)
+
+    params.each do |key, value|
+      scope.merge!(scope_for(key, value.to_s.strip)) if value.present?
+    end
+
+    scope
+  end
+
+  private
+
+  def scope_for(key, value)
+    case key.to_s
+    when 'limited'
+      Instance.joins(:domain_block).reorder(Arel.sql('domain_blocks.id desc'))
+    when 'allowed'
+      Instance.joins(:domain_allow).reorder(Arel.sql('domain_allows.id desc'))
+    when 'by_domain'
+      Instance.matches_domain(value)
     else
-      scope = Account.remote
-      scope = scope.matches_domain(params[:by_domain]) if params[:by_domain].present?
-      scope.by_domain_accounts
+      raise "Unknown filter: #{key}"
     end
   end
 end