Fix #463 - Fetch and display previews of URLs using OpenGraph tags
This commit is contained in:
parent
8d0284f8d9
commit
f0de621e76
26 changed files with 302 additions and 7 deletions
33
app/services/fetch_link_card_service.rb
Normal file
33
app/services/fetch_link_card_service.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue