From e38f17524b121be0a9482c0c59816afa7263a65f Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 8 Jun 2016 16:08:41 -0400 Subject: [PATCH] FIX: Reflected links weren't being cleaned up properly --- app/models/topic_link.rb | 34 ++++++++++++++++------------ spec/components/post_revisor_spec.rb | 2 +- spec/models/topic_link_spec.rb | 18 +++------------ 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index 0f19ba7d3fa..44d6a41ac13 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -106,7 +106,7 @@ class TopicLink < ActiveRecord::Base TopicLink.transaction do added_urls = [] - reflected_urls = [] + reflected_ids = [] PrettyText .extract_links(post.cooked) @@ -167,8 +167,7 @@ class TopicLink < ActiveRecord::Base internal: internal, link_topic_id: topic_id, link_post_id: reflected_post.try(:id), - quote: link.is_quote - ) + quote: link.is_quote) # Create the reflection if we can if topic_id.present? @@ -179,17 +178,17 @@ class TopicLink < ActiveRecord::Base prefix = Discourse.base_url_no_prefix reflected_url = "#{prefix}#{post.topic.relative_url(post.post_number)}" + tl = TopicLink.create(user_id: post.user_id, + topic_id: topic_id, + post_id: reflected_post.try(:id), + url: reflected_url, + domain: Discourse.current_hostname, + reflection: true, + internal: true, + link_topic_id: post.topic_id, + link_post_id: post.id) - reflected_urls << reflected_url - TopicLink.create(user_id: post.user_id, - topic_id: topic_id, - post_id: reflected_post.try(:id), - url: reflected_url, - domain: Discourse.current_hostname, - reflection: true, - internal: true, - link_topic_id: post.topic_id, - link_post_id: post.id) + reflected_ids << tl.try(:id) end end @@ -203,7 +202,14 @@ class TopicLink < ActiveRecord::Base # Remove links that aren't there anymore if added_urls.present? TopicLink.delete_all ["(url not in (:urls)) AND (post_id = :post_id AND NOT reflection)", urls: added_urls, post_id: post.id] - TopicLink.delete_all ["(url not in (:urls)) AND (link_post_id = :post_id AND reflection)", urls: reflected_urls, post_id: post.id] + + reflected_ids.compact! + if reflected_ids.present? + TopicLink.delete_all ["(id not in (:reflected_ids)) AND (link_post_id = :post_id AND reflection)", + reflected_ids: reflected_ids, post_id: post.id] + else + TopicLink.delete_all ["link_post_id = :post_id AND reflection", post_id: post.id] + end else TopicLink.delete_all ["(post_id = :post_id AND NOT reflection) OR (link_post_id = :post_id AND reflection)", post_id: post.id] end diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb index 9cf5b3ee594..402ebf6bff6 100644 --- a/spec/components/post_revisor_spec.rb +++ b/spec/components/post_revisor_spec.rb @@ -357,7 +357,7 @@ describe PostRevisor do revisor.revise!(newuser, { title: 'this is a test topic' }) end - message = messages.find { |message| message.channel == "/topic/#{topic.id}" } + message = messages.find { |m| m.channel == "/topic/#{topic.id}" } payload = message.data expect(payload[:reload_topic]).to eq(true) end diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index bb5afa16c75..081c8120c80 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -113,20 +113,9 @@ http://b.com/#{'a'*500} expect(reflection.link_post_id).to eq(linked_post.id) expect(reflection.user_id).to eq(link.user_id) - end - context 'removing a link' do - - before do - post.revise(post.user, { raw: "no more linkies" }) - TopicLink.extract_from(post) - end - - it 'should remove the link' do - expect(topic.topic_links.where(post_id: post.id)).to be_blank - # should remove the reflected link - expect(other_topic.topic_links).to be_blank - end + linked_post.revise(post.user, { raw: "no more linkies https://eviltrout.com" }) + expect(other_topic.topic_links.where(link_post_id: linked_post.id)).to be_blank end end @@ -249,8 +238,7 @@ http://b.com/#{'a'*500} alternate_uri = URI.parse(Discourse.base_url) url = "http://#{alternate_uri.host}:5678/t/topic-slug/#{other_topic.id}" - post = topic.posts.create(user: user, - raw: "Link to another topic: #{url}") + post = topic.posts.create(user: user, raw: "Link to another topic: #{url}") TopicLink.extract_from(post) reflection = other_topic.topic_links.first