You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. class FetchLinkCardService < BaseService
  3. include HttpHelper
  4. URL_PATTERN = %r{https?://\S+}
  5. def call(status)
  6. # Get first http/https URL that isn't local
  7. url = parse_urls(status)
  8. return if url.nil?
  9. url = url.to_s
  10. card = PreviewCard.where(status: status).first_or_initialize(status: status, url: url)
  11. res = http_client.head(url)
  12. return if res.code != 200 || res.mime_type != 'text/html'
  13. attempt_opengraph(card, url) unless attempt_oembed(card, url)
  14. rescue HTTP::ConnectionError, OpenSSL::SSL::SSLError
  15. nil
  16. end
  17. private
  18. def parse_urls(status)
  19. if status.local?
  20. urls = status.text.match(URL_PATTERN).to_a.map { |uri| Addressable::URI.parse(uri).normalize }
  21. else
  22. html = Nokogiri::HTML(status.text)
  23. links = html.css('a')
  24. urls = links.map { |a| Addressable::URI.parse(a['href']).normalize unless skip_link?(a) }.compact
  25. end
  26. urls.reject { |uri| bad_url?(uri) }.first
  27. end
  28. def bad_url?(uri)
  29. # Avoid local instance URLs and invalid URLs
  30. uri.host.blank? || TagManager.instance.local_url?(uri.to_s) || !%w(http https).include?(uri.scheme)
  31. end
  32. def skip_link?(a)
  33. # Avoid links for hashtags and mentions (microformats)
  34. a['rel']&.include?('tag') || a['class']&.include?('u-url')
  35. end
  36. def attempt_oembed(card, url)
  37. response = OEmbed::Providers.get(url)
  38. card.type = response.type
  39. card.title = response.respond_to?(:title) ? response.title : ''
  40. card.author_name = response.respond_to?(:author_name) ? response.author_name : ''
  41. card.author_url = response.respond_to?(:author_url) ? response.author_url : ''
  42. card.provider_name = response.respond_to?(:provider_name) ? response.provider_name : ''
  43. card.provider_url = response.respond_to?(:provider_url) ? response.provider_url : ''
  44. card.width = 0
  45. card.height = 0
  46. case card.type
  47. when 'link'
  48. card.image = URI.parse(response.thumbnail_url) if response.respond_to?(:thumbnail_url)
  49. when 'photo'
  50. card.url = response.url
  51. card.width = response.width.presence || 0
  52. card.height = response.height.presence || 0
  53. when 'video'
  54. card.width = response.width.presence || 0
  55. card.height = response.height.presence || 0
  56. card.html = Formatter.instance.sanitize(response.html, Sanitize::Config::MASTODON_OEMBED)
  57. when 'rich'
  58. # Most providers rely on <script> tags, which is a no-no
  59. return false
  60. end
  61. card.save_with_optional_image!
  62. rescue OEmbed::NotFound
  63. false
  64. end
  65. def attempt_opengraph(card, url)
  66. response = http_client.get(url)
  67. return if response.code != 200 || response.mime_type != 'text/html'
  68. html = response.to_s
  69. detector = CharlockHolmes::EncodingDetector.new
  70. detector.strip_tags = true
  71. guess = detector.detect(html, response.charset)
  72. page = Nokogiri::HTML(html, nil, guess&.fetch(:encoding))
  73. card.type = :link
  74. card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content
  75. card.description = meta_property(page, 'og:description') || meta_property(page, 'description')
  76. card.image_remote_url = meta_property(page, 'og:image') if meta_property(page, 'og:image')
  77. return if card.title.blank?
  78. card.save_with_optional_image!
  79. end
  80. def meta_property(html, property)
  81. html.at_xpath("//meta[@property=\"#{property}\"]")&.attribute('content')&.value || html.at_xpath("//meta[@name=\"#{property}\"]")&.attribute('content')&.value
  82. end
  83. end