Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions lib/custom_wizard/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def send_message

params = basic_topic_params

targets = map_list(action["recipient"]).uniq(&:downcase)
targets = map_list(action["recipient"]).uniq { |target| target.to_s.downcase }

if targets.blank?
log_error("no recipients", "send_message has no recipients")
Expand All @@ -85,11 +85,11 @@ def send_message
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
elsif target.match(/@/) # Compare discourse/discourse/app/controllers/posts_controller.rb#L922-L923
if group = target.is_a?(Integer) ? Group.find_by(id: target) : Group.find_by(name: target)
params[:target_group_names] << group.name
elsif recipient = User.find_by_username(target)
params[:target_usernames] << recipient.username
elsif target.to_s.match(/@/) # Compare discourse/discourse/app/controllers/posts_controller.rb#L922-L923
params[:target_emails] << target
end
end
Expand Down Expand Up @@ -169,7 +169,13 @@ def watch_tags
end

def watch_categories
watched_categories = map_list(action["categories"]).map(&:to_i)
values = map_list(action["categories"])
watched_categories = find_category_ids(values)

if values.present? && watched_categories.empty?
log_error("categories not found", "categories: #{values.join(",")}")
return
end

notification_level = action["notification_level"]

Expand Down Expand Up @@ -390,13 +396,20 @@ def action_category

return false if output.blank?

if output.is_a?(Array)
output.first
elsif output.is_a?(Integer)
output
elsif output.is_a?(String)
output.to_i
end
find_category_id(output) || false
end

def find_category_id(value)
find_category_ids(Array.wrap(value).first).first
end

def find_category_ids(values)
values = Array.wrap(values).map { |value| value.to_s.strip }.compact_blank
Category
.where(id: values.grep(/\A\d+\z/))
.or(Category.where(slug: values))
.order(:id)
.pluck(:id)
end

def action_tags
Expand Down Expand Up @@ -528,8 +541,7 @@ def basic_topic_params
def public_topic_params
params = {}

# Like TopicCreator, ignore a category that doesn't exist.
if (category = action_category) && Category.exists?(id: category)
if category = action_category
params[:category] = category
end

Expand Down Expand Up @@ -695,7 +707,9 @@ def new_category_params
CustomWizard::Mapper.new(inputs: action[attr], data: mapper_data, user: user).perform

if value
value = value[0] if attr === "parent_category_id" && value.is_a?(Array)
if attr === "parent_category_id"
value = find_category_id(value) || Array.wrap(value).first
end

if attr === "permissions" && value.is_a?(Array)
permissions = value
Expand Down
4 changes: 1 addition & 3 deletions lib/custom_wizard/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def append_field(step, step_template, field_template, build_opts)
]
end

if field_template["type"] === "group" && params[:value].present?
params[:value] = params[:value].first
end
params[:value] = Array.wrap(params[:value]).first if field_template["type"] === "group"

params[:value] = standardise_boolean(params[:value]) if field_template["type"] === "checkbox"

Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# name: discourse-custom-wizard
# about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more.
# version: 2.18.13
# version: 2.18.14
# authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez
# url: https://github.com/paviliondev/discourse-custom-wizard
# contact_emails: development@pavilion.tech
Expand Down
96 changes: 77 additions & 19 deletions spec/components/custom_wizard/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,31 @@ def complete_wizard(body: "topic body", **last_step_fields)
expect(submission.redirect_on_complete).to eq(Topic.find_by(title: "Topic Title").url)
end

it "ignores a category that doesn't exist" do
def assign_category(output)
wizard_template[:actions][0]["category"] = [
{ type: "assignment", output_type: "text", output_connector: "set", output: "missing" },
{ type: "assignment", output_type: "text", output_connector: "set", output: output },
]
update_template(wizard_template)
end

it "ignores a category that doesn't exist" do
assign_category("missing")

complete_wizard

topic = Topic.find_by(title: "Topic Title")
expect(topic.category_id).to eq(SiteSetting.uncategorized_category_id)
end

it "uses a category given by slug" do
assign_category(category.slug)

complete_wizard

topic = Topic.find_by(title: "Topic Title")
expect(topic.category_id).to eq(category.id)
end

context "with tags" do
fab!(:tag2) { Fabricate(:tag, name: "tag2") }

Expand Down Expand Up @@ -484,17 +497,43 @@ def composer_query
)
end

it "watches categories" do
watch_categories[:categories][0][:output] = category.id
def run_watch_categories
wizard_template[:actions] << watch_categories
update_template(wizard_template)

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
end

expect(
CategoryUser.where(category_id: category.id, user_id: user.id).first.notification_level,
).to eq(2)
def category_level(category)
CategoryUser.find_by(category: category, user: user)&.notification_level
end

it "watches categories" do
watch_categories[:categories][0][:output] = category.id
run_watch_categories

expect(category_level(category)).to eq(CategoryUser.notification_levels[:tracking])
end

it "watches categories given by slug" do
watch_categories[:categories][0][:output] = category.slug
run_watch_categories

expect(category_level(category)).to eq(CategoryUser.notification_levels[:tracking])
end

it "mutes the other categories when no category is given" do
run_watch_categories

expect(category_level(category)).to eq(CategoryUser.notification_levels[:muted])
end

it "doesn't mute categories when no category is found" do
watch_categories[:categories][0][:output] = "missing"
run_watch_categories

expect(CategoryUser.where(user: user)).to be_empty
end

it "watches categories for the users in the action" do
Expand All @@ -510,11 +549,7 @@ def composer_query
output: [user1.username, user2.username],
},
]
wizard_template[:actions] << watch_categories
update_template(wizard_template)

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
run_watch_categories

expect(
CategoryUser.where(category_id: category.id, notification_level: 2).pluck(:user_id),
Expand All @@ -532,11 +567,7 @@ def composer_query
watch_categories[:usernames] = [user1, user2].map do |u|
{ type: "assignment", output_type: "user", output_connector: "set", output: [u.username] }
end
wizard_template[:actions] << watch_categories
update_template(wizard_template)

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
run_watch_categories

expect(
CategoryUser.where(notification_level: 2).pluck(:user_id, :category_id),
Expand Down Expand Up @@ -645,6 +676,21 @@ def composer_query
expect(topic.allowed_groups).to contain_exactly(group)
end

it "#send_message sends to the group in a group field" do
wizard_template[:steps][0][:fields] << { id: "step_1_field_5", type: "group" }.as_json
send_message["recipient"] = [step_1_field_1_input.merge(output: "step_1_field_5")]
wizard_template["actions"] << send_message
update_template(wizard_template)

wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[0].id, step_1_field_5: group.id).update
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(wizard.steps[1].id, {}).update

topic = Topic.find_by!(archetype: Archetype.private_message, title: "Message title")
expect(topic.allowed_groups).to contain_exactly(group)
end

it "#send_message sends to each recipient in a comma separated value" do
send_message["recipient"] = [step_1_field_1_input]
wizard_template["actions"] << send_message
Expand Down Expand Up @@ -816,7 +862,7 @@ def complete_as_guest(email)
end

context "business actions" do
it "#create_category" do
def run_create_category
wizard_template["actions"] << create_category
wizard_template["actions"] << create_group
update_template(wizard_template)
Expand All @@ -825,7 +871,19 @@ def complete_as_guest(email)
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "Text input").update
wizard.create_updater(wizard.steps[1].id, {}).update

expect(Category.where(id: wizard.current_submission.fields["action_8"]).exists?).to eq(true)
Category.find_by(id: wizard.current_submission.fields["action_8"])
end

it "#create_category" do
expect(run_create_category).to be_present
end

it "#create_category uses a parent category given by slug" do
create_category["parent_category_id"] = [
{ type: "assignment", output_type: "text", output_connector: "set", output: category.slug },
]

expect(run_create_category.parent_category_id).to eq(category.id)
end

it "#create_group" do
Expand Down
Loading