From 504f46ba113c8abac63cc049e41e0a911e322f56 Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Tue, 10 Dec 2024 11:02:49 +0100
Subject: [PATCH] UX: clarify the need for authorized extension (#346)

When checking `attach_csv` in the `Schedule a PM with Data Explorer results`
 automation script, `csv` has to be added to the list of authorized extensions in the site settings. This will now raise an error if it's not the case.
---
 config/locales/server.en.yml                  |  1 +
 plugin.rb                                     | 13 ++++++++++-
 .../recurring_data_explorer_result_pm_spec.rb | 23 +++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 7c92a5e..083ef16 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -6,6 +6,7 @@ en:
       recurring_data_explorer_result_pm:
         title: Schedule a PM with Data Explorer results
         description: Get scheduled reports sent to your messages
+        no_csv_allowed: "When using `attach_csv` field, `csv` must be added to the list of authorized extensions in the site settings."
       recurring_data_explorer_result_topic:
         title: Schedule a post in a topic with Data Explorer results
         description: Get scheduled reports posted to a specific topic
diff --git a/plugin.rb b/plugin.rb
index 7f1ec0a..df79050 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -82,7 +82,18 @@ after_initialize do
         field :query_id, component: :choices, required: true, extra: { content: queries }
         field :query_params, component: :"key-value", accepts_placeholders: true
         field :skip_empty, component: :boolean
-        field :attach_csv, component: :boolean
+        field :attach_csv,
+              component: :boolean,
+              validator: ->(attach_csv) do
+                return if !attach_csv
+
+                extensions = SiteSetting.authorized_extensions.split("|")
+                if (extensions & %w[csv *]).empty?
+                  I18n.t(
+                    "discourse_automation.scriptables.recurring_data_explorer_result_pm.no_csv_allowed",
+                  )
+                end
+              end
 
         version 1
         triggerables [:recurring]
diff --git a/spec/automation/recurring_data_explorer_result_pm_spec.rb b/spec/automation/recurring_data_explorer_result_pm_spec.rb
index 39aefff..775d03e 100644
--- a/spec/automation/recurring_data_explorer_result_pm_spec.rb
+++ b/spec/automation/recurring_data_explorer_result_pm_spec.rb
@@ -126,4 +126,27 @@ describe "RecurringDataExplorerResultPM" do
       )
     end
   end
+
+  context "when using attach_csv" do
+    it "requires csv to be in authorized extensions" do
+      SiteSetting.authorized_extensions = "pdf|txt"
+
+      expect { automation.upsert_field!("attach_csv", "boolean", { value: true }) }.to raise_error(
+        ActiveRecord::RecordInvalid,
+        /#{I18n.t("discourse_automation.scriptables.recurring_data_explorer_result_pm.no_csv_allowed")}/,
+      )
+
+      SiteSetting.authorized_extensions = "pdf|txt|csv"
+
+      expect {
+        automation.upsert_field!("attach_csv", "boolean", { value: true })
+      }.to_not raise_error
+
+      SiteSetting.authorized_extensions = "*"
+
+      expect {
+        automation.upsert_field!("attach_csv", "boolean", { value: true })
+      }.to_not raise_error
+    end
+  end
 end