]> cat aescling's git repositories - mastodon.git/commitdiff
Fix some failure cases on FetchLinkCardService (#5347)
authorunarist <m.unarist@gmail.com>
Thu, 12 Oct 2017 10:01:32 +0000 (19:01 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Thu, 12 Oct 2017 10:01:32 +0000 (12:01 +0200)
* If OEmbed response doesn't have a required property `type`, ignore it.
  e.g. `NoMethodError: undefined method 'type' for ...`
* If we failed to detect encoding, fallback to default behavior of Nokogiri.
  e.g. `KeyError: key not found: :encoding`

app/services/fetch_link_card_service.rb

index cf3d7868303d5c5b77d03c13868681c0702dffdc..7029c4d757bf6243ede863b3d1dbd4af5e9f96ae 100644 (file)
@@ -72,6 +72,8 @@ class FetchLinkCardService < BaseService
   def attempt_oembed
     response = OEmbed::Providers.get(@url)
 
+    return false unless response.respond_to?(:type)
+
     @card.type          = response.type
     @card.title         = response.respond_to?(:title)         ? response.title         : ''
     @card.author_name   = response.respond_to?(:author_name)   ? response.author_name   : ''
@@ -113,7 +115,7 @@ class FetchLinkCardService < BaseService
     detector.strip_tags = true
 
     guess = detector.detect(html, response.charset)
-    page  = Nokogiri::HTML(html, nil, guess&.fetch(:encoding))
+    page  = Nokogiri::HTML(html, nil, guess&.fetch(:encoding, nil))
 
     if meta_property(page, 'twitter:player')
       @card.type   = :video