2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
class UserAvatar < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2018-12-13 13:26:07 +08:00
|
|
|
belongs_to :gravatar_upload, class_name: 'Upload'
|
|
|
|
belongs_to :custom_upload, class_name: 'Upload'
|
2014-05-22 15:37:02 +08:00
|
|
|
|
2020-05-23 12:56:13 +08:00
|
|
|
@@custom_user_gravatar_email_hash = {
|
|
|
|
Discourse::SYSTEM_USER_ID => User.email_hash("info@discourse.org")
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.register_custom_user_gravatar_email_hash(user_id, email)
|
|
|
|
@@custom_user_gravatar_email_hash[user_id] = User.email_hash(email)
|
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
def contains_upload?(id)
|
2014-05-30 12:17:35 +08:00
|
|
|
gravatar_upload_id == id || custom_upload_id == id
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_gravatar!
|
2015-09-18 01:42:44 +08:00
|
|
|
DistributedMutex.synchronize("update_gravatar_#{user_id}") do
|
2015-05-07 07:00:13 +08:00
|
|
|
begin
|
2020-05-23 12:56:13 +08:00
|
|
|
self.update!(last_gravatar_download_attempt: Time.zone.now)
|
2015-05-07 07:00:13 +08:00
|
|
|
|
2015-05-30 00:09:47 +08:00
|
|
|
max = Discourse.avatar_sizes.max
|
2019-03-09 02:39:56 +08:00
|
|
|
|
|
|
|
# The user could be deleted before this executes
|
|
|
|
return if user.blank? || user.primary_email.blank?
|
|
|
|
|
2020-05-23 12:56:13 +08:00
|
|
|
email_hash = @@custom_user_gravatar_email_hash[user_id] || user.email_hash
|
2020-04-08 05:35:42 +08:00
|
|
|
gravatar_url = "https://#{SiteSetting.gravatar_base_url}/avatar/#{email_hash}.png?s=#{max}&d=404&reset_cache=#{SecureRandom.urlsafe_base64(5)}"
|
2017-09-28 14:35:27 +08:00
|
|
|
|
|
|
|
# follow redirects in case gravatar change rules on us
|
2017-05-25 01:42:52 +08:00
|
|
|
tempfile = FileHelper.download(
|
|
|
|
gravatar_url,
|
|
|
|
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
2017-05-25 01:46:57 +08:00
|
|
|
tmp_file_name: "gravatar",
|
2017-09-28 09:32:26 +08:00
|
|
|
skip_rate_limit: true,
|
2017-09-28 14:35:27 +08:00
|
|
|
verbose: false,
|
|
|
|
follow_redirect: true
|
2017-05-25 01:42:52 +08:00
|
|
|
)
|
2017-09-28 14:35:27 +08:00
|
|
|
|
2017-05-25 01:29:50 +08:00
|
|
|
if tempfile
|
2018-07-30 10:48:44 +08:00
|
|
|
ext = File.extname(tempfile)
|
|
|
|
ext = '.png' if ext.blank?
|
|
|
|
|
|
|
|
upload = UploadCreator.new(
|
|
|
|
tempfile,
|
|
|
|
"gravatar#{ext}",
|
|
|
|
origin: gravatar_url,
|
2019-07-31 11:16:03 +08:00
|
|
|
type: "avatar",
|
|
|
|
for_gravatar: true
|
2018-07-30 10:48:44 +08:00
|
|
|
).create_for(user_id)
|
2017-05-25 01:29:50 +08:00
|
|
|
|
2018-10-18 17:02:54 +08:00
|
|
|
if gravatar_upload_id != upload.id
|
2018-08-06 10:42:03 +08:00
|
|
|
User.transaction do
|
|
|
|
if gravatar_upload_id && user.uploaded_avatar_id == gravatar_upload_id
|
2018-10-18 17:02:54 +08:00
|
|
|
user.update!(uploaded_avatar_id: upload.id)
|
2018-08-06 10:42:03 +08:00
|
|
|
end
|
|
|
|
|
2018-10-19 09:51:34 +08:00
|
|
|
self.update!(gravatar_upload: upload)
|
2018-08-06 10:42:03 +08:00
|
|
|
end
|
2017-05-25 01:29:50 +08:00
|
|
|
end
|
2015-05-07 07:00:13 +08:00
|
|
|
end
|
2018-10-23 08:43:14 +08:00
|
|
|
rescue OpenURI::HTTPError => e
|
|
|
|
if e.io&.status[0].to_i != 404
|
|
|
|
raise e
|
|
|
|
end
|
2015-05-07 07:00:13 +08:00
|
|
|
ensure
|
2018-10-19 09:51:34 +08:00
|
|
|
tempfile&.close!
|
2015-05-07 07:00:13 +08:00
|
|
|
end
|
2014-05-26 17:46:43 +08:00
|
|
|
end
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
2015-05-30 00:51:17 +08:00
|
|
|
def self.local_avatar_url(hostname, username, upload_id, size)
|
2015-09-11 21:18:17 +08:00
|
|
|
self.local_avatar_template(hostname, username, upload_id).gsub("{size}", size.to_s)
|
2015-05-30 00:51:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_avatar_template(hostname, username, upload_id)
|
|
|
|
version = self.version(upload_id)
|
|
|
|
"#{Discourse.base_uri}/user_avatar/#{hostname}/#{username}/{size}/#{version}.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.external_avatar_url(user_id, upload_id, size)
|
2015-09-11 21:18:17 +08:00
|
|
|
self.external_avatar_template(user_id, upload_id).gsub("{size}", size.to_s)
|
2015-05-30 00:51:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.external_avatar_template(user_id, upload_id)
|
|
|
|
version = self.version(upload_id)
|
|
|
|
"#{Discourse.store.absolute_base_url}/avatars/#{user_id}/{size}/#{version}.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.version(upload_id)
|
|
|
|
"#{upload_id}_#{OptimizedImage::VERSION}"
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.import_url_for_user(avatar_url, user, options = nil)
|
2017-05-25 01:42:52 +08:00
|
|
|
tempfile = FileHelper.download(
|
|
|
|
avatar_url,
|
|
|
|
max_file_size: SiteSetting.max_image_size_kb.kilobytes,
|
|
|
|
tmp_file_name: "sso-avatar",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
2015-06-24 03:59:50 +08:00
|
|
|
|
2017-05-26 11:33:12 +08:00
|
|
|
return unless tempfile
|
|
|
|
|
2020-05-06 23:28:02 +08:00
|
|
|
ext = FastImage.type(tempfile).to_s
|
|
|
|
tempfile.rewind
|
2015-06-24 03:59:50 +08:00
|
|
|
|
2020-05-06 23:28:02 +08:00
|
|
|
upload = UploadCreator.new(tempfile, "external-avatar." + ext, origin: avatar_url, type: "avatar").create_for(user.id)
|
2015-06-24 03:59:50 +08:00
|
|
|
|
2018-08-23 15:36:34 +08:00
|
|
|
user.create_user_avatar! unless user.user_avatar
|
2015-06-24 03:59:50 +08:00
|
|
|
|
|
|
|
if !user.user_avatar.contains_upload?(upload.id)
|
2018-08-23 15:36:34 +08:00
|
|
|
user.user_avatar.update!(custom_upload_id: upload.id)
|
2020-05-06 23:28:02 +08:00
|
|
|
override_gravatar = !options || options[:override_gravatar]
|
2016-09-19 13:10:02 +08:00
|
|
|
|
|
|
|
if user.uploaded_avatar_id.nil? ||
|
|
|
|
!user.user_avatar.contains_upload?(user.uploaded_avatar_id) ||
|
|
|
|
override_gravatar
|
2018-08-23 15:36:34 +08:00
|
|
|
|
|
|
|
user.update!(uploaded_avatar_id: upload.id)
|
2016-09-19 13:10:02 +08:00
|
|
|
end
|
2015-06-24 03:59:50 +08:00
|
|
|
end
|
2020-05-06 23:28:02 +08:00
|
|
|
|
|
|
|
rescue Net::ReadTimeout, OpenURI::HTTPError
|
|
|
|
# skip saving, we are not connected to the net
|
|
|
|
ensure
|
|
|
|
tempfile.close! if tempfile && tempfile.respond_to?(:close!)
|
2015-06-24 03:59:50 +08:00
|
|
|
end
|
|
|
|
|
2018-08-31 12:46:22 +08:00
|
|
|
def self.ensure_consistency!
|
|
|
|
DB.exec <<~SQL
|
|
|
|
UPDATE user_avatars
|
|
|
|
SET gravatar_upload_id = NULL
|
|
|
|
WHERE gravatar_upload_id IN (
|
|
|
|
SELECT u1.gravatar_upload_id FROM user_avatars u1
|
|
|
|
LEFT JOIN uploads up
|
|
|
|
ON u1.gravatar_upload_id = up.id
|
|
|
|
WHERE u1.gravatar_upload_id IS NOT NULL AND
|
|
|
|
up.id IS NULL
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.exec <<~SQL
|
|
|
|
UPDATE user_avatars
|
|
|
|
SET custom_upload_id = NULL
|
|
|
|
WHERE custom_upload_id IN (
|
|
|
|
SELECT u1.custom_upload_id FROM user_avatars u1
|
|
|
|
LEFT JOIN uploads up
|
|
|
|
ON u1.custom_upload_id = up.id
|
|
|
|
WHERE u1.custom_upload_id IS NOT NULL AND
|
|
|
|
up.id IS NULL
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
2014-05-22 15:37:02 +08:00
|
|
|
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 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-05-28 09:50:49 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2016-11-24 10:13:03 +08:00
|
|
|
# index_user_avatars_on_custom_upload_id (custom_upload_id)
|
|
|
|
# index_user_avatars_on_gravatar_upload_id (gravatar_upload_id)
|
|
|
|
# index_user_avatars_on_user_id (user_id)
|
2014-05-22 15:37:02 +08:00
|
|
|
#
|