From 22b2f5285cef32a5c1e8b9915ecace3300820a6e Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Apr 2016 12:28:18 +1000 Subject: [PATCH] FIX: extract links in post processor when oneboxes are not cached or are refreshed they can introduce new links, these links must be extracted otherwise you can not follow them --- lib/cooked_post_processor.rb | 7 +++++++ spec/components/cooked_post_processor_spec.rb | 9 +++++++++ spec/components/post_creator_spec.rb | 3 ++- spec/controllers/posts_controller_spec.rb | 8 ++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index d0f939ffa91..665caeb813c 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -29,9 +29,16 @@ class CookedPostProcessor optimize_urls pull_hotlinked_images(bypass_bump) grant_badges + extract_links end end + # onebox may have added some links, so extract them now + def extract_links + TopicLink.extract_from(@post) + QuotedPost.extract_from(@post) + end + def has_emoji? (@doc.css("img.emoji") - @doc.css(".quote img")).size > 0 end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index b83a766589c..1e1c4963b1f 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -478,6 +478,15 @@ describe CookedPostProcessor do end + context "extracts links" do + let(:post) { Fabricate(:post, raw: "sam has a blog at https://samsaffron.com") } + it "always re-extracts links on post process" do + TopicLink.destroy_all + CookedPostProcessor.new(post).post_process + expect(TopicLink.count).to eq(1) + end + end + context "grant badges" do context "emoji inside a quote" do let(:post) { Fabricate(:post, raw: "time to eat some sweet [quote]:candy:[/quote] mmmm") } diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 6b5cf4602b0..8675fdf77e6 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -150,8 +150,9 @@ describe PostCreator do end it 'extracts links from the post' do - TopicLink.expects(:extract_from).with(instance_of(Post)) + create_post(raw: "this is a link to the best site at https://google.com") creator.create + expect(TopicLink.count).to eq(1) end it 'queues up post processing job when saved' do diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index da23e399cf1..0f7d6c9f020 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -387,8 +387,12 @@ describe PostsController do end it "extracts links from the new body" do - TopicLink.expects(:extract_from).with(post) - xhr :put, :update, update_params + param = update_params + param[:post][:raw] = 'I just visited this https://google.com so many cool links' + + xhr :put, :update, param + expect(response).to be_success + expect(TopicLink.count).to eq(1) end it "doesn't allow updating of deleted posts" do