about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-01-20 01:00:14 +0100
committerEugen Rochko <eugen@zeonfederated.com>2017-01-20 01:00:14 +0100
commitf0de621e76b5a5ba3f7e67bd88c0183aac22b985 (patch)
tree06ecec4ca807eadc7ec743d64074fb9030e172f7 /app/services
parent8d0284f8d9bc22a6dd3bad1054fd8d78cbf32060 (diff)
Fix #463 - Fetch and display previews of URLs using OpenGraph tags
Diffstat (limited to 'app/services')
-rw-r--r--app/services/fetch_link_card_service.rb33
-rw-r--r--app/services/post_status_service.rb1
2 files changed, 34 insertions, 0 deletions
diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb
new file mode 100644
index 000000000..2779b79b5
--- /dev/null
+++ b/app/services/fetch_link_card_service.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+class FetchLinkCardService < BaseService
+  def call(status)
+    # Get first URL
+    url = URI.extract(status.text).reject { |uri| (uri =~ /\Ahttps?:\/\//).nil? }.first
+
+    return if url.nil?
+
+    response = http_client.get(url)
+
+    return if response.code != 200
+
+    page = Nokogiri::HTML(response.to_s)
+    card = PreviewCard.where(status: status).first_or_initialize(status: status, url: url)
+
+    card.title       = meta_property(page, 'og:title') || page.at_xpath('//title')&.content
+    card.description = meta_property(page, 'og:description') || meta_property(page, 'description')
+    card.image       = URI.parse(meta_property(page, 'og:image')) if meta_property(page, 'og:image')
+
+    card.save_with_optional_image!
+  end
+
+  private
+
+  def http_client
+    HTTP.timeout(:per_operation, write: 10, connect: 10, read: 10).follow
+  end
+
+  def meta_property(html, property)
+    html.at_xpath("//meta[@property=\"#{property}\"]")&.attribute('content')&.value || html.at_xpath("//meta[@name=\"#{property}\"]")&.attribute('content')&.value
+  end
+end
diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb
index af31c923f..8765ef5e3 100644
--- a/app/services/post_status_service.rb
+++ b/app/services/post_status_service.rb
@@ -22,6 +22,7 @@ class PostStatusService < BaseService
     process_mentions_service.call(status)
     process_hashtags_service.call(status)
 
+    LinkCrawlWorker.perform_async(status.id)
     DistributionWorker.perform_async(status.id)
     Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)