forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavatar_detector.rb
More file actions
32 lines (25 loc) · 766 Bytes
/
Copy pathavatar_detector.rb
File metadata and controls
32 lines (25 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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