Adds a class that can detect whether a user has uploaded a custom avatar

This commit is contained in:
Robin Ward 2013-09-10 15:18:22 -04:00
parent 21e08a423e
commit 2319924206
3 changed files with 107 additions and 7 deletions

View file

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

32
lib/avatar_detector.rb Normal file
View file

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

View file

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