A platform for community discussion. Free, open, simple.
UX: The Site's logo is the selected option when changing the system's user avatar. (#12861)
Roman Rizzicdbdb04909c4
diff --git a/app/assets/javascripts/discourse/app/controllers/avatar-selector.js b/app/assets/javascripts/discourse/app/controllers/avatar-selector.jsindex a7d7e2cf24a..965207f83cb 100644--- a/app/assets/javascripts/discourse/app/controllers/avatar-selector.js+++ b/app/assets/javascripts/discourse/app/controllers/avatar-selector.js@@ -11,6 +11,11 @@ export default Controller.extend(ModalFunctionality, {gravatarBaseUrl: setting("gravatar_base_url"),gravatarLoginUrl: setting("gravatar_login_url"),+ @discourseComputed("selected", "uploading")+ submitDisabled(selected, uploading) {+ return selected === "logo" || uploading;+ },+@discourseComputed("siteSettings.selectable_avatars_enabled","siteSettings.selectable_avatars"@@ -22,12 +27,20 @@ export default Controller.extend(ModalFunctionality, {},@discourseComputed(+ "user.use_logo_small_as_avatar","user.avatar_template","user.system_avatar_template","user.gravatar_avatar_template")- selected(avatarTemplate, systemAvatarTemplate, gravatarAvatarTemplate) {- if (avatarTemplate === systemAvatarTemplate) {+ selected(+ useLogo,+ avatarTemplate,+ systemAvatarTemplate,+ gravatarAvatarTemplate+ ) {+ if (useLogo) {+ return "logo";+ } else if (avatarTemplate === systemAvatarTemplate) {return "system";} else if (avatarTemplate === gravatarAvatarTemplate) {return "gravatar";diff --git a/app/assets/javascripts/discourse/app/templates/modal/avatar-selector.hbs b/app/assets/javascripts/discourse/app/templates/modal/avatar-selector.hbsindex bb86ad93f63..fe52ecf0be3 100644--- a/app/assets/javascripts/discourse/app/templates/modal/avatar-selector.hbs+++ b/app/assets/javascripts/discourse/app/templates/modal/avatar-selector.hbs@@ -8,6 +8,12 @@{{/each}}</div>{{else}}+ {{#if user.use_logo_small_as_avatar}}+ <div class="avatar-choice">+ {{radio-button id="logo-small" name="logo" value="logo" selection=selected}}+ <label class="radio" for="logo-small">{{bound-avatar-template siteSettings.site_logo_small_url "large"}} {{html-safe (i18n "user.change_avatar.logo_small")}}</label>+ </div>+ {{/if}}<div class="avatar-choice">{{radio-button id="system-avatar" name="avatar" value="system" selection=selected}}<label class="radio" for="system-avatar">{{bound-avatar-template user.system_avatar_template "large"}} {{html-safe (i18n "user.change_avatar.letter_based")}}</label>@@ -28,7 +34,7 @@{{/if}}</div><div class="avatar-choice">- {{radio-button id="uploaded-avatar" name="avatar" value="uploaded" selection=selected}}+ {{radio-button id="uploaded-avatar" name="avatar" value="custom" selection=selected}}<label class="radio" for="uploaded-avatar">{{#if user.custom_avatar_template}}{{bound-avatar-template user.custom_avatar_template "large"}}@@ -50,7 +56,7 @@{{#unless siteSettings.selectable_avatars_enabled}}<div class="modal-footer">- {{d-button action=(action "saveAvatarSelection") class="btn-primary" disabled=uploading label="save"}}+ {{d-button action=(action "saveAvatarSelection") class="btn-primary" disabled=submitDisabled label="save"}}{{d-modal-cancel close=(route-action "closeModal")}}</div>{{/unless}}diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 0940a32c760..bb796c35fda 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -1129,11 +1129,9 @@ class UsersController < ApplicationControllerif type.blank? || type == 'system'upload_id = nil+ elsif !SiteSetting.allow_uploaded_avatars+ return render json: failed_json, status: 422else- if !SiteSetting.allow_uploaded_avatars- return render json: failed_json, status: 422- end-upload_id = params[:upload_id]upload = Upload.find_by(id: upload_id)@@ -1153,6 +1151,10 @@ class UsersController < ApplicationControllerendend+ if user.is_system_user?+ SiteSetting.use_site_small_logo_as_system_avatar = false+ end+user.uploaded_avatar_id = upload_iduser.save!user.user_avatar.save!@@ -1187,6 +1189,11 @@ class UsersController < ApplicationControllerenduser.uploaded_avatar_id = upload.id++ if user.is_system_user?+ SiteSetting.use_site_small_logo_as_system_avatar = false+ end+user.save!avatar = user.user_avatar || user.create_user_avatardiff --git a/app/models/user.rb b/app/models/user.rbindex e9c7af53654..eb2489b16c5 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -872,8 +872,12 @@ class User < ActiveRecord::BaseDigest::MD5.hexdigest(username)[0...15].to_i(16) % lengthend+ def is_system_user?+ id == Discourse::SYSTEM_USER_ID+ end+def avatar_template- use_small_logo = id == Discourse::SYSTEM_USER_ID &&+ use_small_logo = is_system_user? &&SiteSetting.logo_small && SiteSetting.use_site_small_logo_as_system_avatarif use_small_logodiff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rbindex afe57d8e739..56d47786b83 100644--- a/app/serializers/user_serializer.rb+++ b/app/serializers/user_serializer.rb@@ -59,7 +59,8 @@ class UserSerializer < UserCardSerializer:can_change_website,:user_api_keys,:user_auth_tokens,- :user_notification_schedule+ :user_notification_schedule,+ :use_logo_small_as_avataruntrusted_attributes :bio_raw,:bio_cooked,@@ -309,6 +310,10 @@ class UserSerializer < UserCardSerializerobject.profile_background_upload&.urlend+ def use_logo_small_as_avatar+ object.is_system_user? && SiteSetting.logo_small && SiteSetting.use_site_small_logo_as_system_avatar+ end+privatedef custom_field_keysdiff --git a/app/serializers/web_hook_user_serializer.rb b/app/serializers/web_hook_user_serializer.rbindex 32fbde02f5f..43200e4fd67 100644--- a/app/serializers/web_hook_user_serializer.rb+++ b/app/serializers/web_hook_user_serializer.rb@@ -34,6 +34,7 @@ class WebHookUserSerializer < UserSerializergroup_usersuser_auth_tokensuser_auth_token_logs+ use_logo_small_as_avatar}.each do |attr|define_method("include_#{attr}?") dofalsediff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex b84d8d71dc3..0f97c5f963d 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -1268,6 +1268,7 @@ en:uploaded_avatar_empty: "Add a custom picture"upload_title: "Upload your picture"image_is_not_a_square: "Warning: we've cropped your image; width and height were not equal."+ logo_small: "Site's small logo. Used by default."change_profile_background:title: "Profile Header"diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 31c8b0750fe..d48cc547a59 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -2393,6 +2393,20 @@ describe UsersController doexpect(user.reload.uploaded_avatar_id).to eq(nil)end+ it 'disables the use_site_small_logo_as_system_avatar setting when picking an avatar for the system user' do+ system_user = Discourse.system_user+ SiteSetting.use_site_small_logo_as_system_avatar = true+ another_upload = Fabricate(:upload, user: system_user)+ sign_in(system_user)++ put "/u/#{system_user.username}/preferences/avatar/pick.json", params: {+ upload_id: another_upload.id, type: "uploaded"+ }++ expect(response.status).to eq(200)+ expect(SiteSetting.use_site_small_logo_as_system_avatar).to eq(false)+ end+it 'can successfully pick a gravatar' douser.user_avatar.update_columns(gravatar_upload_id: upload.id)@@ -2494,6 +2508,19 @@ describe UsersController doexpect(user.reload.uploaded_avatar_id).to eq(avatar1.id)expect(user.user_avatar.reload.custom_upload_id).to eq(avatar1.id)end++ it 'disables the use_site_small_logo_as_system_avatar setting when picking an avatar for the system user' do+ system_user = Discourse.system_user+ SiteSetting.use_site_small_logo_as_system_avatar = true+ sign_in(system_user)++ put "/u/#{system_user.username}/preferences/avatar/select.json", params: {+ url: UrlHelper.cook_url(avatar1.url)+ }++ expect(response.status).to eq(200)+ expect(SiteSetting.use_site_small_logo_as_system_avatar).to eq(false)+ endendendend