2013-08-23 02:20:43 -04:00
|
|
|
class Auth::TwitterAuthenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"twitter"
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token)
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
|
2016-10-12 18:30:39 -04:00
|
|
|
result.email = data["email"]
|
|
|
|
result.email_valid = result.email.present?
|
|
|
|
result.username = data["nickname"]
|
2016-09-06 12:18:13 -04:00
|
|
|
result.name = data["name"]
|
2013-08-23 02:20:43 -04:00
|
|
|
twitter_user_id = auth_token["uid"]
|
|
|
|
|
|
|
|
result.extra_data = {
|
|
|
|
twitter_user_id: twitter_user_id,
|
2016-10-17 09:43:40 -04:00
|
|
|
twitter_screen_name: result.username,
|
|
|
|
twitter_image: data["image"],
|
|
|
|
twitter_description: data["description"],
|
|
|
|
twitter_location: data["location"]
|
2013-08-23 02:20:43 -04:00
|
|
|
}
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
result.user = user_info.try(:user)
|
2016-10-17 09:43:40 -04:00
|
|
|
if !result.user && result.email_valid && result.user = User.find_by_email(result.email)
|
2016-09-06 12:18:13 -04:00
|
|
|
TwitterUserInfo.create(
|
|
|
|
user_id: result.user.id,
|
2016-10-12 18:30:39 -04:00
|
|
|
screen_name: result.username,
|
2016-09-06 12:18:13 -04:00
|
|
|
twitter_user_id: twitter_user_id
|
|
|
|
)
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2016-10-17 09:43:40 -04:00
|
|
|
retrieve_avatar(result.user, result.extra_data)
|
|
|
|
retrieve_profile(result.user, result.extra_data)
|
2016-10-12 18:30:39 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
2016-10-17 09:43:40 -04:00
|
|
|
extra_data = auth[:extra_data]
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
TwitterUserInfo.create(
|
|
|
|
user_id: user.id,
|
2016-10-17 09:43:40 -04:00
|
|
|
screen_name: extra_data[:twitter_screen_name],
|
|
|
|
twitter_user_id: extra_data[:twitter_user_id]
|
2013-08-23 02:20:43 -04:00
|
|
|
)
|
2016-10-17 09:43:40 -04:00
|
|
|
|
|
|
|
retrieve_avatar(user, extra_data)
|
|
|
|
retrieve_profile(user, extra_data)
|
2016-10-24 11:15:13 -04:00
|
|
|
|
|
|
|
true
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :twitter,
|
2017-07-27 21:20:09 -04:00
|
|
|
setup: lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
2013-08-25 21:04:16 -04:00
|
|
|
strategy.options[:consumer_key] = SiteSetting.twitter_consumer_key
|
|
|
|
strategy.options[:consumer_secret] = SiteSetting.twitter_consumer_secret
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def retrieve_avatar(user, data)
|
|
|
|
return unless user
|
|
|
|
return if user.user_avatar.try(:custom_upload_id).present?
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
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
|
2016-10-17 09:43:40 -04:00
|
|
|
end
|
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
def retrieve_profile(user, data)
|
|
|
|
return unless user
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
bio = data[:twitter_description]
|
|
|
|
location = data[:twitter_location]
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
if 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
|
2016-10-17 09:43:40 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|