Add report filtering capabilities

This commit is contained in:
Andreas Nedbal 2024-01-24 22:45:25 +01:00 committed by Andreas Nedbal
parent 2a9404e285
commit 311fc0812b
2 changed files with 53 additions and 3 deletions

View File

@ -4,10 +4,10 @@ class Moderation::ReportsController < ApplicationController
before_action :authenticate_user!
def index
@type = params[:type]
@reports = list_reports(type: @type, last_id: params[:last_id])
filter = ReportFilter.new(filter_params)
@reports = filter.cursored_results(last_id: params[:last_id])
@reports_last_id = @reports.map(&:id).min
@more_data_available = !list_reports(type: @type, last_id: @reports_last_id, size: 1).count.zero?
@more_data_available = filter.cursored_results(last_id: @reports_last_id, size: 1).count.positive?
respond_to do |format|
format.html
@ -17,6 +17,10 @@ class Moderation::ReportsController < ApplicationController
private
def filter_params
params.slice(*ReportFilter::KEYS).permit(*ReportFilter::KEYS)
end
def list_reports(type:, last_id:, size: nil)
cursor_params = { last_id:, size: }.compact

View File

@ -0,0 +1,46 @@
# frozen_string_literal: true
class ReportFilter
include CursorPaginatable
define_cursor_paginator :cursored_results, :results
KEYS = %i[
user
target_user
type
].freeze
attr_reader :params
def initialize(params)
@params = params
end
def results
scope = Report.where(deleted: false)
.order(:created_at)
.reverse_order
params.each do |key, value|
scope.merge!(scope_for(key, value)) if value.present?
end
scope
end
private
def scope_for(key, value)
case key.to_s
when "user"
Report.joins(:user)
.where(users: { screen_name: value })
when "target_user"
Report.joins(:target_user)
.where(users: { screen_name: value })
when "type"
Report.where('LOWER(type) = ?', "reports::#{value}")
end
end
end