FIX: pull twitter's avatar & profile when signing up
This commit is contained in:
parent
3949c24f80
commit
0862ad406d
|
@ -68,9 +68,7 @@ class UserAvatar < ActiveRecord::Base
|
||||||
|
|
||||||
upload = Upload.create_for(user.id, tempfile, "external-avatar." + ext, File.size(tempfile.path), origin: avatar_url, image_type: "avatar")
|
upload = Upload.create_for(user.id, tempfile, "external-avatar." + ext, File.size(tempfile.path), origin: avatar_url, image_type: "avatar")
|
||||||
|
|
||||||
unless user.user_avatar
|
user.create_user_avatar unless user.user_avatar
|
||||||
user.create_user_avatar
|
|
||||||
end
|
|
||||||
|
|
||||||
if !user.user_avatar.contains_upload?(upload.id)
|
if !user.user_avatar.contains_upload?(upload.id)
|
||||||
user.user_avatar.update_columns(custom_upload_id: upload.id)
|
user.user_avatar.update_columns(custom_upload_id: upload.id)
|
||||||
|
@ -84,7 +82,6 @@ class UserAvatar < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
rescue => e
|
rescue => e
|
||||||
# skip saving, we are not connected to the net
|
# skip saving, we are not connected to the net
|
||||||
Rails.logger.warn "#{e}: Failed to download external avatar: #{avatar_url}, user id #{ user.id }"
|
Rails.logger.warn "#{e}: Failed to download external avatar: #{avatar_url}, user id #{ user.id }"
|
||||||
|
|
|
@ -17,13 +17,16 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
||||||
|
|
||||||
result.extra_data = {
|
result.extra_data = {
|
||||||
twitter_user_id: twitter_user_id,
|
twitter_user_id: twitter_user_id,
|
||||||
twitter_screen_name: result.username
|
twitter_screen_name: result.username,
|
||||||
|
twitter_image: data["image"],
|
||||||
|
twitter_description: data["description"],
|
||||||
|
twitter_location: data["location"]
|
||||||
}
|
}
|
||||||
|
|
||||||
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
|
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
|
||||||
|
|
||||||
result.user = user_info.try(:user)
|
result.user = user_info.try(:user)
|
||||||
if !result.user && result.email.present? && result.user = User.find_by_email(result.email)
|
if !result.user && result.email_valid && result.user = User.find_by_email(result.email)
|
||||||
TwitterUserInfo.create(
|
TwitterUserInfo.create(
|
||||||
user_id: result.user.id,
|
user_id: result.user.id,
|
||||||
screen_name: result.username,
|
screen_name: result.username,
|
||||||
|
@ -31,33 +34,23 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
||||||
)
|
)
|
||||||
end
|
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 = data["image"]).present?
|
|
||||||
UserAvatar.import_url_for_user(avatar_url.sub("_normal", ""), user, override_gravatar: false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
bio = data["description"]
|
|
||||||
location = data["location"]
|
|
||||||
|
|
||||||
if user && (bio || location)
|
|
||||||
profile = user.user_profile
|
|
||||||
profile.bio_raw = bio unless profile.bio_raw.present?
|
|
||||||
profile.location = location unless profile.location.present?
|
|
||||||
profile.save
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_create_account(user, auth)
|
def after_create_account(user, auth)
|
||||||
data = auth[:extra_data]
|
extra_data = auth[:extra_data]
|
||||||
|
|
||||||
TwitterUserInfo.create(
|
TwitterUserInfo.create(
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
screen_name: data[:twitter_screen_name],
|
screen_name: extra_data[:twitter_screen_name],
|
||||||
twitter_user_id: data[:twitter_user_id]
|
twitter_user_id: extra_data[:twitter_user_id]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
retrieve_avatar(user, extra_data)
|
||||||
|
retrieve_profile(user, extra_data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_middleware(omniauth)
|
def register_middleware(omniauth)
|
||||||
|
@ -69,4 +62,27 @@ class Auth::TwitterAuthenticator < Auth::Authenticator
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def retrieve_avatar(user, data)
|
||||||
|
return unless user
|
||||||
|
return if user.user_avatar.try(:custom_upload_id).present?
|
||||||
|
|
||||||
|
if (avatar_url = data[:twitter_image]).present?
|
||||||
|
UserAvatar.import_url_for_user(avatar_url.sub("_normal", ""), user, override_gravatar: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def retrieve_profile(user, data)
|
||||||
|
return unless user
|
||||||
|
|
||||||
|
bio = data[:twitter_description]
|
||||||
|
location = data[:twitter_location]
|
||||||
|
|
||||||
|
if user && (bio || location)
|
||||||
|
profile = user.user_profile
|
||||||
|
profile.bio_raw = bio unless profile.bio_raw.present?
|
||||||
|
profile.location = location unless profile.location.present?
|
||||||
|
profile.save
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue