diff --git a/app/liquid_tags/unified_embed/tag.rb b/app/liquid_tags/unified_embed/tag.rb index 81e486922..d12c64e2c 100644 --- a/app/liquid_tags/unified_embed/tag.rb +++ b/app/liquid_tags/unified_embed/tag.rb @@ -27,6 +27,10 @@ module UnifiedEmbed # Extract just the URL from the input, without any params, for validation actual_link = extract_only_url(stripped_input) + # When Listings are disabled, it makes little sense to perform a validate_link + # network call. + handle_listings_disabled!(actual_link) + # Before matching against the embed registry, we check if the link # is valid (e.g. no typos). # If the link is invalid, we raise an error encouraging the user to @@ -66,6 +70,12 @@ module UnifiedEmbed raise StandardError, I18n.t("liquid_tags.unified_embed.tag.invalid_url") end + def self.handle_listings_disabled!(link) + return unless link.start_with?("#{URL.url}/listings/") && !Listing.feature_enabled? + + raise StandardError, I18n.t("liquid_tags.unified_embed.tag.listings_disabled") + end + def self.extract_only_url(input) url_portion = input.split.length > 1 ? input.split[0] : input diff --git a/config/initializers/reserved_words.rb b/config/initializers/reserved_words.rb index 1efd20b34..edcf3b0de 100644 --- a/config/initializers/reserved_words.rb +++ b/config/initializers/reserved_words.rb @@ -116,6 +116,8 @@ class ReservedWords links linux listen + listing + listings live live_articles loop diff --git a/config/locales/liquid_tags/en.yml b/config/locales/liquid_tags/en.yml index 399a5ab2a..33e85455f 100644 --- a/config/locales/liquid_tags/en.yml +++ b/config/locales/liquid_tags/en.yml @@ -99,7 +99,7 @@ en: tag: invalid_url: URL provided may have a typo or error; please check and try again not_found: URL provided was not found; please check and try again - unsupported_url: Embeds for this URL are not supported + listings_disabled: Listings are disabled on this Forem; cannot embed a listing URL user_subscription_tag: submit: Submit submitting: Submitting... diff --git a/config/locales/liquid_tags/fr.yml b/config/locales/liquid_tags/fr.yml index a7e7f8f45..503afca84 100644 --- a/config/locales/liquid_tags/fr.yml +++ b/config/locales/liquid_tags/fr.yml @@ -99,7 +99,7 @@ fr: tag: invalid_url: URL provided may have a typo or error; please check and try again not_found: URL provided was not found; please check and try again - unsupported_url: Embeds for this URL are not supported + listings_disabled: Listings are disabled on this Forem; cannot embed a listing URL user_subscription_tag: submit: Submit submitting: Submitting... diff --git a/spec/liquid_tags/unified_embed/tag_spec.rb b/spec/liquid_tags/unified_embed/tag_spec.rb index 2cf12d87d..0efdd65b5 100644 --- a/spec/liquid_tags/unified_embed/tag_spec.rb +++ b/spec/liquid_tags/unified_embed/tag_spec.rb @@ -1,6 +1,8 @@ require "rails_helper" RSpec.describe UnifiedEmbed::Tag, type: :liquid_tag do + let(:listing) { create(:listing) } + it "delegates parsing to the link-matching class" do link = "https://gist.github.com/jeremyf/662585f5c4d22184a6ae133a71bf891a" @@ -72,4 +74,13 @@ RSpec.describe UnifiedEmbed::Tag, type: :liquid_tag do expect(OpenGraphTag).to have_received(:new) end + + it "raises an error when Listings are disabled and a listing URL is embedded" do + allow(FeatureFlag).to receive(:accessible?).with(:listing_feature_enabled).and_return(false) + listing_url = "#{URL.url}/listings/#{listing.slug}" + + expect do + Liquid::Template.parse("{% embed #{listing_url} %}") + end.to raise_error(StandardError, "Listings are disabled on this Forem; cannot embed a listing URL") + end end