mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +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:
parent
b31fa878bb
commit
923db2e559
3 changed files with 97 additions and 90 deletions
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"]
|
||||||
|
@ -104,38 +66,58 @@ 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"]
|
||||||
|
info = auth_token["info"]
|
||||||
|
|
||||||
raw_info = auth_token["extra"]["raw_info"]
|
email = auth_token["info"][:email]
|
||||||
info = auth_token["info"]
|
|
||||||
|
|
||||||
email = auth_token["info"][:email]
|
website = (info["urls"] && info["urls"]["Website"]) || nil
|
||||||
|
|
||||||
website = (info["urls"] && info["urls"]["Website"]) || nil
|
{
|
||||||
|
facebook: {
|
||||||
{
|
facebook_user_id: auth_token["uid"],
|
||||||
facebook: {
|
link: raw_info["link"],
|
||||||
facebook_user_id: auth_token["uid"],
|
username: raw_info["username"],
|
||||||
link: raw_info["link"],
|
first_name: raw_info["first_name"],
|
||||||
username: raw_info["username"],
|
last_name: raw_info["last_name"],
|
||||||
first_name: raw_info["first_name"],
|
email: email,
|
||||||
last_name: raw_info["last_name"],
|
gender: raw_info["gender"],
|
||||||
|
name: raw_info["name"],
|
||||||
|
avatar_url: info["image"],
|
||||||
|
location: info["location"],
|
||||||
|
website: website,
|
||||||
|
about_me: info["description"]
|
||||||
|
},
|
||||||
email: email,
|
email: email,
|
||||||
gender: raw_info["gender"],
|
email_valid: true
|
||||||
name: raw_info["name"],
|
}
|
||||||
avatar_url: info["image"],
|
end
|
||||||
location: info["location"],
|
|
||||||
website: website,
|
|
||||||
about_me: info["description"]
|
|
||||||
},
|
|
||||||
email: email,
|
|
||||||
email_valid: true
|
|
||||||
}
|
|
||||||
|
|
||||||
end
|
def retrieve_avatar(user, data)
|
||||||
|
return unless user
|
||||||
|
return if user.user_avatar.try(:custom_upload_id).present?
|
||||||
|
|
||||||
def add_avatar_parameters(avatar_url)
|
if (avatar_url = data[:avatar_url]).present?
|
||||||
"#{avatar_url}?height=#{AVATAR_SIZE}&width=#{AVATAR_SIZE}"
|
url = "#{avatar_url}?height=#{AVATAR_SIZE}&width=#{AVATAR_SIZE}"
|
||||||
end
|
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
|
||||||
|
|
|
@ -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,27 +64,30 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def retrieve_avatar(user, data)
|
protected
|
||||||
return unless user
|
|
||||||
return if user.user_avatar.try(:custom_upload_id).present?
|
|
||||||
|
|
||||||
if (avatar_url = data[:twitter_image]).present?
|
def retrieve_avatar(user, data)
|
||||||
UserAvatar.import_url_for_user(avatar_url.sub("_normal", ""), user, override_gravatar: false)
|
return unless user
|
||||||
|
return if user.user_avatar.try(:custom_upload_id).present?
|
||||||
|
|
||||||
|
if (avatar_url = data[:twitter_image]).present?
|
||||||
|
url = avatar_url.sub("_normal", "")
|
||||||
|
Jobs.enqueue(:download_avatar_from_url, url: url, user_id: user.id, override_gravatar: false)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def retrieve_profile(user, data)
|
def retrieve_profile(user, data)
|
||||||
return unless user
|
return unless user
|
||||||
|
|
||||||
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?
|
||||||
profile.save
|
profile.save
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue