Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abuse_reports_finder.rb « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a6d04131944c2eb18a214767ed55c70e3469105 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

class AbuseReportsFinder
  attr_reader :params, :reports

  DEFAULT_STATUS_FILTER = 'open'
  DEFAULT_SORT = 'created_at_desc'
  ALLOWED_SORT = [DEFAULT_SORT, *%w[created_at_asc updated_at_desc updated_at_asc]].freeze

  def initialize(params = {})
    @params = params
    @reports = AbuseReport.all
  end

  def execute
    filter_reports
    sort_reports

    reports.with_users.page(params[:page])
  end

  private

  def filter_reports
    filter_by_user_id

    filter_by_user
    filter_by_reporter
    filter_by_status
    filter_by_category
  end

  def filter_by_status
    return unless Feature.enabled?(:abuse_reports_list)
    return unless params[:status].present?

    status = params[:status]
    status = DEFAULT_STATUS_FILTER unless status.in?(AbuseReport.statuses.keys)

    case status
    when 'open'
      @reports = @reports.open
    when 'closed'
      @reports = @reports.closed
    end
  end

  def filter_by_category
    return unless params[:category].present?

    @reports = @reports.by_category(params[:category])
  end

  def filter_by_user
    return unless params[:user].present?

    user_id = find_user_id(params[:user])
    return unless user_id

    @reports = @reports.by_user_id(user_id)
  end

  def filter_by_reporter
    return unless params[:reporter].present?

    user_id = find_user_id(params[:reporter])
    return unless user_id

    @reports = @reports.by_reporter_id(user_id)
  end

  def filter_by_user_id
    return unless params[:user_id].present?

    @reports = @reports.by_user_id(params[:user_id])
  end

  def sort_reports
    if Feature.disabled?(:abuse_reports_list)
      @reports = @reports.with_order_id_desc
      return
    end

    sort_by = params[:sort]
    sort_by = DEFAULT_SORT unless sort_by.in?(ALLOWED_SORT)

    @reports = @reports.order_by(sort_by)
  end

  def find_user_id(username)
    User.by_username(username).pick(:id)
  end
end