Skip to content

A platform for community discussion. Free, open, simple.

DEV: Remove defunct "Encrypts PM if available" option from automation (#36973)

Régis Hanola94a001c64d0
diff --git a/plugins/automation/admin/assets/javascripts/admin/components/fields/da-pms-field.gjs b/plugins/automation/admin/assets/javascripts/admin/components/fields/da-pms-field.gjs
index aab1c5c8056..e76be9c6dfd 100644
--- a/plugins/automation/admin/assets/javascripts/admin/components/fields/da-pms-field.gjs
+++ b/plugins/automation/admin/assets/javascripts/admin/components/fields/da-pms-field.gjs
@@ -19,10 +19,6 @@ export default class PmsField extends BaseField {
noPmCreatedLabel = i18n("discourse_automation.fields.pms.no_pm_created");
- prefersEncryptLabel = i18n(
- "discourse_automation.fields.pms.prefers_encrypt.label"
- );
-
delayLabel = i18n("discourse_automation.fields.pms.delay.label");
pmTitleLabel = i18n("discourse_automation.fields.pms.title.label");
@@ -104,21 +100,6 @@ export default class PmsField extends BaseField {
</div>
</div>
- <div class="control-group">
- <label class="control-label">
- {{this.prefersEncryptLabel}}
- </label>
-
- <div class="controls">
- <Input
- @type="checkbox"
- class="pm-prefers-encrypt"
- @checked={{pm.prefers_encrypt}}
- {{on "click" (fn this.prefersEncrypt pm)}}
- disabled={{@field.isDisabled}}
- />
- </div>
- </div>
<section class="actions">
<DButton
@icon="trash-can"
@@ -173,16 +154,10 @@ export default class PmsField extends BaseField {
title: "",
raw: "",
delay: 0,
- prefers_encrypt: true,
})
);
}
- @action
- prefersEncrypt(pm, event) {
- pm.prefers_encrypt = event.target.checked;
- }
-
@action
mutPmTitle(pm, event) {
pm.title = event.target.value;
diff --git a/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb b/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb
index ae38059737c..031d6c7c06d 100644
--- a/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb
+++ b/plugins/automation/app/jobs/scheduled/discourse_automation/tracker.rb
@@ -43,7 +43,6 @@ module Jobs
::DiscourseAutomation::Scriptable::Utils.send_pm(
pending_pm.attributes.slice("target_usernames", "title", "raw"),
sender: pending_pm.sender,
- prefers_encrypt: pending_pm.prefers_encrypt,
)
pending_pm.destroy!
diff --git a/plugins/automation/app/models/discourse_automation/field.rb b/plugins/automation/app/models/discourse_automation/field.rb
index 5f1c78df1bc..49c1ab74b60 100644
--- a/plugins/automation/app/models/discourse_automation/field.rb
+++ b/plugins/automation/app/models/discourse_automation/field.rb
@@ -222,9 +222,6 @@ module DiscourseAutomation
"delay" => {
"type" => "integer",
},
- "prefers_encrypt" => {
- "type" => "boolean",
- },
},
},
],
diff --git a/plugins/automation/app/models/discourse_automation/pending_pm.rb b/plugins/automation/app/models/discourse_automation/pending_pm.rb
index aa87d302a1e..ae31e603d3f 100644
--- a/plugins/automation/app/models/discourse_automation/pending_pm.rb
+++ b/plugins/automation/app/models/discourse_automation/pending_pm.rb
@@ -2,6 +2,9 @@
module DiscourseAutomation
class PendingPm < ActiveRecord::Base
+ # TODO: Remove after 20260106102330_drop_prefers_encrypt_from_pending_pms has been promoted
+ self.ignored_columns = %w[prefers_encrypt]
+
self.table_name = "discourse_automation_pending_pms"
belongs_to :automation, class_name: "DiscourseAutomation::Automation"
@@ -13,13 +16,12 @@ end
# Table name: discourse_automation_pending_pms
#
# id :bigint not null, primary key
-# target_usernames :string is an Array
+# execute_at :datetime not null
+# raw :string
# sender :string
+# target_usernames :string is an Array
# title :string
-# raw :string
-# automation_id :bigint not null
-# execute_at :datetime not null
# created_at :datetime not null
# updated_at :datetime not null
-# prefers_encrypt :boolean default(FALSE), not null
+# automation_id :bigint not null
#
diff --git a/plugins/automation/config/locales/client.en.yml b/plugins/automation/config/locales/client.en.yml
index 925a74fc483..42c283829ee 100644
--- a/plugins/automation/config/locales/client.en.yml
+++ b/plugins/automation/config/locales/client.en.yml
@@ -54,8 +54,6 @@ en:
label: Body
delay:
label: Delay (minutes)
- prefers_encrypt:
- label: Encrypts PM if available
group:
label: Group
text:
diff --git a/plugins/automation/db/post_migrate/20260106102330_drop_prefers_encrypt_from_pending_pms.rb b/plugins/automation/db/post_migrate/20260106102330_drop_prefers_encrypt_from_pending_pms.rb
new file mode 100644
index 00000000000..a4499735c89
--- /dev/null
+++ b/plugins/automation/db/post_migrate/20260106102330_drop_prefers_encrypt_from_pending_pms.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class DropPrefersEncryptFromPendingPms < ActiveRecord::Migration[8.0]
+ DROPPED_COLUMNS = { discourse_automation_pending_pms: %i[prefers_encrypt] }
+
+ def up
+ DROPPED_COLUMNS.each { |table, columns| Migration::ColumnDropper.execute_drop(table, columns) }
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/automation/lib/discourse_automation/scriptable.rb b/plugins/automation/lib/discourse_automation/scriptable.rb
index d44ab0f87ae..4eaff6c9865 100644
--- a/plugins/automation/lib/discourse_automation/scriptable.rb
+++ b/plugins/automation/lib/discourse_automation/scriptable.rb
@@ -250,21 +250,13 @@ module DiscourseAutomation
"[quote=#{params.join(", ")}]\n#{post.raw.strip}\n[/quote]\n\n"
end
- def self.send_pm(
- pm,
- sender: Discourse.system_user.username,
- delay: nil,
- automation_id: nil,
- prefers_encrypt: true
- )
+ def self.send_pm(pm, sender: Discourse.system_user.username, delay: nil, automation_id: nil)
pm = pm.symbolize_keys
- prefers_encrypt = prefers_encrypt && !!defined?(EncryptedPostCreator)
if delay && delay.to_i > 0 && automation_id
pm[:execute_at] = delay.to_i.minutes.from_now
pm[:sender] = sender
pm[:automation_id] = automation_id
- pm[:prefers_encrypt] = prefers_encrypt
DiscourseAutomation::PendingPm.create!(pm)
else
sender = User.find_by(username: sender)
@@ -322,10 +314,8 @@ module DiscourseAutomation
Rails.logger.warn "[discourse-automation] Did not send PM #{pm[:title]} to all users - some do not exist: `#{non_existing_targets.join(",")}`"
end
- post_created = EncryptedPostCreator.new(sender, pm).create if prefers_encrypt
-
pm[:acting_user] = Discourse.system_user
- PostCreator.new(sender, pm).create! if !post_created
+ PostCreator.new(sender, pm).create!
end
end
end
diff --git a/plugins/automation/lib/discourse_automation/scripts/gift_exchange.rb b/plugins/automation/lib/discourse_automation/scripts/gift_exchange.rb
index a37989963ba..32c86c3fb57 100644
--- a/plugins/automation/lib/discourse_automation/scripts/gift_exchange.rb
+++ b/plugins/automation/lib/discourse_automation/scripts/gift_exchange.rb
@@ -60,7 +60,6 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::GIFT_EXCHANGE)
utils.send_pm(
{ target_usernames: Array(gifter), title: title, raw: raw },
delay: giftee_assignment_message["delay"],
- prefers_encrypt: giftee_assignment_message["prefers_encrypt"],
automation_id: automation.id,
)
end
diff --git a/plugins/automation/lib/discourse_automation/scripts/send_pms.rb b/plugins/automation/lib/discourse_automation/scripts/send_pms.rb
index 7bfc61304e6..d95456a2fc9 100644
--- a/plugins/automation/lib/discourse_automation/scripts/send_pms.rb
+++ b/plugins/automation/lib/discourse_automation/scripts/send_pms.rb
@@ -47,7 +47,6 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::SEND_PMS) do
sender: sender_username,
automation_id: automation.id,
delay: sendable["delay"],
- prefers_encrypt: !!sendable["prefers_encrypt"],
)
end
end
diff --git a/plugins/automation/spec/scripts/send_pms_spec.rb b/plugins/automation/spec/scripts/send_pms_spec.rb
index 498f2a6f6c8..de89fff6e19 100644
--- a/plugins/automation/spec/scripts/send_pms_spec.rb
+++ b/plugins/automation/spec/scripts/send_pms_spec.rb
@@ -4,158 +4,99 @@ describe "SendPms" do
fab!(:automation) do
Fabricate(:automation, script: DiscourseAutomation::Scripts::SEND_PMS, trigger: "stalled_wiki")
end
- let(:raw) { "This is a message sent to @{{receiver_username}}" }
before do
SiteSetting.discourse_automation_enabled = true
-
automation.upsert_field!("sender", "user", { value: Discourse.system_user.username })
automation.upsert_field!(
"sendable_pms",
"pms",
- { value: [{ title: "A message from {{sender_username}}", raw: raw }] },
+ {
+ value: [
+ { title: "Hello {{receiver_username}}", raw: "Message for @{{receiver_username}}" },
+ ],
+ },
)
end
- context "when run from stalled_wiki trigger" do
- fab!(:post_creator_1) { Fabricate(:user, admin: true) }
- fab!(:post_1) { Fabricate(:post, user: post_creator_1) }
+ context "with stalled_wiki trigger" do
+ fab!(:user) { Fabricate(:user, admin: true) }
+ fab!(:post) { Fabricate(:post, user: user) }
before do
automation.upsert_field!("stalled_after", "choices", { value: "PT1H" }, target: "trigger")
automation.upsert_field!("retriggered_after", "choices", { value: "PT1H" }, target: "trigger")
-
- post_1.revise(
- post_creator_1,
- { wiki: true },
- { force_new_version: true, revised_at: 2.hours.ago },
- )
+ post.revise(user, { wiki: true }, { force_new_version: true, revised_at: 2.hours.ago })
end
- it "creates expected PM" do
- expect {
- Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil)
-
- post = Post.last
- expect(post.topic.title).to eq("A message from #{Discourse.system_user.username}")
- expect(post.raw).to eq("This is a message sent to @#{post_creator_1.username}")
- expect(post.topic.topic_allowed_users.exists?(user_id: post_creator_1.id)).to eq(true)
- expect(post.topic.topic_allowed_users.exists?(user_id: Discourse.system_user.id)).to eq(
- true,
- )
- }.to change { Post.count }.by(1)
+ it "sends PM with placeholders replaced" do
+ expect { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }.to change {
+ Topic.where(archetype: Archetype.private_message).count
+ }.by(1)
+
+ pm = Topic.last
+ expect(pm.title).to eq("Hello #{user.username}")
+ expect(pm.first_post.raw).to eq("Message for @#{user.username}")
end
end
- context "when run from user_added_to_group trigger" do
- fab!(:user_1) { Fabricate(:user, refresh_auto_groups: true) }
- fab!(:tracked_group_1, :group)
+ context "with user_added_to_group trigger" do
+ fab!(:user)
+ fab!(:group)
before do
automation.update!(trigger: "user_added_to_group")
- automation.upsert_field!(
- "joined_group",
- "group",
- { value: tracked_group_1.id },
- target: "trigger",
- )
+ automation.upsert_field!("joined_group", "group", { value: group.id }, target: "trigger")
end
- it "creates expected PM" do
- expect {
- tracked_group_1.add(user_1)
-
- post = Post.last
- expect(post.topic.title).to eq("A message from #{Discourse.system_user.username}")
- expect(post.raw).to eq("This is a message sent to @#{user_1.username}")
- expect(post.topic.topic_allowed_users.exists?(user_id: user_1.id)).to eq(true)
- expect(post.topic.topic_allowed_users.exists?(user_id: Discourse.system_user.id)).to eq(
- true,
- )
- }.to change { Post.count }.by(1)
+ it "sends PM when user joins group" do
+ expect { group.add(user) }.to change {
+ Topic.where(archetype: Archetype.private_message).count
+ }.by(1)
+
+ pm = Topic.last
+ expect(pm.title).to eq("Hello #{user.username}")
+ expect(pm.allowed_users).to include(user, Discourse.system_user)
end
- context "when sent from regular user" do
- fab!(:user_2, :user)
- fab!(:user_3, :user)
- let(:raw) { "This is a general message sent" }
+ context "with custom sender" do
+ fab!(:sender) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:user_2) { Fabricate(:user, refresh_auto_groups: true) }
before do
SiteSetting.unique_posts_mins = 1
- automation.upsert_field!("sender", "user", { value: user_1.username })
+ automation.upsert_field!("sender", "user", { value: sender.username })
end
- it "creates expected PMs without validating similarity" do
- expect {
- tracked_group_1.add(user_2)
-
- post = Post.last
- expect(post.topic.title).to eq("A message from #{user_1.username}")
- expect(post.raw).to eq("This is a general message sent")
- expect(post.topic.topic_allowed_users.exists?(user_id: user_1.id)).to eq(true)
- expect(post.topic.topic_allowed_users.exists?(user_id: user_2.id)).to eq(true)
- }.to change { Post.count }.by(1)
- expect {
- tracked_group_1.add(user_3)
-
- post = Post.last
- expect(post.topic.title).to eq("A message from #{user_1.username}")
- expect(post.raw).to eq("This is a general message sent")
- expect(post.topic.topic_allowed_users.exists?(user_id: user_1.id)).to eq(true)
- expect(post.topic.topic_allowed_users.exists?(user_id: user_3.id)).to eq(true)
- }.to change { Post.count }.by(1)
+ it "bypasses similarity validation for multiple recipients" do
+ expect { group.add(user) }.to change { Topic.count }.by(1)
+ expect { group.add(user_2) }.to change { Topic.count }.by(1)
end
end
end
- context "when delayed" do
- fab!(:user_1, :user)
-
- before { automation.update!(trigger: DiscourseAutomation::Triggers::RECURRING) }
+ context "with delay" do
+ fab!(:user)
- it "correctly sets encrypt preference to false even when option is not specified" do
+ before do
+ automation.update!(trigger: DiscourseAutomation::Triggers::RECURRING)
+ automation.upsert_field!("receiver", "user", { value: user.username })
automation.upsert_field!(
"sendable_pms",
"pms",
- {
- value: [
- {
- title: "A message from {{sender_username}}",
- raw: "This is a message sent to @{{receiver_username}}",
- delay: 1,
- },
- ],
- },
- target: "script",
+ { value: [{ title: "Delayed", raw: "Content", delay: 5 }] },
)
- automation.upsert_field!("receiver", "user", { value: Discourse.system_user.username })
-
- automation.trigger!
-
- expect(DiscourseAutomation::PendingPm.last.prefers_encrypt).to eq(false)
end
- it "correctly stores encrypt preference to false" do
- automation.upsert_field!(
- "sendable_pms",
- "pms",
- {
- value: [
- {
- title: "A message from {{sender_username}}",
- raw: "This is a message sent to @{{receiver_username}}",
- delay: 1,
- prefers_encrypt: false,
- },
- ],
- },
- target: "script",
- )
- automation.upsert_field!("receiver", "user", { value: Discourse.system_user.username })
-
- automation.trigger!
+ it "creates a pending PM instead of sending immediately" do
+ expect { automation.trigger! }.to change { DiscourseAutomation::PendingPm.count }.by(
+ 1,
+ ).and not_change { Topic.count }
- expect(DiscourseAutomation::PendingPm.last.prefers_encrypt).to eq(false)
+ pending_pm = DiscourseAutomation::PendingPm.last
+ expect(pending_pm.title).to eq("Delayed")
+ expect(pending_pm.target_usernames).to eq([user.username])
+ expect(pending_pm.execute_at).to be_within(1.minute).of(5.minutes.from_now)
end
end
end
diff --git a/plugins/automation/test/javascripts/integration/components/da-pms-field-test.gjs b/plugins/automation/test/javascripts/integration/components/da-pms-field-test.gjs
index c346823dd64..dc6b6f8e217 100644
--- a/plugins/automation/test/javascripts/integration/components/da-pms-field-test.gjs
+++ b/plugins/automation/test/javascripts/integration/components/da-pms-field-test.gjs
@@ -31,12 +31,10 @@ module("Integration | Component | da-pms-field", function (hooks) {
await fillIn(".pm-title", "title");
await fillIn(".d-editor-input", "raw");
await fillIn(".pm-delay", 6);
- await click(".pm-prefers-encrypt", 6);
assert.deepEqual(this.field.metadata.value, [
{
delay: "6",
- prefers_encrypt: false,
raw: "raw",
title: "title",
},
diff --git a/plugins/discourse-data-explorer/plugin.rb b/plugins/discourse-data-explorer/plugin.rb
index 55e9e2b6104..2293ff7725e 100644
--- a/plugins/discourse-data-explorer/plugin.rb
+++ b/plugins/discourse-data-explorer/plugin.rb
@@ -132,7 +132,7 @@ after_initialize do
)
.each do |pm|
begin
- utils.send_pm(pm, automation_id: automation.id, prefers_encrypt: false)
+ utils.send_pm(pm, automation_id: automation.id)
rescue ActiveRecord::RecordNotSaved => e
Rails.logger.warn "#{DiscourseDataExplorer::PLUGIN_NAME} - couldn't send PM for automation #{automation.id}: #{e.message}"
end