diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index 48b84011b48..bd0e36c5fab 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -256,10 +256,10 @@ const User = RestModel.extend({ }); }, - pickAvatar(upload_id, avatar_template) { + pickAvatar(upload_id, type, avatar_template) { return Discourse.ajax(`/users/${this.get("username_lower")}/preferences/avatar/pick`, { type: 'PUT', - data: { upload_id } + data: { upload_id, type } }).then(() => this.setProperties({ avatar_template, uploaded_avatar_id: upload_id diff --git a/app/assets/javascripts/discourse/routes/preferences.js.es6 b/app/assets/javascripts/discourse/routes/preferences.js.es6 index e1f9d597da7..8bea9b46047 100644 --- a/app/assets/javascripts/discourse/routes/preferences.js.es6 +++ b/app/assets/javascripts/discourse/routes/preferences.js.es6 @@ -49,9 +49,12 @@ export default RestrictedUserRoute.extend({ const user = this.modelFor('user'), controller = this.controllerFor('avatar-selector'), selectedUploadId = controller.get("selectedUploadId"), - selectedAvatarTemplate = controller.get("selectedAvatarTemplate"); + selectedAvatarTemplate = controller.get("selectedAvatarTemplate"), + type = controller.get("selected"); - user.pickAvatar(selectedUploadId, selectedAvatarTemplate) + if (type === "uploaded") { type = "custom" } + + user.pickAvatar(selectedUploadId, type, selectedAvatarTemplate) .then(() => { user.setProperties(controller.getProperties( 'system_avatar_template', diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 22334cc9b99..ab1104e8d44 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -515,13 +515,13 @@ class UsersController < ApplicationController results = UserSearch.new(term, topic_id: topic_id, topic_allowed_users: topic_allowed_users, searching_user: current_user).search - user_fields = [:username, :upload_avatar_template, :uploaded_avatar_id] + user_fields = [:username, :upload_avatar_template] user_fields << :name if SiteSetting.enable_names? to_render = { users: results.as_json(only: user_fields, methods: [:avatar_template]) } if params[:include_groups] == "true" - to_render[:groups] = Group.search_group(term, current_user).map {|m| {:name=>m.name, :usernames=> m.usernames.split(",")} } + to_render[:groups] = Group.search_group(term, current_user).map { |m| { name: m.name, usernames: m.usernames.split(",") } } end render json: to_render @@ -533,12 +533,11 @@ class UsersController < ApplicationController upload_id = params[:upload_id] - user.uploaded_avatar_id = upload_id + type = params[:type] + type = "custom" if type == "uploaded" - # ensure we associate the custom avatar properly - if upload_id && user.user_avatar.custom_upload_id != upload_id - user.user_avatar.custom_upload_id = upload_id - end + user.uploaded_avatar_id = upload_id + user.user_avatar.send("#{type}_upload_id=", upload_id) user.save! user.user_avatar.save! diff --git a/app/models/upload.rb b/app/models/upload.rb index 39e5fb94efc..bcc468d4450 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -50,7 +50,7 @@ class Upload < ActiveRecord::Base end # list of image types that will be cropped - CROPPED_IMAGE_TYPES ||= ["avatar", "profile_background", "card_background"] + CROPPED_IMAGE_TYPES ||= %w{avatar profile_background card_background} # options # - content_type diff --git a/app/models/user.rb b/app/models/user.rb index b00ac21b3a2..a17e4e9baa4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -462,8 +462,8 @@ class User < ActiveRecord::Base end def self.avatar_template(username, uploaded_avatar_id) - return default_template(username) if !uploaded_avatar_id username ||= "" + return default_template(username) if !uploaded_avatar_id hostname = RailsMultisite::ConnectionManagement.current_hostname UserAvatar.local_avatar_template(hostname, username.downcase, uploaded_avatar_id) end @@ -482,7 +482,7 @@ class User < ActiveRecord::Base end def self.letter_avatar_color(username) - username = username || "" + username ||= "" color = LetterAvatar::COLORS[Digest::MD5.hexdigest(username)[0...15].to_i(16) % LetterAvatar::COLORS.length] color.map { |c| c.to_s(16).rjust(2, '0') }.join end diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index f61df736bf7..dd17fd6e3ed 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -39,8 +39,7 @@ class UserAvatar < ActiveRecord::Base end def self.local_avatar_url(hostname, username, upload_id, size) - version = self.version(upload_id) - "#{Discourse.base_uri}/user_avatar/#{hostname}/#{username}/#{size}/#{version}.png" + self.local_avatar_template(hostname, username, upload_id).gsub("{size}", size) end def self.local_avatar_template(hostname, username, upload_id) @@ -49,8 +48,7 @@ class UserAvatar < ActiveRecord::Base end def self.external_avatar_url(user_id, upload_id, size) - version = self.version(upload_id) - "#{Discourse.store.absolute_base_url}/avatars/#{user_id}/#{size}/#{version}.png" + self.external_avatar_template(user_id, upload_id).gsub("{size}", size) end def self.external_avatar_template(user_id, upload_id) diff --git a/app/serializers/admin_post_serializer.rb b/app/serializers/admin_post_serializer.rb index 2dd29df5c03..a06328d832f 100644 --- a/app/serializers/admin_post_serializer.rb +++ b/app/serializers/admin_post_serializer.rb @@ -3,7 +3,7 @@ class AdminPostSerializer < ApplicationSerializer attributes :id, :created_at, :post_number, - :name, :username, :avatar_template, :uploaded_avatar_id, + :name, :username, :avatar_template, :topic_id, :topic_slug, :topic_title, :category_id, :excerpt, @@ -29,10 +29,6 @@ class AdminPostSerializer < ApplicationSerializer object.user.avatar_template end - def uploaded_avatar_id - object.user.uploaded_avatar_id - end - def topic_slug topic.slug end diff --git a/app/serializers/post_action_user_serializer.rb b/app/serializers/post_action_user_serializer.rb index b69a2734822..72dbd8e4d82 100644 --- a/app/serializers/post_action_user_serializer.rb +++ b/app/serializers/post_action_user_serializer.rb @@ -9,10 +9,6 @@ class PostActionUserSerializer < BasicUserSerializer object.user.username end - def uploaded_avatar_id - object.user.uploaded_avatar_id - end - def avatar_template object.user.avatar_template end diff --git a/app/serializers/topic_post_count_serializer.rb b/app/serializers/topic_post_count_serializer.rb index 586f9f2d79d..c780d120312 100644 --- a/app/serializers/topic_post_count_serializer.rb +++ b/app/serializers/topic_post_count_serializer.rb @@ -14,8 +14,4 @@ class TopicPostCountSerializer < BasicUserSerializer object[:post_count] end - def uploaded_avatar_id - object[:user].uploaded_avatar_id - end - end diff --git a/lib/avatar_lookup.rb b/lib/avatar_lookup.rb index b1fc1c61692..4af184052a1 100644 --- a/lib/avatar_lookup.rb +++ b/lib/avatar_lookup.rb @@ -12,10 +12,7 @@ class AvatarLookup private def self.lookup_columns - @lookup_columns ||= [:id, - :email, - :username, - :uploaded_avatar_id] + @lookup_columns ||= %i{id email username uploaded_avatar_id} end def users