DEV: Clean up twitter onebox code (#18012)

This commit is contained in:
Jarek Radosz 2022-08-21 19:26:24 +02:00 committed by GitHub
parent 4e3c688d65
commit 49fa2e93c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 21 deletions

View File

@ -461,7 +461,7 @@ module Oneboxer
preview_result = Onebox.preview(uri.to_s, onebox_options) preview_result = Onebox.preview(uri.to_s, onebox_options)
result = { result = {
onebox: WordWatcher.censor(preview_result.to_s), onebox: WordWatcher.censor(preview_result.to_s),
preview: WordWatcher.censor(preview_result&.placeholder_html.to_s) preview: WordWatcher.censor(preview_result.placeholder_html.to_s)
} }
# NOTE: Call preview_result.errors after calling placeholder_html # NOTE: Call preview_result.errors after calling placeholder_html

View File

@ -2,10 +2,11 @@
# lightweight Twitter api calls # lightweight Twitter api calls
class TwitterApi class TwitterApi
class << self class << self
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
BASE_URL = 'https://api.twitter.com'
def prettify_tweet(tweet) def prettify_tweet(tweet)
text = tweet["full_text"].dup text = tweet["full_text"].dup
if (entities = tweet["entities"]) && (urls = entities["urls"]) if (entities = tweet["entities"]) && (urls = entities["urls"])
@ -74,20 +75,11 @@ class TwitterApi
number_to_human(count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' }) number_to_human(count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' })
end end
def user_timeline(screen_name)
JSON.parse(twitter_get(user_timeline_uri_for screen_name))
end
def tweet_for(id) def tweet_for(id)
JSON.parse(twitter_get(tweet_uri_for id)) JSON.parse(twitter_get(tweet_uri_for(id)))
end end
alias_method :status, :tweet_for alias_method :status, :tweet_for
def raw_tweet_for(id)
twitter_get(tweet_uri_for id)
end
def twitter_credentials_missing? def twitter_credentials_missing?
consumer_key.blank? || consumer_secret.blank? consumer_key.blank? || consumer_secret.blank?
end end
@ -110,18 +102,10 @@ class TwitterApi
end.strip end.strip
end end
def user_timeline_uri_for(screen_name)
URI.parse "#{BASE_URL}/1.1/statuses/user_timeline.json?screen_name=#{screen_name}&count=50&include_rts=false&exclude_replies=true"
end
def tweet_uri_for(id) def tweet_uri_for(id)
URI.parse "#{BASE_URL}/1.1/statuses/show.json?id=#{id}&tweet_mode=extended" URI.parse "#{BASE_URL}/1.1/statuses/show.json?id=#{id}&tweet_mode=extended"
end end
unless defined? BASE_URL
BASE_URL = 'https://api.twitter.com'
end
def twitter_get(uri) def twitter_get(uri)
request = Net::HTTP::Get.new(uri) request = Net::HTTP::Get.new(uri)
request.add_field 'Authorization', "Bearer #{bearer_token}" request.add_field 'Authorization', "Bearer #{bearer_token}"
@ -172,6 +156,5 @@ class TwitterApi
def consumer_secret def consumer_secret
SiteSetting.twitter_consumer_secret SiteSetting.twitter_consumer_secret
end end
end end
end end