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.

106 lines
3.5 KiB

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