about summary refs log tree commit diff
path: root/app/models/admin/status_filter.rb
blob: 4d439e9a1ce23dbd7d4c881142baeb76fd21f33f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

class Admin::StatusFilter
  KEYS = %i(
    media
    report_id
  ).freeze

  attr_reader :params

  def initialize(account, params)
    @account = account
    @params  = params
  end

  def results
    scope = @account.statuses.where(visibility: [:public, :unlisted])

    params.each do |key, value|
      next if %w(page report_id).include?(key.to_s)

      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 'media'
      Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id).reorder('statuses.id desc')
    else
      raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
    end
  end
end