From 66d8f99a30f9e6062f1bff37d5115beddce9b55d Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 18 Apr 2017 13:36:18 -0400 Subject: [PATCH] Admin reports with accounts (#2092) * Add a ReportFilter class * Add reports and targeted_reports relationships to Account * Use ReportFilter from admin/reports controller * Link to admin/reports filtered views from admin account show view * Add indexes to reports.account_id and reports.target_account_id --- app/controllers/admin/reports_controller.rb | 10 ++++-- app/helpers/admin/filter_helper.rb | 2 +- app/models/account.rb | 4 +++ app/models/report_filter.rb | 30 ++++++++++++++++++ app/views/admin/accounts/show.html.haml | 6 ++++ config/locales/en.yml | 4 +++ ...728_add_indexes_to_reports_for_accounts.rb | 6 ++++ db/schema.rb | 4 ++- spec/models/report_filter_spec.rb | 31 +++++++++++++++++++ 9 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 app/models/report_filter.rb create mode 100644 db/migrate/20170418160728_add_indexes_to_reports_for_accounts.rb create mode 100644 spec/models/report_filter_spec.rb diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb index 4a6f9ea7f6..fc63ca5c0c 100644 --- a/app/controllers/admin/reports_controller.rb +++ b/app/controllers/admin/reports_controller.rb @@ -49,14 +49,18 @@ module Admin end def filtered_reports - filtering_scope.order('id desc').includes( + ReportFilter.new(filter_params).results.order('id desc').includes( :account, :target_account ) end - def filtering_scope - params[:resolved].present? ? Report.resolved : Report.unresolved + def filter_params + params.permit( + :account_id, + :resolved, + :target_account_id + ) end def set_report diff --git a/app/helpers/admin/filter_helper.rb b/app/helpers/admin/filter_helper.rb index 591056dd78..5080e63505 100644 --- a/app/helpers/admin/filter_helper.rb +++ b/app/helpers/admin/filter_helper.rb @@ -2,7 +2,7 @@ module Admin::FilterHelper ACCOUNT_FILTERS = %i[local remote by_domain silenced suspended recent].freeze - REPORT_FILTERS = %i[resolved].freeze + REPORT_FILTERS = %i[resolved account_id target_account_id].freeze FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS diff --git a/app/models/account.rb b/app/models/account.rb index 259a874511..2a10f8345a 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -55,6 +55,10 @@ class Account < ApplicationRecord # PuSH subscriptions has_many :subscriptions, dependent: :destroy + # Report relationships + has_many :reports + has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id + scope :remote, -> { where.not(domain: nil) } scope :local, -> { where(domain: nil) } scope :without_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) = 0') } diff --git a/app/models/report_filter.rb b/app/models/report_filter.rb new file mode 100644 index 0000000000..56ab28df7f --- /dev/null +++ b/app/models/report_filter.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class ReportFilter + attr_reader :params + + def initialize(params) + @params = params + end + + def results + scope = Report.unresolved + params.each do |key, value| + scope = scope.merge scope_for(key, value) + end + scope + end + + def scope_for(key, value) + case key.to_sym + when :resolved + Report.resolved + when :account_id + Report.where(account_id: value) + when :target_account_id + Report.where(target_account_id: value) + else + raise "Unknown filter: #{key}" + end + end +end diff --git a/app/views/admin/accounts/show.html.haml b/app/views/admin/accounts/show.html.haml index 7609868e60..ebf2b5ac1b 100644 --- a/app/views/admin/accounts/show.html.haml +++ b/app/views/admin/accounts/show.html.haml @@ -60,6 +60,12 @@ = @account.media_attachments.count = surround '(', ')' do = number_to_human_size @account.media_attachments.sum('file_file_size') + %tr + %th= t('.created_reports') + %td= link_to pluralize(@account.reports.count, t('.report')), admin_reports_path(account_id: @account.id) + %tr + %th= t('.targeted_reports') + %td= link_to pluralize(@account.targeted_reports.count, t('.report')), admin_reports_path(target_account_id: @account.id) - if @account.local? %div{ style: 'float: right' } diff --git a/config/locales/en.yml b/config/locales/en.yml index 039dabf871..2d88bee940 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -73,6 +73,10 @@ en: push_subscription_expires: PuSH subscription expires reset_password: Reset password salmon_url: Salmon URL + show: + created_reports: Reports created by this account + report: report + targeted_reports: Reports made about this account silence: Silence statuses: Statuses title: Accounts diff --git a/db/migrate/20170418160728_add_indexes_to_reports_for_accounts.rb b/db/migrate/20170418160728_add_indexes_to_reports_for_accounts.rb new file mode 100644 index 0000000000..cd69bb8b14 --- /dev/null +++ b/db/migrate/20170418160728_add_indexes_to_reports_for_accounts.rb @@ -0,0 +1,6 @@ +class AddIndexesToReportsForAccounts < ActiveRecord::Migration[5.0] + def change + add_index :reports, :account_id + add_index :reports, :target_account_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 62ff4207d3..78b419b827 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170414132105) do +ActiveRecord::Schema.define(version: 20170418160728) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -215,6 +215,8 @@ ActiveRecord::Schema.define(version: 20170414132105) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "action_taken_by_account_id" + t.index ["account_id"], name: "index_reports_on_account_id", using: :btree + t.index ["target_account_id"], name: "index_reports_on_target_account_id", using: :btree end create_table "settings", force: :cascade do |t| diff --git a/spec/models/report_filter_spec.rb b/spec/models/report_filter_spec.rb new file mode 100644 index 0000000000..fc0b7d7b50 --- /dev/null +++ b/spec/models/report_filter_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +describe ReportFilter do + describe 'with empty params' do + it 'defaults to unresolved reports list' do + filter = ReportFilter.new({}) + + expect(filter.results).to eq Report.unresolved + end + end + + describe 'with invalid params' do + it 'raises with key error' do + filter = ReportFilter.new(wrong: true) + + expect { filter.results }.to raise_error(/wrong/) + end + end + + describe 'with valid params' do + it 'combines filters on Report' do + filter = ReportFilter.new(account_id: '123', resolved: true) + + allow(Report).to receive(:where).and_return(Report.none) + allow(Report).to receive(:resolved).and_return(Report.none) + filter.results + expect(Report).to have_received(:where).with(account_id: '123') + expect(Report).to have_received(:resolved) + end + end +end