FEATURE: Make staff action logs export respect the filter (#34113)

On the Admin > Logs & Screening page, the Export button never applied
the current page's filters. For older websites, viewing large exported
logs may cause lag.

This commit makes the Export button respect the current page's filters,
reducing the size of exported staff action logs and improving
consistency.
This commit is contained in:
Linca
2025-08-07 16:25:16 +08:00
committed by GitHub
parent 0466f51081
commit 7d838842bf
5 changed files with 64 additions and 21 deletions
@@ -156,6 +156,7 @@ export default class AdminLogsStaffActionLogsController extends Controller {
@action
exportStaffActionLogs() {
exportEntity("staff_action", {
...this.filters,
start_date: this.startDate?.toISOString(),
end_date: this.endDate?.toISOString(),
}).then(outputExportResult);
+1 -1
View File
@@ -67,7 +67,7 @@ class ExportCsvController < ApplicationController
@_export_params ||=
begin
params.require(:entity)
params.permit(:entity, args: Report::FILTERS).to_h
params.permit(:entity, args: [*Report::FILTERS, *UserHistory.staff_filters]).to_h
end
end
end
+3 -17
View File
@@ -148,23 +148,9 @@ module Jobs
end
def staff_action_export
start_date = @extra[:start_date]&.to_time if @extra
end_date = @extra[:end_date]&.to_time if @extra
query = UserHistory
query = query.where("created_at >= ?", start_date) if start_date.present?
query = query.where("created_at <= ?", end_date) if end_date.present?
staff_action_data =
if @current_user.admin?
query.only_staff_actions
else
query.where(admin_only: false).only_staff_actions
end
staff_action_data.find_each(order: :desc) do |staff_action|
yield get_staff_action_fields(staff_action)
end
UserHistory
.staff_action_records(@current_user, @extra)
.find_each { |staff_action| yield get_staff_action_fields(staff_action) }
end
def screened_email_export
+36 -3
View File
@@ -74,7 +74,7 @@ RSpec.describe Jobs::ExportCsvFile do
end
context "when exporting staff action" do
before do
it "exports staff action logs with date filters" do
freeze_time
(1..10).each do |i|
Fabricate(
@@ -83,9 +83,7 @@ RSpec.describe Jobs::ExportCsvFile do
created_at: i.days.ago,
)
end
end
it "exports staff action logs with date filters" do
expect do
Jobs::ExportCsvFile.new.execute(
user_id: admin.id,
@@ -105,6 +103,41 @@ RSpec.describe Jobs::ExportCsvFile do
end
end
end
it "delegates to UserHistory.staff_action_records" do
Fabricate(:user_history, action: UserHistory.actions[:suspend_user])
Fabricate(:user_history, action: UserHistory.actions[:change_site_setting])
Fabricate(:user_history, action: UserHistory.actions[:delete_theme])
res =
UserHistory.staff_action_records(
admin,
action_id: UserHistory.actions[:suspend_user].to_s,
)
UserHistory
.expects(:staff_action_records)
.with(
admin,
HashWithIndifferentAccess.new("action_id" => UserHistory.actions[:suspend_user].to_s),
)
.returns(res)
Jobs::ExportCsvFile.new.execute(
user_id: admin.id,
entity: "staff_action",
args: {
"action_id" => UserHistory.actions[:suspend_user].to_s,
},
)
Zip::File.open(Discourse.store.path_for(Upload.last)) do |zip_file|
zip_file.each do |entry|
content = zip_file.read(entry)
expect(CSV.parse(content).size).to eq(2)
end
end
end
end
end
@@ -64,6 +64,29 @@ describe "Admin staff action logs", type: :system do
expect(staff_action_logs_page).to have_log_row(history_3)
end
it "can export filtered logs" do
visit "/admin/logs/staff_action_logs"
staff_action_logs_page.filter_by_action(:change_site_setting)
expect(page).to have_css(
".staff-action-logs-filters .filter",
text: I18n.t("admin_js.admin.logs.staff_actions.actions.change_site_setting"),
)
expect(page).to have_css(".export-staff-action-logs")
expect do
staff_action_logs_page.click_export_button
expect(page).to have_text(I18n.t("admin_js.admin.export_csv.success"))
end.to change { Jobs::ExportCsvFile.jobs.size }.by(1)
job = Jobs::ExportCsvFile.jobs.last
args = job["args"].first["args"]
expect(args).to include({ "action_id" => UserHistory.actions[:change_site_setting].to_s })
end
it "displays no result" do
visit "/admin/logs/staff_action_logs"
staff_action_logs_page.filter_by_action(:toggle_flag)