From aa88aca0ad89f24fead6a1c6dbfad9def94bd47e Mon Sep 17 00:00:00 2001 From: Emelia Smith Date: Tue, 16 Jul 2024 15:23:08 +0200 Subject: [PATCH] Add optional OAuth application to reports (#30539) --- app/controllers/api/v1/reports_controller.rb | 2 +- app/models/report.rb | 2 ++ app/services/report_service.rb | 4 +++- app/views/admin/reports/_header_details.html.haml | 6 ++++++ config/locales/en.yml | 1 + .../20240713171841_add_application_to_reports.rb | 8 ++++++++ ...13171909_validate_add_application_to_reports.rb | 7 +++++++ db/schema.rb | 4 +++- spec/lib/activitypub/activity/flag_spec.rb | 1 + spec/requests/api/v1/reports_spec.rb | 1 + spec/services/report_service_spec.rb | 14 ++++++++++++++ 11 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20240713171841_add_application_to_reports.rb create mode 100644 db/migrate/20240713171909_validate_add_application_to_reports.rb diff --git a/app/controllers/api/v1/reports_controller.rb b/app/controllers/api/v1/reports_controller.rb index 300c9faa3f..72f358bb5b 100644 --- a/app/controllers/api/v1/reports_controller.rb +++ b/app/controllers/api/v1/reports_controller.rb @@ -10,7 +10,7 @@ class Api::V1::ReportsController < Api::BaseController @report = ReportService.new.call( current_account, reported_account, - report_params + report_params.merge(application: doorkeeper_token.application) ) render json: @report, serializer: REST::ReportSerializer diff --git a/app/models/report.rb b/app/models/report.rb index 3df5a20e18..17c1503436 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -18,6 +18,7 @@ # category :integer default("other"), not null # action_taken_at :datetime # rule_ids :bigint(8) is an Array +# application_id :bigint(8) # class Report < ApplicationRecord @@ -31,6 +32,7 @@ class Report < ApplicationRecord rate_limit by: :account, family: :reports belongs_to :account + belongs_to :application, class_name: 'Doorkeeper::Application', optional: true with_options class_name: 'Account' do belongs_to :target_account diff --git a/app/services/report_service.rb b/app/services/report_service.rb index dea6df7b0a..c95e216c79 100644 --- a/app/services/report_service.rb +++ b/app/services/report_service.rb @@ -10,6 +10,7 @@ class ReportService < BaseService @comment = options.delete(:comment).presence || '' @category = options[:rule_ids].present? ? 'violation' : (options.delete(:category).presence || 'other') @rule_ids = options.delete(:rule_ids).presence + @application = options.delete(:application).presence @options = options raise ActiveRecord::RecordNotFound if @target_account.unavailable? @@ -35,7 +36,8 @@ class ReportService < BaseService uri: @options[:uri], forwarded: forward_to_origin?, category: @category, - rule_ids: @rule_ids + rule_ids: @rule_ids, + application: @application ) end diff --git a/app/views/admin/reports/_header_details.html.haml b/app/views/admin/reports/_header_details.html.haml index 45790b9cd5..434231f733 100644 --- a/app/views/admin/reports/_header_details.html.haml +++ b/app/views/admin/reports/_header_details.html.haml @@ -14,6 +14,12 @@ = admin_account_link_to report.account - else = report.account.domain + - if report.application.present? + .report-header__details__item + .report-header__details__item__header + %strong= t('admin.reports.reported_with_application') + .report-header__details__item__content + = report.application.name .report-header__details__item .report-header__details__item__header %strong= t('admin.reports.status') diff --git a/config/locales/en.yml b/config/locales/en.yml index 0e3d209ff3..cccb172163 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -639,6 +639,7 @@ en: report: 'Report #%{id}' reported_account: Reported account reported_by: Reported by + reported_with_application: Reported with application resolved: Resolved resolved_msg: Report successfully resolved! skip_to_actions: Skip to actions diff --git a/db/migrate/20240713171841_add_application_to_reports.rb b/db/migrate/20240713171841_add_application_to_reports.rb new file mode 100644 index 0000000000..42ba14e9b2 --- /dev/null +++ b/db/migrate/20240713171841_add_application_to_reports.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddApplicationToReports < ActiveRecord::Migration[7.1] + def change + add_column :reports, :application_id, :bigint, null: true + add_foreign_key :reports, :oauth_applications, column: :application_id, on_delete: :nullify, validate: false + end +end diff --git a/db/migrate/20240713171909_validate_add_application_to_reports.rb b/db/migrate/20240713171909_validate_add_application_to_reports.rb new file mode 100644 index 0000000000..34ae690a39 --- /dev/null +++ b/db/migrate/20240713171909_validate_add_application_to_reports.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ValidateAddApplicationToReports < ActiveRecord::Migration[7.1] + def change + validate_foreign_key :reports, :oauth_applications + end +end diff --git a/db/schema.rb b/db/schema.rb index 87c49d7415..66da12f713 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[7.1].define(version: 2024_07_12_064044) do +ActiveRecord::Schema[7.1].define(version: 2024_07_13_171909) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -929,6 +929,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_12_064044) do t.integer "category", default: 0, null: false t.datetime "action_taken_at", precision: nil t.bigint "rule_ids", array: true + t.bigint "application_id" t.index ["account_id"], name: "index_reports_on_account_id" t.index ["action_taken_by_account_id"], name: "index_reports_on_action_taken_by_account_id", where: "(action_taken_by_account_id IS NOT NULL)" t.index ["assigned_account_id"], name: "index_reports_on_assigned_account_id", where: "(assigned_account_id IS NOT NULL)" @@ -1361,6 +1362,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_12_064044) do add_foreign_key "reports", "accounts", column: "assigned_account_id", on_delete: :nullify add_foreign_key "reports", "accounts", column: "target_account_id", name: "fk_eb37af34f0", on_delete: :cascade add_foreign_key "reports", "accounts", name: "fk_4b81f7522c", on_delete: :cascade + add_foreign_key "reports", "oauth_applications", column: "application_id", on_delete: :nullify add_foreign_key "scheduled_statuses", "accounts", on_delete: :cascade add_foreign_key "session_activations", "oauth_access_tokens", column: "access_token_id", name: "fk_957e5bda89", on_delete: :cascade add_foreign_key "session_activations", "users", name: "fk_e5fda67334", on_delete: :cascade diff --git a/spec/lib/activitypub/activity/flag_spec.rb b/spec/lib/activitypub/activity/flag_spec.rb index 426cd97df9..be1bd42a14 100644 --- a/spec/lib/activitypub/activity/flag_spec.rb +++ b/spec/lib/activitypub/activity/flag_spec.rb @@ -36,6 +36,7 @@ RSpec.describe ActivityPub::Activity::Flag do expect(report).to_not be_nil expect(report.comment).to eq 'Boo!!' expect(report.status_ids).to eq [status.id] + expect(report.application).to be_nil end end diff --git a/spec/requests/api/v1/reports_spec.rb b/spec/requests/api/v1/reports_spec.rb index 491c6263d3..a72d9bbcd8 100644 --- a/spec/requests/api/v1/reports_spec.rb +++ b/spec/requests/api/v1/reports_spec.rb @@ -47,6 +47,7 @@ RSpec.describe 'Reports' do expect(target_account.targeted_reports).to_not be_empty expect(target_account.targeted_reports.first.comment).to eq 'reasons' + expect(target_account.targeted_reports.first.application).to eq token.application expect(emails.size) .to eq(1) diff --git a/spec/services/report_service_spec.rb b/spec/services/report_service_spec.rb index bf1f937191..6518c5c27a 100644 --- a/spec/services/report_service_spec.rb +++ b/spec/services/report_service_spec.rb @@ -23,6 +23,11 @@ RSpec.describe ReportService do stub_request(:post, 'http://example.com/inbox').to_return(status: 200) end + it 'does not have an application' do + report = subject.call(source_account, remote_account) + expect(report.application).to be_nil + end + context 'when forward is true', :inline_jobs do let(:forward) { true } @@ -96,6 +101,15 @@ RSpec.describe ReportService do end end + context 'when passed an application' do + let(:application) { Fabricate(:application) } + + it 'has an application' do + report = subject.call(source_account, target_account, application: application) + expect(report.application).to eq application + end + end + context 'when the reported status is a DM' do subject do -> { described_class.new.call(source_account, target_account, status_ids: [status.id]) }