blob: 43cebd16d92cba8869d62437b92d12e43ff93b7c (
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# frozen_string_literal: true
class AbuseReportsFinder
attr_reader :params, :reports
STATUS_OPEN = 'open'
DEFAULT_SORT_STATUS_CLOSED = 'created_at_desc'
ALLOWED_SORT = [DEFAULT_SORT_STATUS_CLOSED, *%w[created_at_asc updated_at_desc updated_at_asc]].freeze
DEFAULT_SORT_STATUS_OPEN = 'number_of_reports_desc'
SORT_BY_COUNT = [DEFAULT_SORT_STATUS_OPEN].freeze
def initialize(params = {})
@params = params
@reports = AbuseReport.all
end
def execute
filter_reports
aggregate_reports
sort_reports
reports.with_users.page(params[:page])
end
private
def filter_reports
if Feature.disabled?(:abuse_reports_list)
filter_by_user_id
return
end
filter_by_status
filter_by_user
filter_by_reporter
filter_by_category
end
def filter_by_user_id
return unless params[:user_id].present?
@reports = @reports.by_user_id(params[:user_id])
end
def filter_by_status
return unless params[:status].present?
status = params[:status]
status = STATUS_OPEN 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 sort_key
sort_key = params[:sort]
return sort_key if sort_key.in?(ALLOWED_SORT + SORT_BY_COUNT)
return DEFAULT_SORT_STATUS_OPEN if status_open?
DEFAULT_SORT_STATUS_CLOSED
end
def sort_reports
if Feature.disabled?(:abuse_reports_list)
@reports = @reports.with_order_id_desc
return
end
# let sub_query in aggregate_reports do the sorting if sorting by number of reports
return if sort_key.in?(SORT_BY_COUNT)
@reports = @reports.order_by(sort_key)
end
def find_user_id(username)
User.by_username(username).pick(:id)
end
def status_open?
return unless Feature.enabled?(:abuse_reports_list) && params[:status].present?
status = params[:status]
status = STATUS_OPEN unless status.in?(AbuseReport.statuses.keys)
status == STATUS_OPEN
end
def aggregate_reports
if status_open?
sort_by_count = sort_key.in?(SORT_BY_COUNT)
@reports = @reports.aggregated_by_user_and_category(sort_by_count)
end
@reports
end
end
|