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.

126 lines
3.8 KiB

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