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.

100 lines
3.3 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. end
  15. private
  16. def parse_urls(status)
  17. if status.local?
  18. urls = status.text.match(URL_PATTERN).to_a.map { |uri| Addressable::URI.parse(uri).normalize }
  19. else
  20. html = Nokogiri::HTML(status.text)
  21. links = html.css('a')
  22. urls = links.map { |a| Addressable::URI.parse(a['href']).normalize unless skip_link?(a) }.compact
  23. end
  24. urls.reject { |uri| bad_url?(uri) }.first
  25. end
  26. def bad_url?(uri)
  27. # Avoid local instance URLs and invalid URLs
  28. uri.host.blank? || TagManager.instance.local_url?(uri.to_s) || !%w(http https).include?(uri.scheme)
  29. end
  30. def skip_link?(a)
  31. # Avoid links for hashtags and mentions (microformats)
  32. a['rel']&.include?('tag') || a['class']&.include?('u-url')
  33. end
  34. def attempt_oembed(card, url)
  35. response = OEmbed::Providers.get(url)
  36. card.type = response.type
  37. card.title = response.respond_to?(:title) ? response.title : ''
  38. card.author_name = response.respond_to?(:author_name) ? response.author_name : ''
  39. card.author_url = response.respond_to?(:author_url) ? response.author_url : ''
  40. card.provider_name = response.respond_to?(:provider_name) ? response.provider_name : ''
  41. card.provider_url = response.respond_to?(:provider_url) ? response.provider_url : ''
  42. card.width = 0
  43. card.height = 0
  44. case card.type
  45. when 'link'
  46. card.image = URI.parse(response.thumbnail_url) if response.respond_to?(:thumbnail_url)
  47. when 'photo'
  48. card.url = response.url
  49. card.width = response.width.presence || 0
  50. card.height = response.height.presence || 0
  51. when 'video'
  52. card.width = response.width.presence || 0
  53. card.height = response.height.presence || 0
  54. card.html = Formatter.instance.sanitize(response.html, Sanitize::Config::MASTODON_OEMBED)
  55. when 'rich'
  56. # Most providers rely on <script> tags, which is a no-no
  57. return false
  58. end
  59. card.save_with_optional_image!
  60. rescue OEmbed::NotFound
  61. false
  62. end
  63. def attempt_opengraph(card, url)
  64. response = http_client.get(url)
  65. return if response.code != 200 || response.mime_type != 'text/html'
  66. page = Nokogiri::HTML(response.to_s)
  67. card.type = :link
  68. card.title = meta_property(page, 'og:title') || page.at_xpath('//title')&.content
  69. card.description = meta_property(page, 'og:description') || meta_property(page, 'description')
  70. card.image_remote_url = meta_property(page, 'og:image') if meta_property(page, 'og:image')
  71. return if card.title.blank?
  72. card.save_with_optional_image!
  73. end
  74. def meta_property(html, property)
  75. html.at_xpath("//meta[@property=\"#{property}\"]")&.attribute('content')&.value || html.at_xpath("//meta[@name=\"#{property}\"]")&.attribute('content')&.value
  76. end
  77. end