From f443840358f926edc3cb2f11cdb3a427d4859fe6 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Wed, 14 Apr 2021 08:59:12 +0530 Subject: [PATCH] FEATURE: allow targetting multiple users/groups when sending a private message --- lib/custom_wizard/action.rb | 6 ++- spec/components/custom_wizard/action_spec.rb | 55 ++++++++++++++------ spec/fixtures/wizard.json | 26 +++++++++ 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index fbfa17a5..6a650125 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -89,11 +89,13 @@ class CustomWizard::Action return end + params[:target_group_names] = [] + params[:target_usernames] = [] targets.each do |target| if Group.find_by(name: target) - params[:target_group_names] = target + params[:target_group_names] << target elsif User.find_by_username(target) - params[:target_usernames] = target + params[:target_usernames] << target else # end diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index 9ccb51bf..932539e5 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -67,26 +67,49 @@ describe CustomWizard::Action do end end - it 'sends a message' do - User.create(username: 'angus1', email: "angus1@email.com") + context 'sending a message' do + it 'works' do + User.create(username: 'angus1', email: "angus1@email.com") - wizard = CustomWizard::Builder.new(@template[:id], user).build - wizard.create_updater(wizard.steps[0].id, {}).update - wizard.create_updater(wizard.steps[1].id, {}).update + wizard = CustomWizard::Builder.new(@template[:id], user).build + wizard.create_updater(wizard.steps[0].id, {}).update + wizard.create_updater(wizard.steps[1].id, {}).update - topic = Topic.where( - archetype: Archetype.private_message, - title: "Message title" - ) + topic = Topic.where( + archetype: Archetype.private_message, + title: "Message title" + ) - post = Post.where( - topic_id: topic.pluck(:id), - raw: "I will interpolate some wizard fields" - ) + post = Post.where( + topic_id: topic.pluck(:id), + raw: "I will interpolate some wizard fields" + ) - expect(topic.exists?).to eq(true) - expect(topic.first.topic_allowed_users.first.user.username).to eq('angus1') - expect(post.exists?).to eq(true) + expect(topic.exists?).to eq(true) + expect(topic.first.topic_allowed_users.first.user.username).to eq('angus1') + expect(post.exists?).to eq(true) + end + + it 'allows using multiple PM targets' do + User.create(username: 'angus1', email: "angus1@email.com") + User.create(username: 'faiz', email: "faiz@email.com") + wizard = CustomWizard::Builder.new(@template[:id], user).build + wizard.create_updater(wizard.steps[0].id, {}).update + wizard.create_updater(wizard.steps[1].id, {}).update + + topic = Topic.where( + archetype: Archetype.private_message, + title: "Multiple Recipients title" + ) + + post = Post.where( + topic_id: topic.pluck(:id), + raw: "I will interpolate some wizard fields" + ) + expect(topic.exists?).to eq(true) + expect(topic.first.all_allowed_users.map(&:username)).to include('angus1', 'faiz') + expect(post.exists?).to eq(true) + end end it 'updates a profile' do diff --git a/spec/fixtures/wizard.json b/spec/fixtures/wizard.json index 3718b154..14210d7d 100644 --- a/spec/fixtures/wizard.json +++ b/spec/fixtures/wizard.json @@ -464,6 +464,32 @@ } ] }, + { + "id": "action_11", + "run_after": "step_2", + "type": "send_message", + "post_builder": true, + "post_template": "I will interpolate some wizard fields w{step_1_field_1} w{step_1_field_2}", + "title": [ + { + "type": "assignment", + "output": "Multiple Recipients title", + "output_type": "text", + "output_connector": "set" + } + ], + "recipient": [ + { + "type": "assignment", + "output_type": "user", + "output_connector": "set", + "output": [ + "angus1", + "faiz" + ] + } + ] + }, { "id": "action_3", "run_after": "step_2",