discourse/app/models/user_avatar.rb

54 lines
1.7 KiB
Ruby
Raw Normal View History

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)
gravatar_upload_id == id || custom_upload_id == id
end
def update_gravatar!
2014-05-26 22:38:16 -04:00
# special logic for our system user, we do not want the discourse email there
email_hash = user.id == -1 ? User.email_hash("info@discourse.org") : user.email_hash
self.last_gravatar_download_attempt = Time.new
2014-05-26 22:38:16 -04:00
gravatar_url = "http://www.gravatar.com/avatar/#{email_hash}.png?s=500&d=404"
2015-02-03 12:44:18 -05:00
tempfile = FileHelper.download(gravatar_url, SiteSetting.max_image_size_kb.kilobytes, "gravatar")
2015-02-03 12:44:18 -05:00
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
2014-05-26 07:17:20 -04:00
rescue OpenURI::HTTPError
save!
rescue SocketError
# skip saving, we are not connected to the net
2015-02-03 12:44:18 -05:00
Rails.logger.warn "Failed to download gravatar, socket error - user id #{user.id}"
ensure
2014-06-26 13:50:16 -04:00
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
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
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_user_avatars_on_user_id (user_id)
#