From 405a4e3ffab1ab6adbf583db1b20159ca39bc30c Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 12 Dec 2022 13:19:04 +0100 Subject: [PATCH] Add avatar interpolation support --- .../components/wizard-text-editor.js.es6 | 3 +- lib/custom_wizard/mapper.rb | 13 ++++++++ spec/components/custom_wizard/mapper_spec.rb | 31 ++++++++++++++++++- spec/fixtures/mapper/inputs.json | 8 +++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 index 88d7200c..50e5c2e1 100644 --- a/assets/javascripts/discourse/components/wizard-text-editor.js.es6 +++ b/assets/javascripts/discourse/components/wizard-text-editor.js.es6 @@ -6,7 +6,6 @@ import Component from "@ember/component"; import I18n from "I18n"; const excludedUserProperties = [ - "avatar", "profile_background", "card_background", ]; @@ -57,7 +56,7 @@ export default Component.extend({ @discourseComputed("wizardActions") wizardActionList(wizardActions) { - return wizardActions.map((a) => ` w{${a.id}}`); + return (wizardActions || []).map((a) => ` w{${a.id}}`); }, actions: { diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index aa444de1..b677a710 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -211,6 +211,8 @@ class CustomWizard::Mapper user.send(value) elsif USER_OPTION_FIELDS.include?(value) user.user_option.send(value) + elsif value.include?('avatar') + get_avatar_url(value) else nil end @@ -269,4 +271,15 @@ class CustomWizard::Mapper def bool(value) ActiveRecord::Type::Boolean.new.cast(value) end + + def get_avatar_url(value) + parts = value.split('.') + valid_sizes = Discourse.avatar_sizes.to_a + + if value === 'avatar' || parts.size === 1 || valid_sizes.exclude?(parts.last.to_i) + user.small_avatar_url + else + user.avatar_template_url.gsub("{size}", parts.last) + end + end end diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 111558a4..9fd02e9d 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - describe CustomWizard::Mapper do fab!(:user1) { Fabricate(:user, @@ -254,6 +253,36 @@ describe CustomWizard::Mapper do user: user1 ).perform).to eq("Time: #{Time.now.strftime("%B %-d, %Y")}") end + + it "avatar" do + expect(CustomWizard::Mapper.new( + inputs: inputs['interpolate_avatar'], + data: data, + user: user1 + ).perform).to eq("Avatar: ![avatar](#{user1.small_avatar_url})") + end + + it "avatar with invalid size" do + avatar_inputs = inputs['interpolate_avatar'].dup + avatar_inputs[0]["output"] = "Avatar: ![avatar](u{avatar.345})" + + expect(CustomWizard::Mapper.new( + inputs: avatar_inputs, + data: data, + user: user1 + ).perform).to eq("Avatar: ![avatar](#{user1.small_avatar_url})") + end + + it "avatar with valid size" do + avatar_inputs = inputs['interpolate_avatar'].dup + avatar_inputs[0]["output"] = "Avatar: ![avatar](u{avatar.120})" + + expect(CustomWizard::Mapper.new( + inputs: avatar_inputs, + data: data, + user: user1 + ).perform).to eq("Avatar: ![avatar](#{user1.avatar_template_url.gsub("{size}", "120")})") + end end it "handles greater than pairs" do diff --git a/spec/fixtures/mapper/inputs.json b/spec/fixtures/mapper/inputs.json index 443f186b..3fd406a4 100644 --- a/spec/fixtures/mapper/inputs.json +++ b/spec/fixtures/mapper/inputs.json @@ -89,6 +89,14 @@ "output": "Time: v{time}" } ], + "interpolate_avatar": [ + { + "type": "assignment", + "output_type": "text", + "output_connector": "set", + "output": "Avatar: ![avatar](u{avatar})" + } + ], "validation": [ { "type": "validation",