diff options
Diffstat (limited to 'app/finders/abuse_reports_finder.rb')
-rw-r--r-- | app/finders/abuse_reports_finder.rb | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/app/finders/abuse_reports_finder.rb b/app/finders/abuse_reports_finder.rb index 6a6d0413194..43cebd16d92 100644 --- a/app/finders/abuse_reports_finder.rb +++ b/app/finders/abuse_reports_finder.rb @@ -3,9 +3,13 @@ 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 + 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 @@ -14,6 +18,7 @@ class AbuseReportsFinder def execute filter_reports + aggregate_reports sort_reports reports.with_users.page(params[:page]) @@ -22,20 +27,28 @@ class AbuseReportsFinder private def filter_reports - filter_by_user_id + if Feature.disabled?(:abuse_reports_list) + filter_by_user_id + return + end + filter_by_status filter_by_user filter_by_reporter - filter_by_status 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 Feature.enabled?(:abuse_reports_list) return unless params[:status].present? status = params[:status] - status = DEFAULT_STATUS_FILTER unless status.in?(AbuseReport.statuses.keys) + status = STATUS_OPEN unless status.in?(AbuseReport.statuses.keys) case status when 'open' @@ -69,10 +82,13 @@ class AbuseReportsFinder @reports = @reports.by_reporter_id(user_id) end - def filter_by_user_id - return unless params[:user_id].present? + def sort_key + sort_key = params[:sort] - @reports = @reports.by_user_id(params[:user_id]) + 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 @@ -81,13 +97,31 @@ class AbuseReportsFinder return end - sort_by = params[:sort] - sort_by = DEFAULT_SORT unless sort_by.in?(ALLOWED_SORT) + # 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_by) + @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 |