Skip to content

Commit

Permalink
FIX: don't overwrite custom uploaded avatar when selecting gravatar
Browse files Browse the repository at this point in the history
FIX: remove unecessary serialized fields
  • Loading branch information
ZogStriP committed Sep 11, 2015
1 parent 569f281 commit 93f9dcf
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/discourse/models/user.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions app/assets/javascripts/discourse/routes/preferences.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 6 additions & 7 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,13 @@ def search_users

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
Expand All @@ -533,12 +533,11 @@ def pick_avatar

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!
Expand Down
2 changes: 1 addition & 1 deletion app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def extension
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
Expand Down
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ def self.default_template(username)
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
Expand All @@ -482,7 +482,7 @@ def self.system_avatar_template(username)
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
Expand Down
6 changes: 2 additions & 4 deletions app/models/user_avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def update_gravatar!
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)
Expand All @@ -49,8 +48,7 @@ def self.local_avatar_template(hostname, username, upload_id)
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)
Expand Down
6 changes: 1 addition & 5 deletions app/serializers/admin_post_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,10 +29,6 @@ def avatar_template
object.user.avatar_template
end

def uploaded_avatar_id
object.user.uploaded_avatar_id
end

def topic_slug
topic.slug
end
Expand Down
4 changes: 0 additions & 4 deletions app/serializers/post_action_user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def username
object.user.username
end

def uploaded_avatar_id
object.user.uploaded_avatar_id
end

def avatar_template
object.user.avatar_template
end
Expand Down
4 changes: 0 additions & 4 deletions app/serializers/topic_post_count_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ def post_count
object[:post_count]
end

def uploaded_avatar_id
object[:user].uploaded_avatar_id
end

end
5 changes: 1 addition & 4 deletions lib/avatar_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ def [](user_id)
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
Expand Down

0 comments on commit 93f9dcf

Please sign in to comment.