Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.

Commit 665f94f

Browse files
ClearlyClairekyori19
authored andcommitted
Handle relative URLs when fetching OEmbed/OpenGraph cards (mastodon#8669)
1 parent 04b1f05 commit 665f94f

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

app/services/fetch_link_card_service.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,36 @@ def skip_link?(a)
8787
end
8888

8989
def attempt_oembed
90-
embed = FetchOEmbedService.new.call(@url, html: @html)
90+
service = FetchOEmbedService.new
91+
embed = service.call(@url, html: @html)
92+
url = Addressable::URI.parse(service.endpoint_url)
9193

9294
return false if embed.nil?
9395

9496
@card.type = embed[:type]
9597
@card.title = embed[:title] || ''
9698
@card.author_name = embed[:author_name] || ''
97-
@card.author_url = embed[:author_url] || ''
99+
@card.author_url = embed[:author_url].present? ? (url + embed[:author_url]).to_s : ''
98100
@card.provider_name = embed[:provider_name] || ''
99-
@card.provider_url = embed[:provider_url] || ''
101+
@card.provider_url = embed[:provider_url].present? ? (url + embed[:provider_url]).to_s : ''
100102
@card.width = 0
101103
@card.height = 0
102104

103105
case @card.type
104106
when 'link'
105-
@card.image_remote_url = embed[:thumbnail_url] if embed[:thumbnail_url].present?
107+
@card.image_remote_url = (url + embed[:thumbnail_url]).to_s if embed[:thumbnail_url].present?
106108
when 'photo'
107109
return false if embed[:url].blank?
108110

109-
@card.embed_url = embed[:url]
110-
@card.image_remote_url = embed[:url]
111+
@card.embed_url = (url + embed[:url]).to_s
112+
@card.image_remote_url = (url + embed[:url]).to_s
111113
@card.width = embed[:width].presence || 0
112114
@card.height = embed[:height].presence || 0
113115
when 'video'
114116
@card.width = embed[:width].presence || 0
115117
@card.height = embed[:height].presence || 0
116118
@card.html = Formatter.instance.sanitize(embed[:html], Sanitize::Config::MASTODON_OEMBED)
117-
@card.image_remote_url = embed[:thumbnail_url] if embed[:thumbnail_url].present?
119+
@card.image_remote_url = (url + embed[:thumbnail_url]).to_s if embed[:thumbnail_url].present?
118120
when 'rich'
119121
# Most providers rely on <script> tags, which is a no-no
120122
return false
@@ -146,7 +148,7 @@ def attempt_opengraph
146148

147149
@card.title = meta_property(page, 'og:title').presence || page.at_xpath('//title')&.content || ''
148150
@card.description = meta_property(page, 'og:description').presence || meta_property(page, 'description') || ''
149-
@card.image_remote_url = meta_property(page, 'og:image') if meta_property(page, 'og:image')
151+
@card.image_remote_url = (Addressable::URI.parse(@url) + meta_property(page, 'og:image')).to_s if meta_property(page, 'og:image')
150152

151153
return if @card.title.blank? && @card.html.blank?
152154

app/services/fetch_oembed_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def discover_endpoint!
3131

3232
return if @endpoint_url.blank?
3333

34-
@endpoint_url = Addressable::URI.parse(@endpoint_url).to_s
34+
@endpoint_url = (Addressable::URI.parse(@url) + @endpoint_url).to_s
3535
rescue Addressable::URI::InvalidURIError
3636
@endpoint_url = nil
3737
end

0 commit comments

Comments
 (0)