From 2c37fa381e80135b7b2e87163a46ca2ecee15925 Mon Sep 17 00:00:00 2001 From: Sergey Linnik Date: Mon, 19 Nov 2012 02:36:50 +0400 Subject: [PATCH 1/2] fix some glitches in gravatar image url generation always use proper size value do not show alt text when avatar image not loaded --- app/decorators/commit_decorator.rb | 2 +- app/helpers/application_helper.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/decorators/commit_decorator.rb b/app/decorators/commit_decorator.rb index 69d5b178..a066b2e4 100644 --- a/app/decorators/commit_decorator.rb +++ b/app/decorators/commit_decorator.rb @@ -76,7 +76,7 @@ class CommitDecorator < ApplicationDecorator source_name = send "#{options[:source]}_name".to_sym source_email = send "#{options[:source]}_email".to_sym text = if options[:avatar] - avatar = h.image_tag h.gravatar_icon(source_email, options[:size]), class: "avatar #{"s#{options[:size]}" if options[:size]}", width: options[:size] + avatar = h.image_tag h.gravatar_icon(source_email, options[:size]), class: "avatar #{"s#{options[:size]}" if options[:size]}", width: options[:size], alt: "" %Q{#{avatar} #{source_name}} else source_name diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a689213b..e130c095 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -30,7 +30,9 @@ module ApplicationHelper args.any? { |v| v.to_s.downcase == action_name } end - def gravatar_icon(user_email = '', size = 40) + def gravatar_icon(user_email = '', size = nil) + size = 40 if size.nil? || size <= 0 + if Gitlab.config.disable_gravatar? || user_email.blank? 'no_avatar.png' else From 0aa7f79ca4255eadd7d2aad7c3378915ee54e107 Mon Sep 17 00:00:00 2001 From: Sergey Linnik Date: Fri, 7 Dec 2012 00:44:22 +0400 Subject: [PATCH 2/2] allow customize gravatar url --- app/helpers/application_helper.rb | 5 +++-- config/gitlab.yml.example | 2 ++ config/initializers/1_settings.rb | 9 +++++++++ spec/helpers/application_helper_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e130c095..81be6242 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,5 @@ require 'digest/md5' +require 'uri' module ApplicationHelper @@ -36,9 +37,9 @@ module ApplicationHelper if Gitlab.config.disable_gravatar? || user_email.blank? 'no_avatar.png' else - gravatar_prefix = request.ssl? ? "https://secure" : "http://www" + gravatar_url = request.ssl? ? Gitlab.config.gravatar_ssl_url : Gitlab.config.gravatar_url user_email.strip! - "#{gravatar_prefix}.gravatar.com/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=mm" + sprintf(gravatar_url, {:hash => Digest::MD5.hexdigest(user_email.downcase), :email => URI.escape(user_email), :size => size}) end end diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index 35683489..7fcfc5b7 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -24,6 +24,8 @@ app: # backup_path: "/vol/backups" # default: Rails.root + backups/ # backup_keep_time: 604800 # default: 0 (forever) (in seconds) # disable_gravatar: true # default: false - Disable user avatars from Gravatar.com + # gravatar_url: "http://" # default: http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=mm + # gravatar_ssl_url: "https://" # default: https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=mm # diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index 32af3d07..e684f378 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -145,5 +145,14 @@ class Settings < Settingslogic def disable_gravatar? app['disable_gravatar'] || false end + + def gravatar_url + app['gravatar_url'] || 'http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=mm' + end + + def gravatar_ssl_url + app['gravatar_ssl_url'] || 'https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=mm' + end + end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index a94d5505..df66e3b4 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -51,14 +51,36 @@ describe ApplicationHelper do gravatar_icon('').should == 'no_avatar.png' end + it "should return default gravatar url" do + stub!(:request).and_return(double(:ssl? => false)) + gravatar_icon(user_email).should match('http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118') + end + it "should use SSL when appropriate" do stub!(:request).and_return(double(:ssl? => true)) gravatar_icon(user_email).should match('https://secure.gravatar.com') end + it "should return custom gravatar path when gravatar_url is set" do + stub!(:request).and_return(double(:ssl? => false)) + Gitlab.config.stub(:gravatar_url).and_return('http://example.local/?s=%{size}&hash=%{hash}') + gravatar_icon(user_email, 20).should == 'http://example.local/?s=20&hash=b58c6f14d292556214bd64909bcdb118' + end + it "should accept a custom size" do stub!(:request).and_return(double(:ssl? => false)) gravatar_icon(user_email, 64).should match(/\?s=64/) end + + it "should use default size when size is wrong" do + stub!(:request).and_return(double(:ssl? => false)) + gravatar_icon(user_email, nil).should match(/\?s=40/) + end + + it "should be case insensitive" do + stub!(:request).and_return(double(:ssl? => false)) + gravatar_icon(user_email).should == gravatar_icon(user_email.upcase + " ") + end + end end