diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb
index fa445faf2c..6c84f6d3b4 100644
--- a/app/services/fetch_link_card_service.rb
+++ b/app/services/fetch_link_card_service.rb
@@ -88,6 +88,10 @@ class FetchLinkCardService < BaseService
end
def referenced_urls
+ referenced_urls_raw.filter { |uri| ActivityPub::TagManager.instance.uri_to_resource(uri, Status, url: true).present? }
+ end
+
+ def referenced_urls_raw
unless @status.local?
document = Nokogiri::HTML(@status.text)
document.search('a[href^="http://"]', 'a[href^="https://"]').each do |link|
diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb
index 3273351b9a..654da1e0d4 100644
--- a/spec/services/fetch_link_card_service_spec.rb
+++ b/spec/services/fetch_link_card_service_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe FetchLinkCardService, type: :service do
let(:html) { '
Hello world' }
let(:oembed_cache) { nil }
+ let(:custom_before) { false }
before do
stub_request(:get, 'http://example.com/html').to_return(headers: { 'Content-Type' => 'text/html' }, body: html)
@@ -30,7 +31,7 @@ RSpec.describe FetchLinkCardService, type: :service do
Rails.cache.write('oembed_endpoint:example.com', oembed_cache) if oembed_cache
- subject.call(status)
+ subject.call(status) unless custom_before
end
context 'with a local status' do
@@ -236,16 +237,46 @@ RSpec.describe FetchLinkCardService, type: :service do
end
end
- context 'with URL of reference' do
- let(:status) { Fabricate(:status, text: 'RT http://example.com/html') }
+ context 'with URI of reference and normal page' do
+ let(:status) { Fabricate(:status, text: 'RT http://example.com/text http://example.com/html') }
+ let(:custom_before) { true }
+
+ before { Fabricate(:status, uri: 'http://example.com/text') }
it 'creates preview card' do
+ subject.call(status)
+ expect(status.preview_card).to_not be_nil
+ expect(status.preview_card.url).to eq 'http://example.com/html'
+ expect(status.preview_card.title).to eq 'Hello world'
+ end
+ end
+
+ context 'with URI of reference' do
+ let(:status) { Fabricate(:status, text: 'RT http://example.com/text') }
+ let(:custom_before) { true }
+
+ before { Fabricate(:status, uri: 'http://example.com/text') }
+
+ it 'does not create preview card' do
+ subject.call(status)
expect(status.preview_card).to be_nil
end
end
- context 'with URL of reference and normal page' do
- let(:status) { Fabricate(:status, text: 'RT http://example.com/text http://example.com/html') }
+ context 'with URL of reference' do
+ let(:status) { Fabricate(:status, text: 'RT http://example.com/text') }
+ let(:custom_before) { true }
+
+ before { Fabricate(:status, uri: 'http://example.com/text/activity', url: 'http://example.com/text') }
+
+ it 'does not create preview card' do
+ subject.call(status)
+ expect(status.preview_card).to be_nil
+ end
+ end
+
+ context 'with reference normal URL' do
+ let(:status) { Fabricate(:status, text: 'RT http://example.com/html') }
it 'creates preview card' do
expect(status.preview_card).to_not be_nil
@@ -281,8 +312,12 @@ RSpec.describe FetchLinkCardService, type: :service do
RT Hello
TEXT
end
+ let(:custom_before) { true }
+
+ before { Fabricate(:status, uri: 'http://example.com/html') }
it 'creates preview card' do
+ subject.call(status)
expect(status.preview_card).to be_nil
end
end
@@ -294,8 +329,12 @@ RSpec.describe FetchLinkCardService, type: :service do
Hello
TEXT
end
+ let(:custom_before) { true }
+
+ before { Fabricate(:status, uri: 'http://example.com/html') }
it 'creates preview card' do
+ subject.call(status)
expect(status.preview_card).to_not be_nil
expect(status.preview_card.url).to eq 'http://example.com/html_sub'
expect(status.preview_card.title).to eq 'Hello world'