From 441ad49bf62f69148b69e93406c2ecb7ac925168 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 8 Jul 2024 11:58:24 +0200 Subject: [PATCH] Add email address support to send_message recipients See further https://coop.pavilion.tech/t/custom-wizard-pm-access-and-send-copy-of-submission-for-guest-users/3600 --- lib/custom_wizard/action.rb | 8 +++-- spec/components/custom_wizard/action_spec.rb | 37 +++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 9fe725e7..7972f92c 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -119,20 +119,22 @@ class CustomWizard::Action params[:target_group_names] = [] params[:target_usernames] = [] + params[:target_emails] = [] [*targets].each do |target| if Group.find_by(name: target) params[:target_group_names] << target elsif User.find_by_username(target) params[:target_usernames] << target - else - # + elsif target.match(/@/) # Compare discourse/discourse/app/controllers/posts_controller.rb#L922-L923 + params[:target_emails] << target end end if params[:title].present? && params[:raw].present? && (params[:target_usernames].present? || - params[:target_group_names].present?) + params[:target_group_names].present? || + params[:target_emails].present?) params[:archetype] = Archetype.private_message diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 03deab27..4ce6d842 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -333,7 +333,7 @@ describe CustomWizard::Action do expect(user2.reload.notifications.count).to eq(1) end - it "send_message works with guests are permitted" do + it "send_message works when guests are permitted" do wizard_template["permitted"] = guests_permitted["permitted"] wizard_template.delete("actions") wizard_template['actions'] = [send_message] @@ -354,6 +354,41 @@ describe CustomWizard::Action do expect(topic.first.topic_allowed_users.second.user.username).to eq(Discourse.system_user.username) expect(post.exists?).to eq(true) end + + it "send_message works when guests are permitted and the target is an email address" do + Jobs.run_immediately! + + wizard_template["permitted"] = guests_permitted["permitted"] + wizard_template.delete("actions") + + send_message["recipient"] = [ + { + "type": "assignment", + "output": "step_1_field_1", + "output_type": "wizard_field", + "output_connector": "set" + } + ] + + wizard_template['actions'] = [send_message] + update_template(wizard_template) + + NotificationEmailer.expects(:process_notification).once + + wizard = CustomWizard::Builder.new(wizard_template["id"], nil, CustomWizard::Wizard.generate_guest_id).build + wizard.create_updater(wizard.steps[0].id, step_1_field_1: "guest@email.com").update + updater = wizard.create_updater(wizard.steps[1].id, {}) + updater.update + + topic = Topic.where(archetype: Archetype.private_message, title: "Message title") + post = Post.where(topic_id: topic.pluck(:id)) + + expect(topic.exists?).to eq(true) + expect(topic.first.topic_allowed_users.first.user.staged).to eq(true) + expect(topic.first.topic_allowed_users.first.user.primary_email.email).to eq('guest@email.com') + expect(topic.first.topic_allowed_users.second.user.username).to eq(Discourse.system_user.username) + expect(post.exists?).to eq(true) + end end context "business subscription actions" do