Skip to content

Commit 2319924

Browse files
committed
Adds a class that can detect whether a user has uploaded a custom avatar
1 parent 21e08a4 commit 2319924

3 files changed

Lines changed: 107 additions & 7 deletions

File tree

app/models/user.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,15 @@ def small_avatar_url
300300
template.gsub("{size}", "45")
301301
end
302302

303+
# the avatars might take a while to generate
304+
# so return the url of the original image in the meantime
305+
def uploaded_avatar_path
306+
return unless SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar
307+
uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url)
308+
end
309+
303310
def avatar_template
304-
if SiteSetting.allow_uploaded_avatars? && use_uploaded_avatar
305-
# the avatars might take a while to generate
306-
# so return the url of the original image in the meantime
307-
uploaded_avatar_template.present? ? uploaded_avatar_template : uploaded_avatar.try(:url)
308-
else
309-
User.gravatar_template(email)
310-
end
311+
uploaded_avatar_path || User.gravatar_template(email)
311312
end
312313

313314
# Updates the denormalized view counts for all users

lib/avatar_detector.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require_dependency 'user'
2+
require 'net/http'
3+
4+
class AvatarDetector
5+
6+
def initialize(user)
7+
raise "Tried to detect an avatar on a non-user instance" unless user && user.is_a?(User)
8+
9+
@user = user
10+
end
11+
12+
def has_custom_avatar?
13+
return true if @user.uploaded_avatar_path
14+
has_custom_gravatar?
15+
end
16+
17+
# Check whether the user has a gravatar by performing a HTTP HEAD request to
18+
# Gravatar using the `d=404` parameter.
19+
def has_custom_gravatar?
20+
result = Net::HTTP.start('www.gravatar.com') do |http|
21+
http.open_timeout = 2
22+
http.read_timeout = 2
23+
http.head("/avatar/#{User.email_hash(@user.email)}?d=404")
24+
end
25+
26+
return result.code.to_i == 200
27+
rescue
28+
# If the HTTP request fails, assume no gravatar
29+
false
30+
end
31+
32+
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# encoding: utf-8
2+
require 'spec_helper'
3+
require_dependency 'avatar_detector'
4+
5+
describe AvatarDetector do
6+
7+
describe "construction" do
8+
9+
it "raises an error without a user" do
10+
-> { AvatarDetector.new(nil) }.should raise_error
11+
end
12+
13+
it "raises an error on a non-user object" do
14+
-> { AvatarDetector.new(Array.new) }.should raise_error
15+
end
16+
17+
end
18+
19+
describe "has_custom_avatar?" do
20+
21+
describe "with a user" do
22+
let(:user) { User.new(use_uploaded_avatar: true) }
23+
let(:avatar_detector) { AvatarDetector.new(user) }
24+
25+
describe "when the user doesn't have an uploaded_avatar_path" do
26+
27+
before do
28+
user.stubs(:uploaded_avatar_path)
29+
end
30+
31+
it "returns true if they have a custom gravatar" do
32+
avatar_detector.expects(:has_custom_gravatar?).returns(true)
33+
avatar_detector.has_custom_avatar?.should be_true
34+
end
35+
36+
it "returns false if they don't have a custom gravatar" do
37+
avatar_detector.expects(:has_custom_gravatar?).returns(false)
38+
avatar_detector.has_custom_avatar?.should be_false
39+
end
40+
end
41+
42+
43+
context "when the user doesn't have an uploaded_avatar_path" do
44+
let(:user) { User.new(use_uploaded_avatar: true) }
45+
let(:avatar_detector) { AvatarDetector.new(user) }
46+
47+
describe "when the user has an uploaded avatar" do
48+
before do
49+
user.expects(:uploaded_avatar_path).returns("/some/uploaded/file.png")
50+
end
51+
52+
it "returns true" do
53+
avatar_detector.has_custom_avatar?.should be_true
54+
end
55+
56+
it "doesn't call has_custom_gravatar" do
57+
avatar_detector.expects(:has_custom_gravatar?).never
58+
avatar_detector.has_custom_avatar?
59+
end
60+
61+
end
62+
end
63+
64+
end
65+
end
66+
67+
end

0 commit comments

Comments
 (0)