Implement Unified Embed for Tweet URLs (#16218)

* building

* complete implementation and add specs
This commit is contained in:
Arit Amana 2022-01-20 08:05:56 -05:00 committed by GitHub
parent 43c188eafb
commit ea6bd0f366
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View file

@ -1,7 +1,9 @@
class TweetTag < LiquidTagBase
include ActionView::Helpers::AssetTagHelper
PARTIAL = "liquids/tweet".freeze
ID_REGEXP = /\A\d{10,20}\z/ # id must be all numbers between 10 and 20 chars
REGISTRY_REGEXP = %r{https://twitter.com/\w{1,15}/status/(?<id>\d{10,20})}
VALID_ID_REGEXP = /\A(?<id>\d{10,20})\Z/
REGEXP_OPTIONS = [REGISTRY_REGEXP, VALID_ID_REGEXP].freeze
SCRIPT = <<~JAVASCRIPT.freeze
var videoPreviews = document.getElementsByClassName("ltag__twitter-tweet__media__video-wrapper");
@ -27,7 +29,7 @@ class TweetTag < LiquidTagBase
def initialize(_tag_name, id, _parse_context)
super
@id = parse_id(id)
@id = parse_id_or_url(strip_tags(id))
@tweet = Tweet.find_or_fetch(@id)
@twitter_logo = ActionController::Base.helpers.asset_path("twitter.svg")
end
@ -49,17 +51,14 @@ class TweetTag < LiquidTagBase
private
def parse_id(input)
input_no_space = input.delete(" ")
raise StandardError, I18n.t("liquid_tags.tweet_tag.invalid_twitter_id") unless valid_id?(input_no_space)
def parse_id_or_url(input)
match = pattern_match_for(input, REGEXP_OPTIONS)
raise StandardError, "Invalid Tweet ID or URL" unless match
input_no_space
end
def valid_id?(id)
ID_REGEXP.match?(id)
match[:id]
end
end
Liquid::Template.register_tag("tweet", TweetTag)
Liquid::Template.register_tag("twitter", TweetTag)
UnifiedEmbed.register(TweetTag, regexp: TweetTag::REGISTRY_REGEXP)

View file

@ -155,6 +155,11 @@ RSpec.describe UnifiedEmbed::Registry do
end
end
it "returns TweetTag for a tweet url" do
expect(described_class.find_liquid_tag_for(link: "https://twitter.com/aritdeveloper/status/1483614684884484099"))
.to eq(TweetTag)
end
it "returns TwitchTag for a valid twitch url" do
valid_twitch_url_formats.each do |url|
expect(described_class.find_liquid_tag_for(link: url))