A platform for community discussion. Free, open, simple.
DEV: Record pending DSA statements for review queue restrictions (#44412)
Alan Guo Xiang Tan8e549739a548
diff --git a/app/models/dsa_statement_of_record.rb b/app/models/dsa_statement_of_record.rbnew file mode 100644index 00000000000..e80ae24a5c4--- /dev/null+++ b/app/models/dsa_statement_of_record.rb@@ -0,0 +1,54 @@+# frozen_string_literal: true++class DsaStatementOfRecord < ActiveRecord::Base+ TERRITORIAL_SCOPE = %w[+ AT+ BE+ BG+ CY+ CZ+ DE+ DK+ EE+ ES+ FI+ FR+ GR+ HR+ HU+ IE+ IS+ IT+ LI+ LT+ LU+ LV+ MT+ NL+ NO+ PL+ PT+ RO+ SE+ SI+ SK+ ].freeze++ enum :status, { pending: 0 }+end++# == Schema Information+#+# Table name: dsa_statement_of_records+#+# id :uuid not null, primary key+# payload :jsonb not null+# status :integer default("pending"), not null+# created_at :datetime not null+# updated_at :datetime not null+# reviewable_id :bigint not null+#+# Indexes+#+# index_dsa_statement_of_records_on_reviewable_id (reviewable_id)+#diff --git a/app/models/reviewable.rb b/app/models/reviewable.rbindex 73d14d6f3ec..7653c614bf0 100644--- a/app/models/reviewable.rb+++ b/app/models/reviewable.rb@@ -436,6 +436,32 @@ class Reviewable < ActiveRecord::Baseupdate_count = transition_to(result.transition_to, performed_by) if result.transition_toupdate_flag_stats(**result.update_flag_stats) if result.update_flag_statsrecalculate_score if result.recalculate_score++ if SiteSetting.dsa_reporting_enabled+ attributes =+ dsa_statement_attributes(+ action_id:,+ result:,+ automated_detection: dsa_automated_detection,+ automated_decision:+ dsa_automated_decision(+ performed_by:,+ decision_automation: args[:decision_automation],+ ),+ )++ if attributes+ DsaStatementOfRecord.create!(+ reviewable_id: id,+ payload:+ attributes.merge(+ application_date: Date.current.iso8601,+ territorial_scope: DsaStatementOfRecord::TERRITORIAL_SCOPE,+ source_type: "SOURCE_VOLUNTARY",+ ),+ )+ end+ endendresult.after_commit.call if result && result.after_commit@@ -1005,6 +1031,132 @@ class Reviewable < ActiveRecord::Baseprivate+ # Returns the DSA statement attributes for a successfully completed action.+ #+ # Override in subclasses to declare each action's decision and subject metadata.+ # Return nil when the action should not produce a statement.+ #+ # Called inside the moderation transaction after the action and reviewable updates+ # succeed, when the dsa_reporting_enabled site setting is enabled. A hash creates+ # one pending statement.+ # Failure to save the statement rolls back the transaction.+ #+ # Keys must be symbols. Each hash must include :content_type, :content_date,+ # :automated_detection, :automated_decision, and at least one decision field.+ #+ # Decision fields:+ #+ # * :decision_visibility [Array<String>] One or more of:+ # * DECISION_VISIBILITY_CONTENT_REMOVED+ # * DECISION_VISIBILITY_CONTENT_DISABLED+ # * DECISION_VISIBILITY_CONTENT_DEMOTED+ # * DECISION_VISIBILITY_CONTENT_AGE_RESTRICTED+ # * DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED+ # * DECISION_VISIBILITY_CONTENT_LABELLED+ # * DECISION_VISIBILITY_OTHER+ # * :decision_visibility_other [String] Required for DECISION_VISIBILITY_OTHER.+ # Maximum 500 characters.+ # * :decision_account [String] One of:+ # * DECISION_ACCOUNT_SUSPENDED+ # * DECISION_ACCOUNT_TERMINATED+ # * :decision_provision [String] One of:+ # * DECISION_PROVISION_PARTIAL_SUSPENSION+ # * DECISION_PROVISION_TOTAL_SUSPENSION+ # * DECISION_PROVISION_PARTIAL_TERMINATION+ # * DECISION_PROVISION_TOTAL_TERMINATION+ # * :decision_monetary [String] One of:+ # * DECISION_MONETARY_SUSPENSION+ # * DECISION_MONETARY_TERMINATION+ # * DECISION_MONETARY_OTHER+ # * :decision_monetary_other [String] Required for DECISION_MONETARY_OTHER.+ # Maximum 500 characters.+ #+ # Subject fields:+ #+ # * :content_type [Array<String>] Required. One or more of:+ # * CONTENT_TYPE_APP+ # * CONTENT_TYPE_AUDIO+ # * CONTENT_TYPE_IMAGE+ # * CONTENT_TYPE_PRODUCT+ # * CONTENT_TYPE_SYNTHETIC_MEDIA+ # * CONTENT_TYPE_TEXT+ # * CONTENT_TYPE_VIDEO+ # * CONTENT_TYPE_OTHER+ # * :content_type_other [String] Required when :content_type includes+ # CONTENT_TYPE_OTHER. Maximum 500 characters. Use "User account" for accounts.+ # * :content_date [String] Required. Subject creation date in YYYY-MM-DD format,+ # on or after 2000-01-01. Use the account creation date for account subjects.+ # * :content_language [String] Optional. Uppercase ISO 639-1 language code.+ #+ # Restriction expiry fields, in YYYY-MM-DD format:+ #+ # * :end_date_visibility_restriction [String]+ # * :end_date_account_restriction [String]+ # * :end_date_service_restriction [String]+ # * :end_date_monetary_restriction [String]+ #+ # These fields are optional. Omit them for an indefinite restriction.+ # Each expiry must be on or after the application date.+ #+ # Automation fields:+ #+ # * :automated_detection [String] Required. "Yes" or "No", indicating whether+ # automated means identified the content.+ # * :automated_decision [String] Required. One of:+ # * AUTOMATED_DECISION_FULLY+ # * AUTOMATED_DECISION_PARTIALLY+ # * AUTOMATED_DECISION_NOT_AUTOMATED+ #+ # Multiple decision fields may describe the same action. Reviewable#perform+ # supplies :application_date, :territorial_scope, and :source_type.+ # Pending statements receive their grounds, category, and supporting explanation+ # during human classification.+ #+ # @param action_id [Symbol] Requested action ID, before alias resolution.+ # @param result [Reviewable::PerformResult] Successful action result, including+ # :deleted_user or :removed_avatar when returned by the action.+ # @param automated_detection [String] Computed detection value: "Yes" or "No".+ # @param automated_decision [String] Computed AUTOMATED_DECISION_* value.+ # @return [Hash{Symbol => String, Array<String>}, nil] Statement attributes,+ # or nil when no statement applies.+ # @example Report a human decision on a manually flagged post+ # def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ # return unless action_id == :agree_and_edit+ #+ # {+ # decision_visibility: ["DECISION_VISIBILITY_OTHER"],+ # decision_visibility_other: "Content edited",+ # content_type: ["CONTENT_TYPE_TEXT"],+ # content_date: post.created_at.to_date.iso8601,+ # automated_detection:,+ # automated_decision:,+ # }+ # end+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ nil+ end++ def dsa_automated_detection+ automated =+ reviewable_scores.where(+ user_id: Discourse::SYSTEM_USER_ID,+ reason: %w[watched_word fast_typer auto_silence_regex email_spam suspect_user],+ ).exists?++ if DiscoursePluginRegistry.apply_modifier(:reviewable_automated_detection, automated, self)+ "Yes"+ else+ "No"+ end+ end++ def dsa_automated_decision(performed_by:, decision_automation:)+ return "AUTOMATED_DECISION_PARTIALLY" if decision_automation == :partial+ return "AUTOMATED_DECISION_FULLY" if decision_automation == :full || !performed_by.human?++ "AUTOMATED_DECISION_NOT_AUTOMATED"+ end+def can_review_target?(guardian)post = target_post!post || guardian.can_review_post?(post)diff --git a/app/models/reviewable_flagged_post.rb b/app/models/reviewable_flagged_post.rbindex 7ef83c150e4..726d06267cc 100644--- a/app/models/reviewable_flagged_post.rb+++ b/app/models/reviewable_flagged_post.rb@@ -415,6 +415,74 @@ class ReviewableFlaggedPost < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :agree_and_edit+ {+ decision_visibility: ["DECISION_VISIBILITY_OTHER"],+ decision_visibility_other: "Content edited",+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ when :agree_and_hide, :agree_and_keep_hidden+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ when :delete_and_agree, :delete_and_ignore, :delete_and_agree_replies,+ :delete_and_ignore_replies, :agree_and_keep_deleted+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ when :agree_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :agree_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :delete_user, :delete_and_block_user, :delete_user_block+ user = target_created_by++ {+ decision_account: "DECISION_ACCOUNT_TERMINATED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ end+ end+def destroyer(performed_by, post)PostDestroyer.new(performed_by, post, reviewable_id: id)enddiff --git a/app/models/reviewable_post.rb b/app/models/reviewable_post.rbindex a6fdb7dabd3..f9bf3083216 100644--- a/app/models/reviewable_post.rb+++ b/app/models/reviewable_post.rb@@ -126,6 +126,45 @@ class ReviewablePost < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :reject_and_delete, :reject_and_keep_deleted+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ when :reject_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :reject_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ end+ end+def post@post ||= target || Post.with_deleted.find_by(id: target_id)enddiff --git a/app/models/reviewable_queued_post.rb b/app/models/reviewable_queued_post.rbindex 9abf39ee0c7..40c0aece92a 100644--- a/app/models/reviewable_queued_post.rb+++ b/app/models/reviewable_queued_post.rb@@ -219,21 +219,85 @@ class ReviewableQueuedPost < Reviewableenddef perform_delete_user(performed_by, args)- reviewable_ids = Reviewable.where(created_by: target_created_by).pluck(:id)- result = super { |r| r.remove_reviewable_ids += reviewable_ids }+ user = target_created_by+ reviewable_ids = Reviewable.where(created_by: user).pluck(:id)++ result =+ super do |result|+ result.deleted_user = user+ result.remove_reviewable_ids += reviewable_ids+ end+update_column(:target_created_by_id, nil)resultenddef perform_delete_and_block_user(performed_by, args)- reviewable_ids = Reviewable.where(created_by: target_created_by).pluck(:id)- result = super { |r| r.remove_reviewable_ids += reviewable_ids }+ user = target_created_by+ reviewable_ids = Reviewable.where(created_by: user).pluck(:id)++ result =+ super do |result|+ result.deleted_user = user+ result.remove_reviewable_ids += reviewable_ids+ end+update_column(:target_created_by_id, nil)resultendprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :reject_post, :revise_and_reject_post, :discard_post+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ when :reject_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :reject_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :delete_user, :delete_and_block_user, :delete_user_block+ user = result.deleted_user++ {+ decision_account: "DECISION_ACCOUNT_TERMINATED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ end+ end+def delete_opts{context: I18n.t("reviewables.actions.delete_user.reason"),diff --git a/app/models/reviewable_user.rb b/app/models/reviewable_user.rbindex 21851b9458d..7e203bc6216 100644--- a/app/models/reviewable_user.rb+++ b/app/models/reviewable_user.rb@@ -87,9 +87,10 @@ class ReviewableUser < Reviewableenddef perform_remove_avatar(performed_by, args)+ avatar = target.uploaded_avatartarget.remove_avatar!(performed_by)- create_result(:success)+ create_result(:success) { |result| result.removed_avatar = avatar }enddef perform_silence_user(performed_by, args)@@ -216,6 +217,57 @@ class ReviewableUser < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :suspend_user+ user = target.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :silence_user+ user = target.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection:,+ automated_decision:,+ }.compact+ when :delete_user, :delete_user_block+ user = target+ return unless user&.destroyed?++ {+ decision_account: "DECISION_ACCOUNT_TERMINATED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ when :remove_avatar+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_IMAGE"],+ content_date: result.removed_avatar.created_at.to_date.iso8601,+ automated_detection:,+ automated_decision:,+ }+ end+ end+def retain_avatar_snapshotupload_id = payload&.dig("avatar_upload_id")return if upload_id.blank?diff --git a/config/site_settings.yml b/config/site_settings.ymlindex f8c4d4b5717..31ddbb3ee41 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -4887,6 +4887,10 @@ experimental:enable_form_templates:client: truedefault: true+ dsa_reporting_enabled:+ default: false+ hidden: true+ area: "experimental"experimental_impersonation_time_limit_minutes:default: 15hidden: truediff --git a/db/migrate/20261009113440_create_dsa_statement_of_records.rb b/db/migrate/20261009113440_create_dsa_statement_of_records.rbnew file mode 100644index 00000000000..4737f8df91f--- /dev/null+++ b/db/migrate/20261009113440_create_dsa_statement_of_records.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++class CreateDsaStatementOfRecords < ActiveRecord::Migration[8.1]+ def change+ create_table :dsa_statement_of_records, id: :uuid do |table|+ table.bigint :reviewable_id, null: false+ table.integer :status, null: false, default: 0+ table.jsonb :payload, null: false+ table.timestamps null: false+ end+ add_index :dsa_statement_of_records, :reviewable_id+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex 47f0e51d253..50547ee317e 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -5937,6 +5937,20 @@ CREATE SEQUENCE public.drafts_id_seqALTER SEQUENCE public.drafts_id_seq OWNED BY public.drafts.id;+--+-- Name: dsa_statement_of_records; Type: TABLE; Schema: public; Owner: -+--++CREATE TABLE public.dsa_statement_of_records (+ id uuid DEFAULT gen_random_uuid() NOT NULL,+ reviewable_id bigint NOT NULL,+ status integer DEFAULT 0 NOT NULL,+ payload jsonb NOT NULL,+ created_at timestamp(6) without time zone NOT NULL,+ updated_at timestamp(6) without time zone NOT NULL+);++---- Name: email_change_requests; Type: TABLE; Schema: public; Owner: ---@@ -17597,6 +17611,14 @@ ALTER TABLE ONLY public.draftsADD CONSTRAINT drafts_pkey PRIMARY KEY (id);+--+-- Name: dsa_statement_of_records dsa_statement_of_records_pkey; Type: CONSTRAINT; Schema: public; Owner: -+--++ALTER TABLE ONLY public.dsa_statement_of_records+ ADD CONSTRAINT dsa_statement_of_records_pkey PRIMARY KEY (id);++---- Name: email_change_requests email_change_requests_pkey; Type: CONSTRAINT; Schema: public; Owner: ---@@ -21942,6 +21964,13 @@ CREATE UNIQUE INDEX index_draft_sequences_on_user_id_and_draft_key ON public.draCREATE UNIQUE INDEX index_drafts_on_user_id_and_draft_key ON public.drafts USING btree (user_id, draft_key);+--+-- Name: index_dsa_statement_of_records_on_reviewable_id; Type: INDEX; Schema: public; Owner: -+--++CREATE INDEX index_dsa_statement_of_records_on_reviewable_id ON public.dsa_statement_of_records USING btree (reviewable_id);++---- Name: index_email_change_requests_on_user_id; Type: INDEX; Schema: public; Owner: ---@@ -25808,6 +25837,7 @@ INSERT INTO "schema_migrations" (version) VALUES('20261009144947'),('20261009130230'),('20261009121104'),+('20261009113440'),('20261008005940'),('20261007082151'),('20261006113418'),diff --git a/lib/reviewable/perform_result.rb b/lib/reviewable/perform_result.rbindex 39143ad626b..a2016d0fbf9 100644--- a/lib/reviewable/perform_result.rb+++ b/lib/reviewable/perform_result.rb@@ -14,6 +14,8 @@ class Reviewable < ActiveRecord::Base:update_flag_stats,:after_commit,:post_revision,+ :deleted_user,+ :removed_avatar,)def initialize(reviewable, status)diff --git a/plugins/chat/app/models/chat/reviewable_message.rb b/plugins/chat/app/models/chat/reviewable_message.rbindex 4138c116137..f81644956ee 100644--- a/plugins/chat/app/models/chat/reviewable_message.rb+++ b/plugins/chat/app/models/chat/reviewable_message.rb@@ -186,6 +186,46 @@ module Chatprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :agree_and_delete, :delete_and_agree, :delete_and_ignore, :agree_and_keep_deleted,+ :agree_and_keep_hidden+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: chat_message.created_at.to_date.iso8601,+ automated_detection: "No",+ automated_decision:,+ }+ when :agree_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection: "No",+ automated_decision:,+ }.compact+ when :agree_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection: "No",+ automated_decision:,+ }.compact+ end+ end+def agreeyield if block_given?create_result(:success, :approved) do |result|diff --git a/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb b/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rbindex f81093e78d2..3bf923f1fdb 100644--- a/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb+++ b/plugins/chat/spec/models/chat/reviewable_chat_message_spec.rb@@ -57,6 +57,177 @@ RSpec.describe Chat::ReviewableMessage, type: :model doexpect(chat_message.reload.deleted_at).to be_nilexpect(reviewable.reload).to be_pendingend++ context "when the dsa_reporting_enabled site setting is enabled" do+ before do+ reviewable.update!(target_created_by: chat_message.user)+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when unsilence_user handles the chat message reviewable" do+ UserSilencer.silence(user, moderator, silenced_till: 2.days.from_now, reason: "Spam")++ expect {+ expect(reviewable.perform(moderator, :unsilence_user)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when agree_and_keep_message handles the chat message reviewable" do+ expect {+ expect(reviewable.perform(moderator, :agree_and_keep_message)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records a statement when agree_and_delete handles the chat message reviewable" do+ expect { reviewable.perform(moderator, :agree_and_delete) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => chat_message.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when delete_and_agree handles the chat message reviewable" do+ expect { reviewable.perform(moderator, :delete_and_agree) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => chat_message.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when agree_and_keep_deleted handles the chat message reviewable" do+ chat_message.trash!(moderator)++ expect { reviewable.perform(moderator, :agree_and_keep_deleted) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => chat_message.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "creates no statement when agree_and_restore handles the chat message reviewable" do+ chat_message.trash!(moderator)++ expect {+ expect(reviewable.perform(moderator, :agree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree_and_restore handles the chat message reviewable" do+ chat_message.trash!(moderator)++ expect {+ expect(reviewable.perform(moderator, :disagree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree handles the chat message reviewable" do+ expect { expect(reviewable.perform(moderator, :disagree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when ignore handles the chat message reviewable" do+ expect { expect(reviewable.perform(moderator, :ignore)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when agree_and_suspend handles the chat message reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => chat_message.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_silence handles the chat message reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => chat_message.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end+ endendit "agree_and_keep agrees with the flag and doesn't delete the message" dodiff --git a/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb b/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rbindex eae77e6f14d..c2654c8a193 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_chat_message.rb@@ -10,7 +10,11 @@ class ReviewableAiChatMessage < Reviewableenddef self.action_aliases- { agree_and_keep_hidden: :agree_and_delete, delete_and_agree: :agree_and_delete }+ {+ agree_and_keep_hidden: :agree_and_delete,+ agree_and_keep_deleted: :agree_and_keep_message,+ delete_and_agree: :agree_and_delete,+ }enddef self.score_to_silence_user@@ -130,6 +134,46 @@ class ReviewableAiChatMessage < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :agree_and_delete, :delete_and_agree, :delete_and_ignore, :agree_and_keep_deleted,+ :agree_and_keep_hidden+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: chat_message.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision:,+ }+ when :agree_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection: "Yes",+ automated_decision:,+ }.compact+ when :agree_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection: "Yes",+ automated_decision:,+ }.compact+ end+ end+def agreeyield if block_given?create_result(:success, :approved) do |result|diff --git a/plugins/discourse-ai/app/models/reviewable_ai_post.rb b/plugins/discourse-ai/app/models/reviewable_ai_post.rbindex 79dac04706e..2540a1072c4 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_post.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_post.rb@@ -181,6 +181,65 @@ class ReviewableAiPost < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :agree_and_hide, :agree_and_keep_hidden+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision:,+ }+ when :delete_and_agree, :delete_and_ignore, :delete_and_agree_replies,+ :delete_and_ignore_replies+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision:,+ }+ when :agree_and_suspend+ user = post.user.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection: "Yes",+ automated_decision:,+ }.compact+ when :agree_and_silence+ user = post.user.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection: "Yes",+ automated_decision:,+ }.compact+ when :delete_user, :delete_user_block+ user = post.user++ {+ decision_account: "DECISION_ACCOUNT_TERMINATED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision:,+ }+ end+ end+def post@post ||= target || Post.with_deleted.find_by(id: target_id)enddiff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rbindex 5d9801a3e0b..4653423feac 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb@@ -79,6 +79,112 @@ class ReviewableAiToolAction < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ return unless action_id == :approve++ parameters = target.tool_parameters++ case target.tool_name+ when "close_topic"+ return unless parameters["closed"]++ post = Post.with_deleted.find_by!(topic_id: parameters["topic_id"], post_number: 1)++ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }+ when "unlist_topic"+ return unless parameters["unlisted"]++ post = Post.with_deleted.find_by!(topic_id: parameters["topic_id"], post_number: 1)++ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_DEMOTED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }+ when "delete_topic"+ return unless parameters["deleted"]++ post = Post.with_deleted.find_by!(topic_id: parameters["topic_id"], post_number: 1)++ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }+ when "lock_post"+ return unless parameters["locked"]++ post = Post.with_deleted.find(parameters["post_id"])++ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }+ when "set_slow_mode"+ return unless parameters["slow_mode_seconds"].to_i.positive?++ post = Post.with_deleted.find_by!(topic_id: parameters["topic_id"], post_number: 1)++ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }+ when "edit_post"+ post = Post.with_deleted.find(parameters["post_id"])++ {+ decision_visibility: ["DECISION_VISIBILITY_OTHER"],+ decision_visibility_other: "Content edited",+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: post.created_at.to_date.iso8601,+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }+ when "suspend_user"+ user = User.find_by_username(parameters["username"])++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }.compact+ when "silence_user"+ user = User.find_by_username(parameters["username"])++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection: "Yes",+ automated_decision: "AUTOMATED_DECISION_PARTIALLY",+ }.compact+ end+ end+def resolution_result(status, args, tool_result: nil)result = create_result(:success, status)inline_post_id = args[:post_id] || args["post_id"]diff --git a/plugins/discourse-ai/lib/agents/tools/perform_reviewable_action.rb b/plugins/discourse-ai/lib/agents/tools/perform_reviewable_action.rbindex a54d2b48e35..89096d8280c 100644--- a/plugins/discourse-ai/lib/agents/tools/perform_reviewable_action.rb+++ b/plugins/discourse-ai/lib/agents/tools/perform_reviewable_action.rb@@ -103,6 +103,7 @@ module DiscourseAiaction_id,version: reviewable.version,guardian: guardian,+ decision_automation: decision_automation,)rescue Reviewable::InvalidActionreturn(@@ -143,6 +144,17 @@ module DiscourseAidef description_args{ reviewable_id: parameters[:reviewable_id], action: parameters[:action_id] }end++ private++ def decision_automation+ return :partial if context.reviewable_id+ if context.feature_context.key?(:automation_id) ||+ %w[llm_triage llm_tagger ai_report ai_tool_action].include?(context.feature_name)+ return :full+ end+ :partial if context.feature_name == "bot" && (context.post_id || context.message_id)+ endendendenddiff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rbindex 09514ee13d7..0ce4f5073a0 100644--- a/plugins/discourse-ai/plugin.rb+++ b/plugins/discourse-ai/plugin.rb@@ -146,6 +146,17 @@ after_initialize doregister_problem_check ProblemCheck::AiLlmVisionDelegationregister_problem_check ProblemCheck::AiImageCaptionAgent+ register_modifier(:reviewable_automated_detection) do |automated, reviewable|+ automated ||+ reviewable+ .reviewable_scores+ .where(+ "context LIKE ?",+ "#{ReviewableScore.sanitize_sql_like(DiscourseAi::Automation::TRIAGE_AUTOMATION_SCORE_CONTEXT_PREFIX)}%",+ )+ .exists?+ end+register_reviewable_type ReviewableAiChatMessageregister_reviewable_type ReviewableAiPostregister_reviewable_type ReviewableAiToolActiondiff --git a/plugins/discourse-ai/spec/models/reviewable_ai_chat_message_spec.rb b/plugins/discourse-ai/spec/models/reviewable_ai_chat_message_spec.rbindex b1c2da48d2b..5d3665f7f24 100644--- a/plugins/discourse-ai/spec/models/reviewable_ai_chat_message_spec.rb+++ b/plugins/discourse-ai/spec/models/reviewable_ai_chat_message_spec.rb@@ -37,6 +37,169 @@ RSpec.describe ReviewableAiChatMessage, type: :model doexpect(chat_message.reload.deleted_at).to be_nilexpect(reviewable.reload).to be_pendingend++ context "when the dsa_reporting_enabled site setting is enabled" do+ before do+ reviewable.update!(target_created_by: chat_message.user)+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when agree_and_keep_message handles the AI chat message reviewable" do+ expect {+ expect(reviewable.perform(moderator, :agree_and_keep_message)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records a statement when agree_and_delete handles the AI chat message reviewable" do+ expect { reviewable.perform(moderator, :agree_and_delete) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => chat_message.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when delete_and_agree handles the AI chat message reviewable" do+ expect { reviewable.perform(moderator, :delete_and_agree) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => chat_message.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when agree_and_keep_deleted handles the AI chat message reviewable" do+ chat_message.trash!(moderator)++ expect { reviewable.perform(moderator, :agree_and_keep_deleted) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => chat_message.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end++ it "creates no statement when agree_and_restore handles the AI chat message reviewable" do+ chat_message.trash!(moderator)++ expect {+ expect(reviewable.perform(moderator, :agree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree_and_restore handles the AI chat message reviewable" do+ chat_message.trash!(moderator)++ expect {+ expect(reviewable.perform(moderator, :disagree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree handles the AI chat message reviewable" do+ expect { expect(reviewable.perform(moderator, :disagree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when ignore handles the AI chat message reviewable" do+ expect { expect(reviewable.perform(moderator, :ignore)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when agree_and_suspend handles the AI chat message reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => chat_message.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when agree_and_silence handles the AI chat message reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => chat_message.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ "automated_detection" => "Yes",+ )+ end+ endendit "agree_and_keep agrees with the flag and doesn't delete the message" dodiff --git a/plugins/discourse-ai/spec/models/reviewable_ai_post_spec.rb b/plugins/discourse-ai/spec/models/reviewable_ai_post_spec.rbindex aa89d1acc11..87f4d03b442 100644--- a/plugins/discourse-ai/spec/models/reviewable_ai_post_spec.rb+++ b/plugins/discourse-ai/spec/models/reviewable_ai_post_spec.rb@@ -275,5 +275,299 @@ describe ReviewableAiPost doexpect(result.transition_to).to eq :ignoredend++ context "when the dsa_reporting_enabled site setting is enabled" do+ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when agree_and_keep handles the AI post reviewable" do+ expect { expect(reviewable.perform(admin, :agree_and_keep)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when agree_and_hide handles the AI post reviewable" do+ expect { reviewable.perform(admin, :agree_and_hide) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when agree_and_keep_hidden handles the AI post reviewable" do+ target.update!(hidden: true)++ expect { reviewable.perform(admin, :agree_and_keep_hidden) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ "automated_detection" => "Yes",+ )+ end++ it "creates no statement when agree_and_restore handles the AI post reviewable" do+ target.update!(user_deleted: true)++ expect {+ expect(reviewable.perform(admin, :agree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree handles the AI post reviewable" do+ expect { expect(reviewable.perform(admin, :disagree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when disagree_and_restore handles the AI post reviewable" do+ target.update!(hidden: true)++ expect {+ expect(reviewable.perform(admin, :disagree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when ignore handles the AI post reviewable" do+ expect { expect(reviewable.perform(admin, :ignore)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when delete_and_agree handles the AI post reviewable" do+ expect { reviewable.perform(admin, :delete_and_agree) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when delete_and_ignore handles the AI post reviewable" do+ expect { reviewable.perform(admin, :delete_and_ignore) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when agree_and_suspend handles the AI post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => target.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when agree_and_silence handles the AI post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => target.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when delete_user handles the AI post reviewable" do+ content_date = reviewable.target_created_by.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when delete_user_block handles the AI post reviewable" do+ content_date = reviewable.target_created_by.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user_block) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when delete_and_agree_replies handles the AI post reviewable" do+ reply =+ Fabricate(+ :post,+ topic: target.topic,+ reply_to_post_number: target.post_number,+ created_at: target.created_at,+ )+ PostReply.create!(post_id: target.id, reply_post_id: reply.id)+ target.update!(reply_count: 1)++ expect { reviewable.perform(admin, :delete_and_agree_replies) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end++ it "records a statement when delete_and_ignore_replies handles the AI post reviewable" do+ reply =+ Fabricate(+ :post,+ topic: target.topic,+ reply_to_post_number: target.post_number,+ created_at: target.created_at,+ )+ PostReply.create!(post_id: target.id, reply_post_id: reply.id)+ target.update!(reply_count: 1)++ expect { reviewable.perform(admin, :delete_and_ignore_replies) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_detection" => "Yes",+ )+ end+ endendenddiff --git a/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rbindex 7a547443168..16e1c60487b 100644--- a/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb+++ b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb@@ -517,4 +517,509 @@ RSpec.describe ReviewableAiToolAction doexpect { reviewable.perform(bot_user, :reject) }.to raise_error(Discourse::InvalidAccess)endend++ describe "#perform" do+ context "when the dsa_reporting_enabled site setting is enabled" do+ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "records a statement when an AI tool reviewable closes a topic" do+ post = Fabricate(:post, topic:)+ tool_action =+ create_tool_action(+ tool_name: "close_topic",+ params: {+ topic_id: topic.id,+ closed: true,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED"],+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "creates no statement when an AI tool reviewable reopens a topic" do+ post = Fabricate(:post, topic:)+ topic.update!(closed: true)+ tool_action =+ create_tool_action(+ tool_name: "close_topic",+ params: {+ topic_id: topic.id,+ closed: false,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when an AI tool reviewable unlists a topic" do+ post = Fabricate(:post, topic:)+ tool_action =+ create_tool_action(+ tool_name: "unlist_topic",+ params: {+ topic_id: topic.id,+ unlisted: true,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DEMOTED"],+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "creates no statement when an AI tool reviewable lists a topic" do+ post = Fabricate(:post, topic:)+ topic.update!(visible: false)+ tool_action =+ create_tool_action(+ tool_name: "unlist_topic",+ params: {+ topic_id: topic.id,+ unlisted: false,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when an AI tool reviewable deletes a topic" do+ post = Fabricate(:post, topic:)+ tool_action =+ create_tool_action(+ tool_name: "delete_topic",+ params: {+ topic_id: topic.id,+ deleted: true,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "creates no statement when an AI tool reviewable restores a topic" do+ post = Fabricate(:post, topic:)+ topic.trash!(admin)+ tool_action =+ create_tool_action(+ tool_name: "delete_topic",+ params: {+ topic_id: topic.id,+ deleted: false,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when an AI tool reviewable locks a post" do+ post = Fabricate(:post)+ tool_action =+ create_tool_action(+ tool_name: "lock_post",+ params: {+ post_id: post.id,+ locked: true,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED"],+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "creates no statement when an AI tool reviewable unlocks a post" do+ post = Fabricate(:post)+ PostLocker.new(post, admin).lock+ tool_action =+ create_tool_action(+ tool_name: "lock_post",+ params: {+ post_id: post.id,+ locked: false,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a partly automated flagged post decision when a moderator approves an AI tool reviewable that hides it" do+ flagged = Fabricate(:reviewable_flagged_post)+ tool_action =+ create_tool_action(+ tool_name: "perform_reviewable_action",+ params: {+ reviewable_id: flagged.id,+ action_id: "agree_and_hide",+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: flagged.id)++ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => flagged.target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "records a statement when an AI tool reviewable enables slow mode" do+ post = Fabricate(:post, topic:)+ tool_action =+ create_tool_action(+ tool_name: "set_slow_mode",+ params: {+ topic_id: topic.id,+ slow_mode_seconds: 60,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_INTERACTION_RESTRICTED"],+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "creates no statement when an AI tool reviewable disables slow mode" do+ post = Fabricate(:post, topic:)+ topic.update!(slow_mode_seconds: 60)+ tool_action =+ create_tool_action(+ tool_name: "set_slow_mode",+ params: {+ topic_id: topic.id,+ slow_mode_seconds: 0,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when an AI tool reviewable edits a post" do+ post = Fabricate(:post)+ tool_action =+ create_tool_action(+ tool_name: "edit_post",+ params: {+ post_id: post.id,+ raw: "Corrected content for this moderated post.",+ edit_reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_OTHER"],+ "decision_visibility_other" => "Content edited",+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "records a statement when an AI tool reviewable suspends a user" do+ user = Fabricate(:user)+ tool_action =+ create_tool_action(+ tool_name: "suspend_user",+ params: {+ username: user.username,+ duration_days: 7,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 7.days.from_now.to_date.iso8601,+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "records a statement when an AI tool reviewable silences a user" do+ user = Fabricate(:user)+ tool_action =+ create_tool_action(+ tool_name: "silence_user",+ params: {+ username: user.username,+ duration_days: 7,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 7.days.from_now.to_date.iso8601,+ "automated_decision" => "AUTOMATED_DECISION_PARTIALLY",+ )+ end++ it "creates no statement when an AI tool reviewable moves a topic to a private category" do+ post = Fabricate(:post, topic:)+ category = Fabricate(:private_category, group: Fabricate(:group))+ tool_action =+ create_tool_action(+ tool_name: "change_topic_category",+ params: {+ topic_id: topic.id,+ category_id: category.id,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when an AI tool reviewable moves a post to a private topic" do+ Fabricate(:post, topic:)+ post = Fabricate(:post, topic:)+ destination =+ Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))+ Fabricate(:post, topic: destination)+ tool_action =+ create_tool_action(+ tool_name: "move_posts",+ params: {+ topic_id: topic.id,+ post_ids: [post.id],+ destination_topic_id: destination.id,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when an AI tool reviewable moves a topic to a public category" do+ post = Fabricate(:post, topic:)+ category = Fabricate(:category)+ tool_action =+ create_tool_action(+ tool_name: "change_topic_category",+ params: {+ topic_id: topic.id,+ category_id: category.id,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when an AI tool reviewable moves a post to a public topic" do+ Fabricate(:post, topic:)+ post = Fabricate(:post, topic:)+ destination = Fabricate(:topic)+ Fabricate(:post, topic: destination)+ tool_action =+ create_tool_action(+ tool_name: "move_posts",+ params: {+ topic_id: topic.id,+ post_ids: [post.id],+ destination_topic_id: destination.id,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when an AI tool reviewable creates a category" do+ tool_action =+ create_tool_action(+ tool_name: "create_category",+ params: {+ name: "Bug reports",+ reason: "Collect reports",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when a moderator rejects an AI tool reviewable" do+ tool_action =+ create_tool_action(+ tool_name: "close_topic",+ params: {+ topic_id: topic.id,+ closed: true,+ reason: "Moderation",+ },+ )+ reviewable = create_reviewable(tool_action)++ expect { expect(reviewable.perform(admin, :reject)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end+ end+ endenddiff --git a/plugins/discourse-ai/spec/models/reviewable_flagged_post_spec.rb b/plugins/discourse-ai/spec/models/reviewable_flagged_post_spec.rbnew file mode 100644index 00000000000..38bfe714e6e--- /dev/null+++ b/plugins/discourse-ai/spec/models/reviewable_flagged_post_spec.rb@@ -0,0 +1,39 @@+# frozen_string_literal: true++RSpec.describe ReviewableFlaggedPost do+ describe "#perform" do+ before do+ enable_current_plugin+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "records automatic detection when a moderator deletes a post queued for review by AI triage" do+ reviewable = Fabricate(:reviewable_flagged_post)+ post = reviewable.target+ reviewable.add_score(+ Discourse.system_user,+ ReviewableScore.types[:spam],+ context: "#{DiscourseAi::Automation::TRIAGE_AUTOMATION_SCORE_CONTEXT_PREFIX}123",+ )++ expect { reviewable.perform(Fabricate(:admin), :delete_and_agree) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end+ end+enddiff --git a/plugins/discourse-ai/spec/models/reviewable_post_spec.rb b/plugins/discourse-ai/spec/models/reviewable_post_spec.rbnew file mode 100644index 00000000000..8a80a1e1c4d--- /dev/null+++ b/plugins/discourse-ai/spec/models/reviewable_post_spec.rb@@ -0,0 +1,44 @@+# frozen_string_literal: true++RSpec.describe ReviewablePost do+ describe "#perform" do+ before do+ enable_current_plugin+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "records automatic detection when a moderator deletes a post queued for review by AI triage" do+ post = Fabricate(:post)+ reviewable =+ ReviewablePost.needs_review!(+ target: post,+ created_by: Discourse.system_user,+ reviewable_by_moderator: true,+ )+ reviewable.add_score(+ Discourse.system_user,+ ReviewableScore.types[:needs_approval],+ context: "#{DiscourseAi::Automation::TRIAGE_AUTOMATION_SCORE_CONTEXT_PREFIX}123",+ )++ expect { reviewable.perform(Fabricate(:admin), :reject_and_delete) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end+ end+enddiff --git a/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb b/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rbindex 98f23ed2f32..f21e983c296 100644--- a/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb+++ b/plugins/discourse-post-voting/app/models/reviewable_post_voting_comment.rb@@ -12,7 +12,11 @@ class ReviewablePostVotingComment < Reviewableenddef self.action_aliases- { agree_and_keep_hidden: :agree_and_delete, delete_and_agree: :agree_and_delete }+ {+ agree_and_keep_hidden: :agree_and_delete,+ agree_and_keep_deleted: :agree_and_keep_comment,+ delete_and_agree: :agree_and_delete,+ }enddef flagged_by_user_ids@@ -127,6 +131,46 @@ class ReviewablePostVotingComment < Reviewableprivate+ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :agree_and_delete, :delete_and_agree, :delete_and_ignore, :agree_and_keep_deleted,+ :agree_and_keep_hidden+ {+ decision_visibility: ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ content_type: ["CONTENT_TYPE_TEXT"],+ content_date: comment.created_at.to_date.iso8601,+ automated_detection: "No",+ automated_decision:,+ }+ when :agree_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection: "No",+ automated_decision:,+ }.compact+ when :agree_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection: "No",+ automated_decision:,+ }.compact+ end+ end+def agreeyield if block_given?create_result(:success, :approved) do |result|diff --git a/plugins/discourse-post-voting/spec/models/reviewable_post_voting_comment_spec.rb b/plugins/discourse-post-voting/spec/models/reviewable_post_voting_comment_spec.rbindex 6dfc8f6c6e2..ec965911802 100644--- a/plugins/discourse-post-voting/spec/models/reviewable_post_voting_comment_spec.rb+++ b/plugins/discourse-post-voting/spec/models/reviewable_post_voting_comment_spec.rb@@ -51,6 +51,169 @@ RSpec.describe ReviewablePostVotingComment, type: :model doexpect(comment.reload.deleted_at).to be_nilexpect(reviewable.reload).to be_pendingend++ context "when the dsa_reporting_enabled site setting is enabled" do+ before do+ reviewable.update!(target_created_by: comment.user)+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when agree_and_keep_comment handles the post voting comment reviewable" do+ expect {+ expect(reviewable.perform(moderator, :agree_and_keep_comment)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records a statement when agree_and_delete handles the post voting comment reviewable" do+ expect { reviewable.perform(moderator, :agree_and_delete) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => comment.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when delete_and_agree handles the post voting comment reviewable" do+ expect { reviewable.perform(moderator, :delete_and_agree) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => comment.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when agree_and_keep_deleted handles the post voting comment reviewable" do+ comment.trash!(moderator)++ expect { reviewable.perform(moderator, :agree_and_keep_deleted) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => comment.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "creates no statement when agree_and_restore handles the post voting comment reviewable" do+ comment.trash!(moderator)++ expect {+ expect(reviewable.perform(moderator, :agree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree_and_restore handles the post voting comment reviewable" do+ comment.trash!(moderator)++ expect {+ expect(reviewable.perform(moderator, :disagree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree handles the post voting comment reviewable" do+ expect { expect(reviewable.perform(moderator, :disagree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when ignore handles the post voting comment reviewable" do+ expect { expect(reviewable.perform(moderator, :ignore)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when agree_and_suspend handles the post voting comment reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => comment.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_silence handles the post voting comment reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => comment.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end+ endendit "agree_and_keep agrees with the flag and doesn't delete the comment" dodiff --git a/plugins/voice/app/models/reviewable_voice_user.rb b/plugins/voice/app/models/reviewable_voice_user.rbindex f75adc8330b..fb2050a44b5 100644--- a/plugins/voice/app/models/reviewable_voice_user.rb+++ b/plugins/voice/app/models/reviewable_voice_user.rb@@ -85,6 +85,39 @@ class ReviewableVoiceUser < Reviewableresult.update_flag_stats = { status: :ignored, user_ids: flagged_by_user_ids }endend++ private++ def dsa_statement_attributes(action_id:, result:, automated_detection:, automated_decision:)+ case action_id+ when :agree_and_suspend+ user = target_created_by.reload++ {+ decision_account: "DECISION_ACCOUNT_SUSPENDED",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_account_restriction:+ (user.suspended_till.to_date.iso8601 unless user.suspended_forever?),+ automated_detection: "No",+ automated_decision:,+ }.compact+ when :agree_and_silence+ user = target_created_by.reload++ {+ decision_provision: "DECISION_PROVISION_PARTIAL_SUSPENSION",+ content_type: ["CONTENT_TYPE_OTHER"],+ content_type_other: "User account",+ content_date: user.created_at.to_date.iso8601,+ end_date_service_restriction:+ (user.silenced_till.to_date.iso8601 unless user.silenced_forever?),+ automated_detection: "No",+ automated_decision:,+ }.compact+ end+ endend# == Schema Informationdiff --git a/plugins/voice/spec/models/reviewable_voice_user_spec.rb b/plugins/voice/spec/models/reviewable_voice_user_spec.rbindex 86b69df2509..61b95677835 100644--- a/plugins/voice/spec/models/reviewable_voice_user_spec.rb+++ b/plugins/voice/spec/models/reviewable_voice_user_spec.rb@@ -111,5 +111,86 @@ RSpec.describe ReviewableVoiceUser doexpect(reviewable.reload).to be_ignoredexpect(reviewable.reviewable_scores.first).to be_ignoredend++ context "when the dsa_reporting_enabled site setting is enabled" do+ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when agree_and_keep handles the voice user reviewable" do+ expect {+ expect(reviewable.perform(moderator, :agree_and_keep)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree handles the voice user reviewable" do+ expect { expect(reviewable.perform(moderator, :disagree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when ignore handles the voice user reviewable" do+ expect { expect(reviewable.perform(moderator, :ignore)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when agree_and_suspend handles the voice user reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => flagged_user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_silence handles the voice user reviewable" do+ expect {+ reviewable.perform(+ moderator,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => flagged_user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end+ endendenddiff --git a/spec/models/reviewable_flagged_post_spec.rb b/spec/models/reviewable_flagged_post_spec.rbindex 28f01eb615c..9278bca60c0 100644--- a/spec/models/reviewable_flagged_post_spec.rb+++ b/spec/models/reviewable_flagged_post_spec.rb@@ -573,6 +573,568 @@ RSpec.describe ReviewableFlaggedPost, type: :model doexpect(flagged.reload).to be_pendingendend++ it "creates no statement for a flagged post deletion when the dsa_reporting_enabled site setting is disabled" do+ flagged = Fabricate(:reviewable_flagged_post)+ SiteSetting.dsa_reporting_enabled = false++ expect { expect(flagged.perform(moderator, :delete_and_agree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ context "when the dsa_reporting_enabled site setting is enabled" do+ fab!(:admin)+ let(:post) { Fabricate(:post) }+ let!(:reviewable) { PostActionCreator.spam(user, post).reviewable }++ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when unsilence_user handles the flagged post reviewable" do+ post.update!(hidden: true, user_deleted: true)+ UserSilencer.silence(post.user, admin, post_id: post.id)++ expect { expect(reviewable.perform(admin, :unsilence_user)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when unsilence_user_and_ignore handles the flagged post reviewable" do+ post.update!(hidden: true, user_deleted: true)+ UserSilencer.silence(post.user, admin, post_id: post.id)++ expect {+ expect(reviewable.perform(admin, :unsilence_user_and_ignore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when a flagged post suspension fails" do+ expect {+ expect(+ reviewable.perform(admin, :agree_and_suspend, reason: "Spam", suspend_until: "invalid"),+ ).not_to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when agree_and_keep handles the flagged post reviewable" do+ expect { expect(reviewable.perform(admin, :agree_and_keep)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when agree_and_hide handles the flagged post reviewable" do+ expect { reviewable.perform(admin, :agree_and_hide) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "records a fully automated decision when a bot agrees with a flagged post and hides it" do+ bot = Fabricate(:bot)++ expect { reviewable.perform(bot, :agree_and_hide, guardian: admin.guardian) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_FULLY",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "records an indefinite restriction when a moderator agrees with a flagged post and permanently suspends its author" do+ expect {+ reviewable.perform(+ admin,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 1000.years.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ )+ end++ it "records an indefinite restriction when a moderator agrees with a flagged post and permanently silences its author" do+ expect {+ reviewable.perform(+ admin,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 1000.years.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ )+ end++ it "records a statement when agree_and_keep_hidden handles the flagged post reviewable" do+ post.update!(hidden: true)++ expect { reviewable.perform(admin, :agree_and_keep_hidden) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "records a statement when agree_and_keep_deleted handles the flagged post reviewable" do+ post.trash!(admin)++ expect { reviewable.perform(admin, :agree_and_keep_deleted) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "creates no statement when agree_and_restore handles the flagged post reviewable" do+ post.update!(user_deleted: true)++ expect {+ expect(reviewable.perform(admin, :agree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree handles the flagged post reviewable" do+ expect { expect(reviewable.perform(admin, :disagree)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when disagree_and_restore handles the flagged post reviewable" do+ post.update!(hidden: true)++ expect {+ expect(reviewable.perform(admin, :disagree_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when disagree_and_keep_deleted handles the flagged post reviewable" do+ post.trash!(admin)++ expect {+ expect(reviewable.perform(admin, :disagree_and_keep_deleted)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when ignore handles the flagged post reviewable" do+ expect { expect(reviewable.perform(admin, :ignore)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when ignore_and_do_nothing handles the flagged post reviewable" do+ expect {+ expect(reviewable.perform(admin, :ignore_and_do_nothing)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records a statement when delete_and_agree handles the flagged post reviewable" do+ expect { reviewable.perform(admin, :delete_and_agree) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when delete_and_ignore handles the flagged post reviewable" do+ expect { reviewable.perform(admin, :delete_and_ignore) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when agree_and_edit handles the flagged post reviewable" do+ post.revise(admin, raw: "The moderator corrected this flagged post.")++ expect {+ reviewable.perform(admin, :agree_and_edit, post_version: post.reload.version)+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_OTHER"],+ "decision_visibility_other" => "Content edited",+ )+ end++ it "records the flagged post author suspension when additional users are suspended" do+ author = post.user+ author.update!(created_at: 4.days.ago)+ same_date_user = Fabricate(:user, created_at: author.created_at)+ other_date_user = Fabricate(:user, created_at: 2.days.ago)++ expect {+ reviewable.perform(+ admin,+ :agree_and_suspend,+ reason: "Related accounts",+ suspend_until: 2.days.from_now,+ other_user_ids: [same_date_user.id, other_date_user.id],+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ expect(author.reload).to be_suspended+ expect(same_date_user.reload).to be_suspended+ expect(other_date_user.reload).to be_suspended++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_suspend handles the flagged post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_suspend handles a hidden flagged post reviewable" do+ post.update!(hidden: true)++ expect {+ reviewable.perform(+ admin,+ :agree_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_silence handles the flagged post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when agree_and_silence handles a deleted flagged post reviewable" do+ post.trash!(admin)++ expect {+ reviewable.perform(+ admin,+ :agree_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when delete_user handles the flagged post reviewable" do+ content_date = reviewable.target_created_by.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ )+ end++ it "records a statement when delete_user_block handles the flagged post reviewable" do+ content_date = reviewable.target_created_by.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user_block) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ )+ end++ it "records a statement when delete_and_agree_replies handles the flagged post reviewable" do+ reply =+ Fabricate(+ :post,+ topic: post.topic,+ reply_to_post_number: post.post_number,+ created_at: post.created_at,+ )+ PostReply.create!(post_id: post.id, reply_post_id: reply.id)+ post.update!(reply_count: 1)++ expect { reviewable.perform(admin, :delete_and_agree_replies) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when delete_and_ignore_replies handles the flagged post reviewable" do+ reply =+ Fabricate(+ :post,+ topic: post.topic,+ reply_to_post_number: post.post_number,+ created_at: post.created_at,+ )+ PostReply.create!(post_id: post.id, reply_post_id: reply.id)+ post.update!(reply_count: 1)++ expect { reviewable.perform(admin, :delete_and_ignore_replies) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end+ endenddescribe "#perform_delete_and_agree" dodiff --git a/spec/models/reviewable_post_spec.rb b/spec/models/reviewable_post_spec.rbindex 830dbd3a68e..e92dbb6ebfc 100644--- a/spec/models/reviewable_post_spec.rb+++ b/spec/models/reviewable_post_spec.rb@@ -177,4 +177,137 @@ RSpec.describe ReviewablePost doendendend++ describe "#perform" do+ context "when the dsa_reporting_enabled site setting is enabled" do+ let(:post) { Fabricate(:post) }+ let(:reviewable) { described_class.needs_review!(target: post, created_by: admin) }++ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when approve handles the post reviewable" do+ expect { expect(reviewable.perform(admin, :approve)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "records a statement when reject_and_delete handles the post reviewable" do+ expect { reviewable.perform(admin, :reject_and_delete) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "records a statement when reject_and_keep_deleted handles the post reviewable" do+ post.trash!(admin)++ expect { reviewable.perform(admin, :reject_and_keep_deleted) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => post.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end++ it "creates no statement when approve_and_restore handles the post reviewable" do+ post.trash!(admin)++ expect {+ expect(reviewable.perform(admin, :approve_and_restore)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when approve_and_unhide handles the post reviewable" do+ post.update!(hidden: true)++ expect {+ expect(reviewable.perform(admin, :approve_and_unhide)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records a statement when reject_and_suspend handles the post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :reject_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when reject_and_silence handles the post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :reject_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => post.user.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end+ end+ endenddiff --git a/spec/models/reviewable_queued_post_spec.rb b/spec/models/reviewable_queued_post_spec.rbindex 79797f22173..39071c0f61a 100644--- a/spec/models/reviewable_queued_post_spec.rb+++ b/spec/models/reviewable_queued_post_spec.rb@@ -615,4 +615,243 @@ RSpec.describe ReviewableQueuedPost, type: :model doexpect(reviewable.target_created_by.reload).to be_silencedendend++ describe "#perform" do+ context "when the dsa_reporting_enabled site setting is enabled" do+ let(:reviewable) { Fabricate(:reviewable_queued_post) }++ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when approve_post handles the queued post reviewable" do+ expect { expect(reviewable.perform(admin, :approve_post)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when approve_post_closed handles the queued post reviewable" do+ reviewable.topic.update!(closed: true)++ expect {+ expect(reviewable.perform(admin, :approve_post_closed)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records automatic detection when a moderator rejects a queued post caught by a watched word" do+ reviewable.add_score(+ Discourse.system_user,+ ReviewableScore.types[:needs_approval],+ reason: "watched_word",+ )++ expect { reviewable.perform(admin, :reject_post) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => reviewable.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "records a statement when revise_and_reject_post handles the queued post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :revise_and_reject_post,+ revise_reason: "Please clarify the topic",+ revise_feedback: "Add more detail.",+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => reviewable.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "creates no statement when the author withdraws their queued post reviewable" do+ expect {+ expect(reviewable.perform(reviewable.target_created_by, :delete)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "creates no statement when an API administrator deletes another author's queued post reviewable" do+ request = ActionDispatch::Request.new(Auth::DefaultCurrentUserProvider::API_KEY_ENV => true)+ guardian = Guardian.new(admin, request)++ expect {+ expect(reviewable.perform(admin, :delete, guardian:)).to be_success+ }.not_to change(DsaStatementOfRecord, :count)+ end++ it "records a statement when reject_and_suspend handles the queued post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :reject_and_suspend,+ reason: "Spam",+ suspend_until: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => reviewable.target_created_by.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when reject_and_silence handles the queued post reviewable" do+ expect {+ reviewable.perform(+ admin,+ :reject_and_silence,+ reason: "Spam",+ silenced_till: 2.days.from_now,+ )+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_type_other" => "User account",+ "content_date" => reviewable.target_created_by.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records account and queued post statements when delete_user handles the queued post reviewable" do+ content_date = reviewable.target_created_by.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(2)++ statement =+ DsaStatementOfRecord.where(reviewable_id: reviewable.id).find_by!(+ "payload ->> 'decision_account' = ?",+ "DECISION_ACCOUNT_TERMINATED",+ )++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ )++ post_statement =+ DsaStatementOfRecord.where(reviewable_id: reviewable.id).find_by!(+ "payload -> 'decision_visibility' @> ?",+ '["DECISION_VISIBILITY_CONTENT_DISABLED"]',+ )++ expect(post_statement).to be_pending+ expect(post_statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => reviewable.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end++ it "records account and queued post statements when delete_user_block handles the queued post reviewable" do+ content_date = reviewable.target_created_by.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user_block) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(2)++ statement =+ DsaStatementOfRecord.where(reviewable_id: reviewable.id).find_by!(+ "payload ->> 'decision_account' = ?",+ "DECISION_ACCOUNT_TERMINATED",+ )++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ )++ post_statement =+ DsaStatementOfRecord.where(reviewable_id: reviewable.id).find_by!(+ "payload -> 'decision_visibility' @> ?",+ '["DECISION_VISIBILITY_CONTENT_DISABLED"]',+ )++ expect(post_statement).to be_pending+ expect(post_statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "No",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_TEXT"],+ "content_date" => reviewable.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_DISABLED"],+ )+ end+ end+ endenddiff --git a/spec/models/reviewable_user_spec.rb b/spec/models/reviewable_user_spec.rbindex 0d64d39d76f..f0fee6e3cbb 100644--- a/spec/models/reviewable_user_spec.rb+++ b/spec/models/reviewable_user_spec.rb@@ -487,6 +487,161 @@ RSpec.describe ReviewableUser, type: :model doexpect(reviewable.target).to be_silencedendend++ context "when the dsa_reporting_enabled site setting is enabled" do+ let(:reviewable) { Fabricate(:suspect_user_reviewable) }++ before do+ freeze_time+ SiteSetting.dsa_reporting_enabled = true+ end++ it "creates no statement when approve_user handles the user reviewable" do+ expect { expect(reviewable.perform(admin, :approve_user)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no statement when delete_user handles a user reviewable whose account was already deleted" do+ reviewable.target.destroy!+ reviewable.reload++ expect { expect(reviewable.perform(admin, :delete_user)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )+ end++ it "creates no termination statement when rejecting a user reviewable preserves the author's posts and account" do+ author = reviewable.target+ Fabricate(:post, user: author)++ expect { expect(reviewable.perform(admin, :delete_user)).to be_success }.not_to change(+ DsaStatementOfRecord,+ :count,+ )++ expect(author.reload).not_to be_approved+ expect(reviewable.reload).to be_rejected+ end++ it "records a statement when delete_user handles the user reviewable" do+ content_date = reviewable.target.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ )+ end++ it "records a statement when delete_user_block handles the user reviewable" do+ content_date = reviewable.target.created_at.to_date.iso8601++ expect { reviewable.perform(admin, :delete_user_block) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => content_date,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_TERMINATED",+ )+ end++ it "records a statement when silence_user handles the user reviewable" do+ expect {+ reviewable.perform(admin, :silence_user, reason: "Spam", silenced_till: 2.days.from_now)+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => reviewable.target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_provision" => "DECISION_PROVISION_PARTIAL_SUSPENSION",+ "end_date_service_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when suspend_user handles the user reviewable" do+ expect {+ reviewable.perform(admin, :suspend_user, reason: "Spam", suspend_until: 2.days.from_now)+ }.to change(DsaStatementOfRecord, :count).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_OTHER"],+ "content_date" => reviewable.target.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "content_type_other" => "User account",+ "decision_account" => "DECISION_ACCOUNT_SUSPENDED",+ "end_date_account_restriction" => 2.days.from_now.to_date.iso8601,+ )+ end++ it "records a statement when remove_avatar handles the user reviewable" do+ avatar = Fabricate(:upload, created_at: 3.days.ago)+ reviewable.target.update!(uploaded_avatar: avatar)++ expect { reviewable.perform(admin, :remove_avatar) }.to change(+ DsaStatementOfRecord,+ :count,+ ).by(1)++ statement = DsaStatementOfRecord.find_by!(reviewable_id: reviewable.id)++ expect(statement).to be_pending+ expect(statement.payload).to eq(+ "territorial_scope" => DsaStatementOfRecord::TERRITORIAL_SCOPE,+ "source_type" => "SOURCE_VOLUNTARY",+ "automated_detection" => "Yes",+ "automated_decision" => "AUTOMATED_DECISION_NOT_AUTOMATED",+ "content_type" => ["CONTENT_TYPE_IMAGE"],+ "content_date" => avatar.created_at.to_date.iso8601,+ "application_date" => Date.current.iso8601,+ "decision_visibility" => ["DECISION_VISIBILITY_CONTENT_REMOVED"],+ )+ end+ endenddescribe "changing must_approve_users" do