2021-05-26 17:41:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Onebox
|
|
|
|
module Engine
|
|
|
|
class TwitterStatusOnebox
|
|
|
|
include Engine
|
|
|
|
include LayoutSupport
|
|
|
|
include HTML
|
|
|
|
|
|
|
|
matches_regexp(
|
|
|
|
%r{^https?://(mobile\.|www\.)?twitter\.com/.+?/status(es)?/\d+(/(video|photo)/\d?+)?+(/?\?.*)?/?$},
|
|
|
|
)
|
|
|
|
always_https
|
|
|
|
|
2022-08-17 23:32:48 +08:00
|
|
|
def self.===(other)
|
2022-08-31 08:42:55 +08:00
|
|
|
client = Onebox.options.twitter_client
|
|
|
|
client && !client.twitter_credentials_missing? && super
|
2022-08-17 23:32:48 +08:00
|
|
|
end
|
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
def http_params
|
|
|
|
{ "User-Agent" => "DiscourseBot/1.0" }
|
|
|
|
end
|
|
|
|
|
2022-08-17 23:32:48 +08:00
|
|
|
def to_html
|
|
|
|
raw.present? ? super : ""
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
2022-08-17 23:32:48 +08:00
|
|
|
private
|
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
def match
|
|
|
|
@match ||= @url.match(%r{twitter\.com/.+?/status(es)?/(?<id>\d+)})
|
|
|
|
end
|
|
|
|
|
|
|
|
def client
|
|
|
|
Onebox.options.twitter_client
|
|
|
|
end
|
|
|
|
|
|
|
|
def twitter_api_credentials_present?
|
|
|
|
client && !client.twitter_credentials_missing?
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
2022-08-17 23:32:48 +08:00
|
|
|
@raw ||= client.status(match[:id]).to_hash if twitter_api_credentials_present?
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def access(*keys)
|
|
|
|
keys.reduce(raw) do |memo, key|
|
|
|
|
next unless memo
|
|
|
|
memo[key] || memo[key.to_s]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweet
|
2022-08-17 23:32:48 +08:00
|
|
|
client.prettify_tweet(raw)&.strip
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def timestamp
|
2022-08-17 23:32:48 +08:00
|
|
|
date = DateTime.strptime(access(:created_at), "%a %b %d %H:%M:%S %z %Y")
|
|
|
|
user_offset = access(:user, :utc_offset).to_i
|
|
|
|
offset = (user_offset >= 0 ? "+" : "-") + Time.at(user_offset.abs).gmtime.strftime("%H%M")
|
|
|
|
date.new_offset(offset).strftime("%-l:%M %p - %-d %b %Y")
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
2022-08-17 23:32:48 +08:00
|
|
|
access(:user, :name)
|
2021-05-27 13:35:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def screen_name
|
2022-08-17 23:32:48 +08:00
|
|
|
access(:user, :screen_name)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def avatar
|
2022-08-17 23:32:48 +08:00
|
|
|
access(:user, :profile_image_url_https).sub("normal", "400x400")
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def likes
|
2022-08-17 23:32:48 +08:00
|
|
|
prettify_number(access(:favorite_count).to_i)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def retweets
|
2022-08-17 23:32:48 +08:00
|
|
|
prettify_number(access(:retweet_count).to_i)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoted_full_name
|
2022-08-17 23:32:48 +08:00
|
|
|
access(:quoted_status, :user, :name)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoted_screen_name
|
2022-08-17 23:32:48 +08:00
|
|
|
access(:quoted_status, :user, :screen_name)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoted_tweet
|
2022-08-17 23:32:48 +08:00
|
|
|
access(:quoted_status, :full_text)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def quoted_link
|
2022-08-17 23:32:48 +08:00
|
|
|
"https://twitter.com/#{quoted_screen_name}/status/#{access(:quoted_status, :id)}"
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def prettify_number(count)
|
|
|
|
count > 0 ? client.prettify_number(count) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
@data ||= {
|
|
|
|
link: link,
|
|
|
|
tweet: tweet,
|
|
|
|
timestamp: timestamp,
|
|
|
|
title: title,
|
2021-05-27 13:35:32 +08:00
|
|
|
screen_name: screen_name,
|
2021-05-26 17:41:35 +08:00
|
|
|
avatar: avatar,
|
|
|
|
likes: likes,
|
|
|
|
retweets: retweets,
|
|
|
|
quoted_tweet: quoted_tweet,
|
|
|
|
quoted_full_name: quoted_full_name,
|
|
|
|
quoted_screen_name: quoted_screen_name,
|
|
|
|
quoted_link: quoted_link,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|