diff --git a/app/models/user.rb b/app/models/user.rb index c2a8ece8963..59f51326964 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -300,14 +300,15 @@ class User < ActiveRecord::Base template.gsub("{size}", "45") end + # the avatars might take a while to generate + # so return the url of the original image in the meantime + def uploaded_avatar_path + return unless SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar + uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url) + end + def avatar_template - if SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar - # the avatars might take a while to generate - # so return the url of the original image in the meantime - uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url) - else - User.gravatar_template(email) - end + uploaded_avatar_path || User.gravatar_template(email) end # Updates the denormalized view counts for all users diff --git a/lib/avatar_detector.rb b/lib/avatar_detector.rb new file mode 100644 index 00000000000..4841e96ef75 --- /dev/null +++ b/lib/avatar_detector.rb @@ -0,0 +1,32 @@ +require_dependency 'user' +require 'net/http' + +class AvatarDetector + + def initialize(user) + raise "Tried to detect an avatar on a non-user instance" unless user && user.is_a?(User) + + @user = user + end + + def has_custom_avatar? + return true if @user.uploaded_avatar_path + has_custom_gravatar? + end + + # Check whether the user has a gravatar by performing a HTTP HEAD request to + # Gravatar using the `d=404` parameter. + def has_custom_gravatar? + result = Net::HTTP.start('www.gravatar.com') do |http| + http.open_timeout = 2 + http.read_timeout = 2 + http.head("/avatar/#{User.email_hash(@user.email)}?d=404") + end + + return result.code.to_i == 200 + rescue + # If the HTTP request fails, assume no gravatar + false + end + +end diff --git a/spec/components/avatar_detector_spec.rb b/spec/components/avatar_detector_spec.rb new file mode 100644 index 00000000000..d994007fabf --- /dev/null +++ b/spec/components/avatar_detector_spec.rb @@ -0,0 +1,67 @@ +# encoding: utf-8 +require 'spec_helper' +require_dependency 'avatar_detector' + +describe AvatarDetector do + + describe "construction" do + + it "raises an error without a user" do + -> { AvatarDetector.new(nil) }.should raise_error + end + + it "raises an error on a non-user object" do + -> { AvatarDetector.new(Array.new) }.should raise_error + end + + end + + describe "has_custom_avatar?" do + + describe "with a user" do + let(:user) { User.new(use_uploaded_avatar: true) } + let(:avatar_detector) { AvatarDetector.new(user) } + + describe "when the user doesn't have an uploaded_avatar_path" do + + before do + user.stubs(:uploaded_avatar_path) + end + + it "returns true if they have a custom gravatar" do + avatar_detector.expects(:has_custom_gravatar?).returns(true) + avatar_detector.has_custom_avatar?.should be_true + end + + it "returns false if they don't have a custom gravatar" do + avatar_detector.expects(:has_custom_gravatar?).returns(false) + avatar_detector.has_custom_avatar?.should be_false + end + end + + + context "when the user doesn't have an uploaded_avatar_path" do + let(:user) { User.new(use_uploaded_avatar: true) } + let(:avatar_detector) { AvatarDetector.new(user) } + + describe "when the user has an uploaded avatar" do + before do + user.expects(:uploaded_avatar_path).returns("/some/uploaded/file.png") + end + + it "returns true" do + avatar_detector.has_custom_avatar?.should be_true + end + + it "doesn't call has_custom_gravatar" do + avatar_detector.expects(:has_custom_gravatar?).never + avatar_detector.has_custom_avatar? + end + + end + end + + end + end + +end \ No newline at end of file