Add audit logs to Admin::InstancesController (#27386)

pull/31109/head
Emelia Smith 2024-07-23 10:12:30 +02:00 committed by GitHub
parent 1b839d2cba
commit bb2e5a4b58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 <strong>%{count} day</strong> without succeeding, no further delivery attempts will be made unless a delivery <em>from</em> the domain is received.

View File

@ -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