mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
FIX: download avatar from facebook/twitter in a job in order to prevent hangs when avatars are huge
This commit is contained in:
20
app/jobs/regular/download_avatar_from_url.rb
Normal file
20
app/jobs/regular/download_avatar_from_url.rb
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
module Jobs
|
||||||
|
|
||||||
|
class DownloadAvatarFromUrl < Jobs::Base
|
||||||
|
sidekiq_options retry: false
|
||||||
|
|
||||||
|
def execute(args)
|
||||||
|
url = args[:url]
|
||||||
|
user_id = args[:user_id]
|
||||||
|
|
||||||
|
raise Discourse::InvalidParameters.new(:url) if url.blank?
|
||||||
|
raise Discourse::InvalidParameters.new(:user_id) if user_id.blank?
|
||||||
|
|
||||||
|
return unless user = User.find_by(id: user_id)
|
||||||
|
|
||||||
|
UserAvatar.import_url_for_user(url, user, override_gravatar: args[:override_gravatar])
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -7,7 +7,6 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def after_authenticate(auth_token)
|
def after_authenticate(auth_token)
|
||||||
|
|
||||||
result = Auth::Result.new
|
result = Auth::Result.new
|
||||||
|
|
||||||
session_info = parse_auth_token(auth_token)
|
session_info = parse_auth_token(auth_token)
|
||||||
@ -20,37 +19,16 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
|
|||||||
result.extra_data = facebook_hash
|
result.extra_data = facebook_hash
|
||||||
|
|
||||||
user_info = FacebookUserInfo.find_by(facebook_user_id: facebook_hash[:facebook_user_id])
|
user_info = FacebookUserInfo.find_by(facebook_user_id: facebook_hash[:facebook_user_id])
|
||||||
|
|
||||||
result.user = user_info.try(:user)
|
result.user = user_info.try(:user)
|
||||||
|
|
||||||
if !result.user && !email.blank? && result.user = User.find_by_email(email)
|
if !result.user && !email.blank? && result.user = User.find_by_email(email)
|
||||||
FacebookUserInfo.create({user_id: result.user.id}.merge(facebook_hash))
|
FacebookUserInfo.create({ user_id: result.user.id }.merge(facebook_hash))
|
||||||
end
|
end
|
||||||
|
|
||||||
if user_info
|
user_info.update_columns(facebook_hash) if user_info
|
||||||
user_info.update_columns(facebook_hash)
|
|
||||||
end
|
|
||||||
|
|
||||||
user = result.user
|
retrieve_avatar(result.user, result.extra_data)
|
||||||
if user && (!user.user_avatar || user.user_avatar.custom_upload_id.nil?)
|
retrieve_profile(result.user, result.extra_data)
|
||||||
if (avatar_url = facebook_hash[:avatar_url]).present?
|
|
||||||
avatar_url_with_parameters = add_avatar_parameters(avatar_url)
|
|
||||||
UserAvatar.import_url_for_user(avatar_url_with_parameters, user, override_gravatar: false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
bio = facebook_hash[:about_me] || facebook_hash[:about]
|
|
||||||
location = facebook_hash[:location]
|
|
||||||
website = facebook_hash[:website]
|
|
||||||
|
|
||||||
if user && (bio || location || website)
|
|
||||||
profile = user.user_profile
|
|
||||||
|
|
||||||
profile.bio_raw = bio unless profile.bio_raw.present?
|
|
||||||
profile.location = location unless profile.location.present?
|
|
||||||
profile.website = website unless profile.website.present?
|
|
||||||
profile.save
|
|
||||||
end
|
|
||||||
|
|
||||||
if email.blank?
|
if email.blank?
|
||||||
UserHistory.create(
|
UserHistory.create(
|
||||||
@ -63,32 +41,16 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def after_create_account(user, auth)
|
def after_create_account(user, auth)
|
||||||
data = auth[:extra_data]
|
extra_data = auth[:extra_data]
|
||||||
FacebookUserInfo.create({user_id: user.id}.merge(data))
|
FacebookUserInfo.create({ user_id: user.id }.merge(extra_data))
|
||||||
|
|
||||||
|
retrieve_avatar(user, extra_data)
|
||||||
if (avatar_url = data[:avatar_url]).present?
|
retrieve_profile(user, extra_data)
|
||||||
avatar_url_with_parameters = add_avatar_parameters(avatar_url)
|
|
||||||
UserAvatar.import_url_for_user(avatar_url_with_parameters, user)
|
|
||||||
user.save
|
|
||||||
end
|
|
||||||
|
|
||||||
bio = data[:about_me]
|
|
||||||
location = data[:location]
|
|
||||||
website = data[:website]
|
|
||||||
|
|
||||||
if bio || location || website
|
|
||||||
user.user_profile.bio_raw = bio
|
|
||||||
user.user_profile.location = location
|
|
||||||
user.user_profile.website = website
|
|
||||||
user.user_profile.save
|
|
||||||
end
|
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_middleware(omniauth)
|
def register_middleware(omniauth)
|
||||||
|
|
||||||
omniauth.provider :facebook,
|
omniauth.provider :facebook,
|
||||||
:setup => lambda { |env|
|
:setup => lambda { |env|
|
||||||
strategy = env["omniauth.strategy"]
|
strategy = env["omniauth.strategy"]
|
||||||
@ -105,7 +67,6 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
def parse_auth_token(auth_token)
|
def parse_auth_token(auth_token)
|
||||||
|
|
||||||
raw_info = auth_token["extra"]["raw_info"]
|
raw_info = auth_token["extra"]["raw_info"]
|
||||||
info = auth_token["info"]
|
info = auth_token["info"]
|
||||||
|
|
||||||
@ -131,11 +92,32 @@ class Auth::FacebookAuthenticator < Auth::Authenticator
|
|||||||
email: email,
|
email: email,
|
||||||
email_valid: true
|
email_valid: true
|
||||||
}
|
}
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_avatar_parameters(avatar_url)
|
def retrieve_avatar(user, data)
|
||||||
"#{avatar_url}?height=#{AVATAR_SIZE}&width=#{AVATAR_SIZE}"
|
return unless user
|
||||||
|
return if user.user_avatar.try(:custom_upload_id).present?
|
||||||
|
|
||||||
|
if (avatar_url = data[:avatar_url]).present?
|
||||||
|
url = "#{avatar_url}?height=#{AVATAR_SIZE}&width=#{AVATAR_SIZE}"
|
||||||
|
Jobs.enqueue(:download_avatar_from_url, url: url, user_id: user.id, override_gravatar: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def retrieve_profile(user, data)
|
||||||
|
return unless user
|
||||||
|
|
||||||
|
bio = data[:about_me] || data[:about]
|
||||||
|
location = data[:location]
|
||||||
|
website = data[:website]
|
||||||
|
|
||||||
|
if bio || location || website
|
||||||
|
profile = user.user_profile
|
||||||
|
profile.bio_raw = bio unless profile.bio_raw.present?
|
||||||
|
profile.location = location unless profile.location.present?
|
||||||
|
profile.website = website unless profile.website.present?
|
||||||
|
profile.save
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -51,6 +51,8 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
|||||||
|
|
||||||
retrieve_avatar(user, extra_data)
|
retrieve_avatar(user, extra_data)
|
||||||
retrieve_profile(user, extra_data)
|
retrieve_profile(user, extra_data)
|
||||||
|
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_middleware(omniauth)
|
def register_middleware(omniauth)
|
||||||
@ -62,12 +64,15 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
def retrieve_avatar(user, data)
|
def retrieve_avatar(user, data)
|
||||||
return unless user
|
return unless user
|
||||||
return if user.user_avatar.try(:custom_upload_id).present?
|
return if user.user_avatar.try(:custom_upload_id).present?
|
||||||
|
|
||||||
if (avatar_url = data[:twitter_image]).present?
|
if (avatar_url = data[:twitter_image]).present?
|
||||||
UserAvatar.import_url_for_user(avatar_url.sub("_normal", ""), user, override_gravatar: false)
|
url = avatar_url.sub("_normal", "")
|
||||||
|
Jobs.enqueue(:download_avatar_from_url, url: url, user_id: user.id, override_gravatar: false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -77,7 +82,7 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
|||||||
bio = data[:twitter_description]
|
bio = data[:twitter_description]
|
||||||
location = data[:twitter_location]
|
location = data[:twitter_location]
|
||||||
|
|
||||||
if user && (bio || location)
|
if bio || location
|
||||||
profile = user.user_profile
|
profile = user.user_profile
|
||||||
profile.bio_raw = bio unless profile.bio_raw.present?
|
profile.bio_raw = bio unless profile.bio_raw.present?
|
||||||
profile.location = location unless profile.location.present?
|
profile.location = location unless profile.location.present?
|
||||||
|
Reference in New Issue
Block a user