0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 01:10:28 +01:00

Add poster and guest_email to topic and message creation && allow guests to create_topic

Dieser Commit ist enthalten in:
Angus McLeod 2024-07-09 11:23:57 +02:00
Ursprung 441ad49bf6
Commit cff8f9f427
15 geänderte Dateien mit 336 neuen und 55 gelöschten Zeilen

Datei anzeigen

@ -145,6 +145,8 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController
custom_fields: mapped_params, custom_fields: mapped_params,
visible: mapped_params, visible: mapped_params,
required: mapped_params, required: mapped_params,
poster: mapped_params,
guest_email: mapped_params,
recipient: mapped_params, recipient: mapped_params,
categories: mapped_params, categories: mapped_params,
mute_remainder: mapped_params, mute_remainder: mapped_params,

Datei anzeigen

@ -5,6 +5,7 @@ import { computed } from "@ember/object";
import UndoChanges from "../mixins/undo-changes"; import UndoChanges from "../mixins/undo-changes";
import Component from "@ember/component"; import Component from "@ember/component";
import I18n from "I18n"; import I18n from "I18n";
import { WIZARD_USER } from "./wizard-user-chooser";
export default Component.extend(UndoChanges, { export default Component.extend(UndoChanges, {
componentType: "action", componentType: "action",

Datei anzeigen

@ -121,6 +121,9 @@ export default Component.extend({
guestGroup: computed("options.guestGroup", "inputType", function () { guestGroup: computed("options.guestGroup", "inputType", function () {
return this.optionEnabled("guestGroup"); return this.optionEnabled("guestGroup");
}), }),
includeMessageableGroups: computed("options.includeMessageableGroups", "inputType", function () {
return this.optionEnabled("includeMessageableGroups");
}),
userEnabled: computed("options.userSelection", "inputType", function () { userEnabled: computed("options.userSelection", "inputType", function () {
return this.optionEnabled("userSelection"); return this.optionEnabled("userSelection");
}), }),
@ -352,6 +355,17 @@ export default Component.extend({
return result; return result;
}, },
@discourseComputed("includeMessageableGroups", "options.userLimit")
userOptions(includeMessageableGroups, userLimit) {
const opts = {
includeMessageableGroups
}
if (userLimit) {
opts.maximum = userLimit
}
return opts;
},
optionEnabled(type) { optionEnabled(type) {
const options = this.options; const options = this.options;
if (!options) { if (!options) {

Datei anzeigen

@ -33,6 +33,8 @@ export default Component.extend({
outputConnector: options.outputConnector || null, outputConnector: options.outputConnector || null,
context: options.context || null, context: options.context || null,
guestGroup: options.guestGroup || false, guestGroup: options.guestGroup || false,
includeMessageableGroups: options.includeMessageableGroups || false,
userLimit: options.userLimit || null
}; };
let inputTypes = ["key", "value", "output"]; let inputTypes = ["key", "value", "output"];

Datei anzeigen

@ -0,0 +1,55 @@
import UserChooserComponent from "select-kit/components/user-chooser";
export const WIZARD_USER = "wizard-user";
export default UserChooserComponent.extend({
pluginApiIdentifiers: ["wizard-user-chooser"],
classNames: ["user-chooser", "wizard-user-chooser"],
classNameBindings: ["selectKit.options.fullWidthWrap:full-width-wrap"],
valueProperty: "id",
nameProperty: "name",
modifyComponentForRow() {
return "wizard-user-chooser/wizard-user-chooser-row";
},
modifyNoSelection() {
return this.defaultItem(WIZARD_USER, I18n.t("admin.wizard.action.poster.wizard_user"));
},
selectKitOptions: {
fullWidthWrap: false,
autoWrap: false,
},
search() {
const superPromise = this._super(...arguments);
if (!superPromise) {
return;
}
return superPromise.then((results) => {
console.log(results)
if (!results || results.length === 0) {
return;
}
return results.map((item) => {
const reconstructed = {};
if (item.username) {
reconstructed.id = item.username;
if (item.username.includes("@")) {
reconstructed.isEmail = true;
} else {
reconstructed.isUser = true;
reconstructed.name = item.name;
reconstructed.showUserStatus = this.showUserStatus;
}
} else if (item.name) {
reconstructed.id = item.name;
reconstructed.name = item.full_name;
reconstructed.isGroup = true;
}
return { ...item, ...reconstructed };
});
});
},
});

Datei anzeigen

@ -0,0 +1,5 @@
import SelectKitRowComponent from "select-kit/components/select-kit/select-kit-row";
export default SelectKitRowComponent.extend({
classNames: ["user-row", "wizard-user-chooser-row"],
});

Datei anzeigen

@ -100,6 +100,8 @@ const action = {
custom_fields: null, custom_fields: null,
skip_redirect: null, skip_redirect: null,
suppress_notifications: null, suppress_notifications: null,
poster: 'wizard-user',
guest_email: null,
add_event: null, add_event: null,
add_location: null, add_location: null,
}, },
@ -111,6 +113,8 @@ const action = {
skip_redirect: null, skip_redirect: null,
custom_fields: null, custom_fields: null,
required: null, required: null,
poster: 'wizard-user',
guest_email: null,
recipient: null, recipient: null,
suppress_notifications: null, suppress_notifications: null,
}, },
@ -184,6 +188,7 @@ const action = {
"custom_fields", "custom_fields",
"required", "required",
"recipient", "recipient",
"poster",
"profile_updates", "profile_updates",
"group", "group",
"url", "url",

Datei anzeigen

@ -98,6 +98,48 @@
</div> </div>
</div> </div>
{{/if}} {{/if}}
<div class="setting full field-mapper-setting">
<div class="setting-label">
<label>{{i18n "admin.wizard.action.poster.label"}}</label>
</div>
<div class="setting-value">
{{wizard-mapper
inputs=this.action.poster
property="poster"
onUpdate=(action "mappedFieldUpdated")
options=(hash
textSelection='key,value'
wizardFieldSelection=true
userSelection="output"
outputDefaultSelection="user"
userLimit="1"
context="action"
)
}}
</div>
</div>
<div class="setting full field-mapper-setting">
<div class="setting-label">
<label>{{i18n "admin.wizard.action.guest_email.label"}}</label>
</div>
<div class="setting-value">
{{wizard-mapper
inputs=this.action.guest_email
property="guest_email"
onUpdate=(action "mappedFieldUpdated")
options=(hash
textSelection="key,value"
wizardFieldSelection=true
outputPlaceholder="admin.wizard.action.guest_email.placeholder"
context="action"
)
}}
</div>
</div>
{{/if}} {{/if}}
{{#if publicTopicFields}} {{#if publicTopicFields}}
@ -217,6 +259,7 @@
userSelection="output" userSelection="output"
outputDefaultSelection="user" outputDefaultSelection="user"
context="action" context="action"
includeMessageableGroups="true"
) )
}} }}
</div> </div>

Datei anzeigen

@ -66,12 +66,12 @@
{{/if}} {{/if}}
{{#if showUser}} {{#if showUser}}
{{email-group-user-chooser {{wizard-user-chooser
placeholderKey=placeholderKey placeholderKey=placeholderKey
value=value value=value
autocomplete="discourse" autocomplete="discourse"
onChange=(action "changeUserValue") onChange=(action "changeUserValue")
options=(hash includeMessageableGroups="true") options=userOptions
}} }}
{{/if}} {{/if}}
</div> </div>

Datei anzeigen

@ -0,0 +1,20 @@
{{#if this.item.isUser}}
{{avatar this.item imageSize="tiny"}}
<div>
<span class="identifier">{{format-username this.item.id}}</span>
<span class="name">{{this.item.name}}</span>
</div>
{{#if (and this.item.showUserStatus this.item.status)}}
<UserStatusMessage @status={{this.item.status}} @showDescription={{true}} />
{{/if}}
{{decorate-username-selector this.item.id}}
{{else if this.item.isGroup}}
{{d-icon "users"}}
<div>
<span class="identifier">{{this.item.id}}</span>
<span class="name">{{this.item.full_name}}</span>
</div>
{{else}}
{{d-icon "envelope"}}
<span class="identifier">{{this.item.id}}</span>
{{/if}}

Datei anzeigen

@ -344,6 +344,12 @@ en:
include: "Include Fields" include: "Include Fields"
title: "Title" title: "Title"
post: "Post" post: "Post"
poster:
label: "Poster"
wizard_user: "Wizard user"
guest_email:
label: "Guest email"
placeholder: "Field for guest email"
topic_attr: "Topic Attribute" topic_attr: "Topic Attribute"
interpolate_fields: "Insert wizard fields using the field_id in w{}. Insert user fields using field key in u{}." interpolate_fields: "Insert wizard fields using the field_id in w{}. Insert user fields using field key in u{}."

Datei anzeigen

@ -7,13 +7,14 @@ class CustomWizard::Action
:result :result
REQUIRES_USER = %w[ REQUIRES_USER = %w[
create_topic
update_profile update_profile
open_composer open_composer
watch_categories watch_categories
add_to_group add_to_group
] ]
WIZARD_USER = 'wizard-user'
def initialize(opts) def initialize(opts)
@wizard = opts[:wizard] @wizard = opts[:wizard]
@action = opts[:action] @action = opts[:action]
@ -69,7 +70,7 @@ class CustomWizard::Action
end end
if params[:title].present? && params[:raw].present? if params[:title].present? && params[:raw].present?
creator = PostCreator.new(user, params) creator = PostCreator.new(topic_poster, params)
post = creator.create post = creator.create
if creator.errors.present? if creator.errors.present?
@ -138,8 +139,7 @@ class CustomWizard::Action
params[:archetype] = Archetype.private_message params[:archetype] = Archetype.private_message
poster = user || Discourse.system_user creator = PostCreator.new(topic_poster, params)
creator = PostCreator.new(poster, params)
post = creator.create post = creator.create
if creator.errors.present? if creator.errors.present?
@ -653,6 +653,45 @@ class CustomWizard::Action
params params
end end
def topic_poster
@topic_poster ||= begin
poster_id = CustomWizard::Mapper.new(
inputs: action['poster'],
data: mapper_data,
user: user,
).perform
poster_id = [*poster_id].first if poster_id.present?
if poster_id.blank? || poster_id === WIZARD_USER
poster = user || guest_user
else
poster = User.find_by_username(poster_id)
end
poster || Discourse.system_user
end
end
def guest_user
@guest_user ||= begin
return nil unless action['guest_email']
email = CustomWizard::Mapper.new(
inputs: action['guest_email'],
data: mapper_data,
).perform
if email&.match(/@/)
User.create!(
email: email,
username: UserNameSuggester.suggest(email),
name: User.suggest_name(email),
staged: true,
)
end
end
end
def new_group_params def new_group_params
params = {} params = {}

Datei anzeigen

@ -31,7 +31,6 @@ class CustomWizard::Field
:step :step
REQUIRES_USER = %w[ REQUIRES_USER = %w[
composer
upload upload
] ]

Datei anzeigen

@ -46,7 +46,7 @@ describe CustomWizard::Action do
update_template(wizard_template) update_template(wizard_template)
end end
context 'creating a topic' do describe '#create_topic' do
it "works" do it "works" do
wizard = CustomWizard::Builder.new(@template[:id], user).build wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater( wizard.create_updater(
@ -160,6 +160,43 @@ describe CustomWizard::Action do
expect(action.result.success?).to eq(true) expect(action.result.success?).to eq(true)
expect(TopicCustomField.exists?(name: custom_field_name)).to eq(true) expect(TopicCustomField.exists?(name: custom_field_name)).to eq(true)
end end
it "allows poster to be set" do
wizard_template[:actions][0]["poster"] = [
{
"type": "assignment",
"output_type": "user",
"output_connector": "set",
"output": [
"angus1"
]
}
]
update_template(wizard_template)
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
)
expect(topic.exists?).to eq(true)
post = Post.find_by(
topic_id: topic.pluck(:id),
raw: "topic body"
)
expect(post.present?).to eq(true)
expect(post.user.username).to eq('angus1')
end
end end
it 'updates a profile' do it 'updates a profile' do
@ -234,6 +271,7 @@ describe CustomWizard::Action do
context "standard subscription actions" do context "standard subscription actions" do
before do before do
enable_subscription("standard") enable_subscription("standard")
Jobs.run_immediately!
end end
it 'watches tags' do it 'watches tags' do
@ -333,61 +371,117 @@ describe CustomWizard::Action do
expect(user2.reload.notifications.count).to eq(1) expect(user2.reload.notifications.count).to eq(1)
end end
it "send_message works when guests are permitted" do context "with a guest" do
wizard_template["permitted"] = guests_permitted["permitted"] describe "#create_topic" do
wizard_template.delete("actions") it "creates a staged guest poster if guest_email is set" do
wizard_template['actions'] = [send_message] Jobs.run_immediately!
update_template(wizard_template)
User.create(username: 'angus1', email: "angus1@email.com") wizard_template["permitted"] = guests_permitted["permitted"]
wizard_template[:steps][0][:fields] << {
"id": "step_1_field_5",
"label": "Guest Email",
"type": "text",
"min_length": "3",
}.as_json
create_topic["run_after"] = "step_3"
create_topic["guest_email"] = [
{
"type": "assignment",
"output": "step_1_field_5",
"output_type": "wizard_field",
"output_connector": "set"
}
]
create_topic["category"] = [
{
"type": "assignment",
"output": "step_3_field_3",
"output_type": "wizard_field",
"output_connector": "set"
}
]
wizard_template.delete("actions")
wizard_template[:actions] = [create_topic]
wizard = CustomWizard::Builder.new(wizard_template["id"], nil, CustomWizard::Wizard.generate_guest_id).build update_template(wizard_template)
wizard.create_updater(wizard.steps[0].id, {}).update
updater = wizard.create_updater(wizard.steps[1].id, {})
updater.update
topic = Topic.where(archetype: Archetype.private_message, title: "Message title") wizard = CustomWizard::Builder.new(
post = Post.where(topic_id: topic.pluck(:id)) @template[:id],
nil,
CustomWizard::Wizard.generate_guest_id
).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_5: "guest@email.com"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
expect(topic.exists?).to eq(true) topic = Topic.where(category_id: category.id).first
expect(topic.first.topic_allowed_users.first.user.username).to eq('angus1') expect(topic.present?).to eq(true)
expect(topic.first.topic_allowed_users.second.user.username).to eq(Discourse.system_user.username) expect(topic.posts.first.user.staged).to eq(true)
expect(post.exists?).to eq(true) expect(topic.posts.first.user.primary_email.email).to eq('guest@email.com')
end end
end
it "send_message works when guests are permitted and the target is an email address" do describe "#send_message" do
Jobs.run_immediately! it "works" do
wizard_template["permitted"] = guests_permitted["permitted"]
wizard_template.delete("actions")
wizard_template['actions'] = [send_message]
update_template(wizard_template)
wizard_template["permitted"] = guests_permitted["permitted"] wizard = CustomWizard::Builder.new(wizard_template["id"], nil, CustomWizard::Wizard.generate_guest_id).build
wizard_template.delete("actions") wizard.create_updater(wizard.steps[0].id, {}).update
updater = wizard.create_updater(wizard.steps[1].id, {})
updater.update
send_message["recipient"] = [ topic = Topic.where(archetype: Archetype.private_message, title: "Message title")
{ post = Post.where(topic_id: topic.pluck(:id))
"type": "assignment",
"output": "step_1_field_1",
"output_type": "wizard_field",
"output_connector": "set"
}
]
wizard_template['actions'] = [send_message] expect(topic.exists?).to eq(true)
update_template(wizard_template) expect(topic.first.topic_allowed_users.first.user.username).to eq(user1.username)
expect(topic.first.topic_allowed_users.second.user.username).to eq(Discourse.system_user.username)
expect(post.exists?).to eq(true)
end
NotificationEmailer.expects(:process_notification).once it "works when the target is an email address" do
Jobs.run_immediately!
wizard = CustomWizard::Builder.new(wizard_template["id"], nil, CustomWizard::Wizard.generate_guest_id).build wizard_template["permitted"] = guests_permitted["permitted"]
wizard.create_updater(wizard.steps[0].id, step_1_field_1: "guest@email.com").update wizard_template.delete("actions")
updater = wizard.create_updater(wizard.steps[1].id, {})
updater.update
topic = Topic.where(archetype: Archetype.private_message, title: "Message title") send_message["recipient"] = [
post = Post.where(topic_id: topic.pluck(:id)) {
"type": "assignment",
"output": "step_1_field_1",
"output_type": "wizard_field",
"output_connector": "set"
}
]
expect(topic.exists?).to eq(true) wizard_template['actions'] = [send_message]
expect(topic.first.topic_allowed_users.first.user.staged).to eq(true) update_template(wizard_template)
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) NotificationEmailer.expects(:process_notification).once
expect(post.exists?).to eq(true)
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
end end
end end
@ -528,7 +622,6 @@ describe CustomWizard::Action do
wizard.create_updater(wizard.steps.second.id, {}).update wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id) wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id)
.update .update
User.create(username: 'angus1', email: 'angus1@email.com')
wizard.create_updater(wizard.steps[0].id, {}).update wizard.create_updater(wizard.steps[0].id, {}).update
wizard.create_updater(wizard.steps[1].id, {}).update wizard.create_updater(wizard.steps[1].id, {}).update
topic = Topic.where(title: 'Topic Title', category_id: category.id) topic = Topic.where(title: 'Topic Title', category_id: category.id)

Datei anzeigen

@ -155,9 +155,6 @@ describe CustomWizard::TemplateValidator do
validator = CustomWizard::TemplateValidator.new(template) validator = CustomWizard::TemplateValidator.new(template)
expect(validator.perform).to eq(false) expect(validator.perform).to eq(false)
errors = validator.errors.to_a errors = validator.errors.to_a
expect(errors).to include(
I18n.t("wizard.validation.not_permitted_for_guests", object_id: "action_1")
)
expect(errors).to include( expect(errors).to include(
I18n.t("wizard.validation.not_permitted_for_guests", object_id: "step_2_field_7") I18n.t("wizard.validation.not_permitted_for_guests", object_id: "step_2_field_7")
) )