Fix: 投稿ではないリンクを参照したときにリンクプレビューが生成されない問題 (#482)

This commit is contained in:
KMY(雪あすか) 2024-01-18 13:09:20 +09:00 committed by KMY
parent ef3eb48932
commit 65cc1273aa
2 changed files with 48 additions and 5 deletions

View file

@ -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|

View file

@ -7,6 +7,7 @@ RSpec.describe FetchLinkCardService, type: :service do
let(:html) { '<!doctype html><title>Hello world</title>' }
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 <a href="http://example.com/html" target="_blank" rel="noopener noreferrer" title="http://example.com/html">Hello</a>&nbsp;
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
<a href="http://example.com/html_sub" target="_blank" rel="noopener noreferrer" title="http://example.com/html_sub">Hello</a>&nbsp;
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'