2014-05-22 03:37:02 -04:00
|
|
|
require_dependency 'letter_avatar'
|
|
|
|
|
|
|
|
class UserAvatar < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :gravatar_upload, class_name: 'Upload', dependent: :destroy
|
|
|
|
belongs_to :custom_upload, class_name: 'Upload', dependent: :destroy
|
|
|
|
|
|
|
|
def contains_upload?(id)
|
2014-05-30 00:17:35 -04:00
|
|
|
gravatar_upload_id == id || custom_upload_id == id
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_gravatar!
|
2015-05-06 19:00:13 -04:00
|
|
|
DistributedMutex.synchronize("update_gravatar_#{user.id}") do
|
|
|
|
begin
|
|
|
|
# special logic for our system user
|
|
|
|
email_hash = user.id == Discourse::SYSTEM_USER_ID ? User.email_hash("info@discourse.org") : user.email_hash
|
|
|
|
|
|
|
|
self.last_gravatar_download_attempt = Time.new
|
|
|
|
|
|
|
|
gravatar_url = "http://www.gravatar.com/avatar/#{email_hash}.png?s=500&d=404"
|
|
|
|
tempfile = FileHelper.download(gravatar_url, SiteSetting.max_image_size_kb.kilobytes, "gravatar")
|
|
|
|
upload = Upload.create_for(user.id, tempfile, 'gravatar.png', tempfile.size, { origin: gravatar_url })
|
|
|
|
|
|
|
|
if gravatar_upload_id != upload.id
|
|
|
|
gravatar_upload.try(:destroy!)
|
|
|
|
self.gravatar_upload = upload
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
rescue OpenURI::HTTPError
|
|
|
|
save!
|
|
|
|
rescue SocketError
|
|
|
|
# skip saving, we are not connected to the net
|
|
|
|
Rails.logger.warn "Failed to download gravatar, socket error - user id #{user.id}"
|
|
|
|
ensure
|
|
|
|
tempfile.try(:close!)
|
|
|
|
end
|
2014-05-26 05:46:43 -04:00
|
|
|
end
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_avatars
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# custom_upload_id :integer
|
|
|
|
# gravatar_upload_id :integer
|
|
|
|
# last_gravatar_download_attempt :datetime
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-05-27 21:50:49 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_avatars_on_user_id (user_id)
|
2014-05-22 03:37:02 -04:00
|
|
|
#
|