diff --git a/app/assets/javascripts/discourse/helpers/application_helpers.js b/app/assets/javascripts/discourse/helpers/application_helpers.js index 708c8d08b56..3eff90d74b1 100644 --- a/app/assets/javascripts/discourse/helpers/application_helpers.js +++ b/app/assets/javascripts/discourse/helpers/application_helpers.js @@ -4,21 +4,13 @@ var safe = Handlebars.SafeString; var registerUnbound = require('discourse/helpers/register-unbound', null, null, true).default; var avatarTemplate = require('discourse/lib/avatar-template', null, null, true).default; -/** - Bound avatar helper. - - @method bound-avatar - @for Handlebars -**/ Em.Handlebars.helper('bound-avatar', function(user, size, uploadId) { if (Em.isEmpty(user)) { return new safe("
"); } var username = Em.get(user, 'username'); - if (arguments.length < 4) { uploadId = Em.get(user, 'uploaded_avatar_id'); } - var avatar = Em.get(user, 'avatar_template') || avatarTemplate(username, uploadId); return new safe(Discourse.Utilities.avatarImg({ size: size, avatarTemplate: avatar })); diff --git a/app/models/user.rb b/app/models/user.rb index e0c302031f6..da3507ef9db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -429,8 +429,23 @@ class User < ActiveRecord::Base UrlHelper.schemaless UrlHelper.absolute avatar_template end + def self.default_template(username) + if SiteSetting.default_avatars.present? + split_avatars = SiteSetting.default_avatars.split("\n") + if split_avatars.present? + hash = username.each_char.reduce(0) do |result, char| + [((result << 5) - result) + char.ord].pack('L').unpack('l').first + end + + avatar_template = split_avatars[hash.abs % split_avatars.size] + end + else + "#{Discourse.base_uri}/letter_avatar/#{username.downcase}/{size}/#{LetterAvatar.version}.png" + end + end + def self.avatar_template(username,uploaded_avatar_id) - return letter_avatar_template(username) if !uploaded_avatar_id + return default_template(username) if !uploaded_avatar_id username ||= "" hostname = RailsMultisite::ConnectionManagement.current_hostname UserAvatar.local_avatar_template(hostname, username.downcase, uploaded_avatar_id) diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 1857885f97c..becdfce8f79 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -25,15 +25,8 @@ module PrettyText return "" unless user.present? # TODO: Add support for ES6 and call `avatar-template` directly - if !user.uploaded_avatar_id && SiteSetting.default_avatars.present? - split_avatars = SiteSetting.default_avatars.split("\n") - if split_avatars.present? - hash = username.each_char.reduce(0) do |result, char| - [((result << 5) - result) + char.ord].pack('L').unpack('l').first - end - - avatar_template = split_avatars[hash.abs % split_avatars.size] - end + if !user.uploaded_avatar_id + avatar_template = User.default_template(username) else avatar_template = user.avatar_template end diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 5da48e1e932..9416d0a681d 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -9,10 +9,11 @@ describe PrettyText do describe "Cooking" do describe "with avatar" do + let(:default_avatar) { "//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png" } before(:each) do eviltrout = User.new - eviltrout.stubs(:avatar_template).returns("//test.localhost/uploads/default/avatars/42d/57c/46ce7ee487/{size}.png") + User.stubs(:default_template).returns(default_avatar) User.expects(:find_by).with(username_lower: "eviltrout").returns(eviltrout) end