From bb2e5a4b585875b85f5025a9124b9b476b408c25 Mon Sep 17 00:00:00 2001 From: Emelia Smith Date: Tue, 23 Jul 2024 10:12:30 +0200 Subject: [PATCH] Add audit logs to Admin::InstancesController (#27386) --- app/controllers/admin/instances_controller.rb | 1 + app/models/admin/action_log_filter.rb | 11 ++++++++++ app/views/admin/instances/show.html.haml | 10 +++++++++ config/locales/en.yml | 3 +++ .../admin/instances_controller_spec.rb | 22 +++++++++++++++++++ 5 files changed, 47 insertions(+) diff --git a/app/controllers/admin/instances_controller.rb b/app/controllers/admin/instances_controller.rb index a6997b62f7..d7f88a71f3 100644 --- a/app/controllers/admin/instances_controller.rb +++ b/app/controllers/admin/instances_controller.rb @@ -13,6 +13,7 @@ module Admin def show authorize :instance, :show? @time_period = (6.days.ago.to_date...Time.now.utc.to_date) + @action_logs = Admin::ActionLogFilter.new(target_domain: @instance.domain).results.limit(5) end def destroy diff --git a/app/models/admin/action_log_filter.rb b/app/models/admin/action_log_filter.rb index fc984b2445..1f48e0a497 100644 --- a/app/models/admin/action_log_filter.rb +++ b/app/models/admin/action_log_filter.rb @@ -5,6 +5,14 @@ class Admin::ActionLogFilter action_type account_id target_account_id + target_domain + ).freeze + + INSTANCE_TARGET_TYPES = %w( + DomainBlock + DomainAllow + Instance + UnavailableDomain ).freeze ACTION_TYPE_MAP = { @@ -95,6 +103,9 @@ class Admin::ActionLogFilter when 'target_account_id' account = Account.find_or_initialize_by(id: value) latest_action_logs.where(target: [account, account.user].compact) + when 'target_domain' + normalized_domain = TagManager.instance.normalize_domain(value) + latest_action_logs.where(human_identifier: normalized_domain, target_type: INSTANCE_TARGET_TYPES) else raise Mastodon::InvalidParameterError, "Unknown filter: #{key}" end diff --git a/app/views/admin/instances/show.html.haml b/app/views/admin/instances/show.html.haml index d916203d0c..c55eb89dc9 100644 --- a/app/views/admin/instances/show.html.haml +++ b/app/views/admin/instances/show.html.haml @@ -114,6 +114,16 @@ - if @instance.persisted? %hr.spacer/ + %h3= t('admin.instances.audit_log.title') + - if @action_logs.empty? + %p= t('accounts.nothing_here') + - else + .report-notes + = render partial: 'admin/action_logs/action_log', collection: @action_logs + = link_to t('admin.instances.audit_log.view_all'), admin_action_logs_path(target_domain: @instance.domain), class: 'button' + + %hr.spacer/ + %h3= t('admin.instances.availability.title') %p diff --git a/config/locales/en.yml b/config/locales/en.yml index cccb172163..322183f4ce 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -471,6 +471,9 @@ en: title: Follow recommendations unsuppress: Restore follow recommendation instances: + audit_log: + title: Recent Audit Logs + view_all: View full audit logs availability: description_html: one: If delivering to the domain fails %{count} day without succeeding, no further delivery attempts will be made unless a delivery from the domain is received. diff --git a/spec/controllers/admin/instances_controller_spec.rb b/spec/controllers/admin/instances_controller_spec.rb index e6fa0b9def..ca64dd90a0 100644 --- a/spec/controllers/admin/instances_controller_spec.rb +++ b/spec/controllers/admin/instances_controller_spec.rb @@ -37,10 +37,32 @@ RSpec.describe Admin::InstancesController do end describe 'GET #show' do + before do + allow(Admin::ActionLogFilter).to receive(:new).and_call_original + end + it 'shows an instance page' do get :show, params: { id: account_popular_main.domain } expect(response).to have_http_status(200) + + instance = assigns(:instance) + expect(instance).to_not be_new_record + + expect(Admin::ActionLogFilter).to have_received(:new).with(target_domain: account_popular_main.domain) + + action_logs = assigns(:action_logs).to_a + expect(action_logs.size).to eq 0 + end + + context 'with an unknown domain' do + it 'returns http success' do + get :show, params: { id: 'unknown.example' } + expect(response).to have_http_status(200) + + instance = assigns(:instance) + expect(instance).to be_new_record + end end end