FIX: don't overwrite custom uploaded avatar when selecting gravatar

FIX: remove unecessary serialized fields
This commit is contained in:
Régis Hanol 2015-09-11 15:04:29 +02:00
parent 569f2815d1
commit 93f9dcfcec
10 changed files with 20 additions and 35 deletions

View file

@ -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

View file

@ -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',

View file

@ -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!

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -14,8 +14,4 @@ class TopicPostCountSerializer < BasicUserSerializer
object[:post_count]
end
def uploaded_avatar_id
object[:user].uploaded_avatar_id
end
end

View file

@ -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