diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index 7794b61c..5d3f3cec 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -15,7 +15,8 @@ export default Component.extend({ routeTo: equal('action.type', 'route_to'), disableId: not('action.isNew'), groupPropertyTypes: selectKitContent(['id', 'name']), - hasAdvanced: or('basicTopicFields', 'routeTo'), + hasAdvanced: or('hasCustomFields', 'routeTo'), + hasCustomFields: or('basicTopicFields', 'updateProfile'), @on('didInsertElement') @observes('action.type') diff --git a/assets/javascripts/discourse/lib/wizard.js.es6 b/assets/javascripts/discourse/lib/wizard.js.es6 index 6bd6e62b..9ba65bab 100644 --- a/assets/javascripts/discourse/lib/wizard.js.es6 +++ b/assets/javascripts/discourse/lib/wizard.js.es6 @@ -32,8 +32,8 @@ function camelCase(string) { const profileFields = [ 'name', - 'username', 'email', + 'avatar', 'date_of_birth', 'title', 'locale', @@ -181,7 +181,7 @@ const advancedProperties = { function(map, type) { if (type === 'route_to') { map[type] = ['code']; - } else if (['create_topic', 'send_message', 'open_composer'].indexOf(type) > -1) { + } else if (['create_topic', 'send_message', 'open_composer', 'update_profile'].indexOf(type) > -1) { map[type] = ['custom_fields']; } else if (['create_topic', 'send_message'].indexOf(type) > -1) { map[type].push('skip_redirect'); diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 18099054..e815a203 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -138,11 +138,11 @@ {{wizard-mapper inputs=action.profile_updates options=(hash - pairConnector='set' + single=true + inputTypes='association' userFieldSelection='key' wizardFieldSelection='value' keyDefaultSelection='userField' - keyPlaceholder='admin.wizard.action.update_profile.key' context='action' )}} @@ -238,7 +238,7 @@ {{#if action.showAdvanced}}
- {{#if basicTopicFields}} + {{#if hasCustomFields}}
diff --git a/lib/custom_wizard/actions.rb b/lib/custom_wizard/actions.rb index 0cadfcfa..e7f67e5a 100644 --- a/lib/custom_wizard/actions.rb +++ b/lib/custom_wizard/actions.rb @@ -63,26 +63,36 @@ class CustomWizard::Action def update_profile return unless (profile_updates = action['profile_updates']).length - - attributes = { custom_fields: {} } - - profile_updates.each do |pu| - pair = field['pairs'].first - field = mapper.map_field(pair['key'], pair['key_type']) - value = mapper.map_field(pair['value'], pair['value_type']) - - if field.include?("custom_field") - attributes[:custom_fields][field] = value - else - attributes[field.to_sym] = value + + allowed_fields = CustomWizard::Mapper.user_fields + ['avatar'] + params = {} + + profile_updates.first[:pairs].each do |pair| + if allowed_fields.include?(pair['key']) + key = pair['key'] + key = "#{key}_upload_url" if ['profile_background', 'card_background'].include?(key) + params[key.to_sym] = mapper.map_field(pair['value'], pair['value_type']) end end - if attributes.present? - user_updater = UserUpdater.new(user, user) - user_updater.update(attributes) + params = add_custom_fields(params) + + if params.present? + UserUpdater.new(Discourse.system_user, user).update(params) + + if params[:avatar].present? + update_avatar(params[:avatar]) + end end end + + def update_avatar(upload) + user.create_user_avatar unless user.user_avatar + user.user_avatar.custom_upload_id = upload['id'] + user.uploaded_avatar_id = upload['id'] + user.save! + user.user_avatar.save! + end def send_to_api api_body = nil @@ -220,11 +230,11 @@ class CustomWizard::Action keyArr = field[:key].split('.') value = field[:value] - if keyArr.length != 2 || keyArr.first === 'topic' + if keyArr.first === 'topic' params[:topic_opts] ||= {} params[:topic_opts][:custom_fields] ||= {} params[:topic_opts][:custom_fields][keyArr.last] = value - elsif keyArr.first === 'post' + else params[:custom_fields] ||= {} params[:custom_fields][keyArr.last.to_sym] = value end @@ -249,8 +259,6 @@ class CustomWizard::Action mapper.interpolate(action['post_template']) : data[action['post']] - params = add_custom_fields(params) - - params + add_custom_fields(params) end end \ No newline at end of file diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index 60c0a4aa..697f9e6d 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -1,8 +1,13 @@ class CustomWizard::Mapper attr_accessor :inputs, :data, :user - USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale', 'trust_level'] + USER_FIELDS = ['name', 'email', 'date_of_birth', 'title', 'locale', 'trust_level'] PROFILE_FIELDS = ['location', 'website', 'bio_raw'] + + def self.user_fields + USER_FIELDS + PROFILE_FIELDS + end + OPERATORS = { equal: '==', greater: '>',