From 65f466cf8c863631e00e371778bf588e10dd204e Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 10 Jun 2016 17:24:30 +1000 Subject: [PATCH] FIX: topic link reflections deleted on second save --- app/models/topic_link.rb | 14 +++++++++++- spec/models/topic_link_spec.rb | 39 +++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index 516f9505dcf..786c5e625d4 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -178,7 +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, + + tl = TopicLink.find_by(topic_id: topic_id, + post_id: reflected_post.try(:id), + url: reflected_url) + + if tl + tl.update_columns(domain: Discourse.current_hostname, + link_topic_id: post.topic.id, + link_post_id: post.id) + else + tl = TopicLink.create(user_id: post.user_id, topic_id: topic_id, post_id: reflected_post.try(:id), url: reflected_url, @@ -188,6 +198,8 @@ class TopicLink < ActiveRecord::Base link_topic_id: post.topic_id, link_post_id: post.id) + end + reflected_ids << tl.try(:id) end end diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index 7ce59822d44..487c6852f30 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -92,27 +92,32 @@ http://b.com/#{'a'*500} topic.posts.create(user: user, raw: 'initial post') linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}") - TopicLink.extract_from(linked_post) + # this is subtle, but we had a bug were second time + # TopicLink.extract_from was called a reflection was nuked + 2.times do + topic.reload + TopicLink.extract_from(linked_post) - link = topic.topic_links.first - expect(link).to be_present - expect(link).to be_internal - expect(link.url).to eq(url) - expect(link.domain).to eq(test_uri.host) - link.link_topic_id == other_topic.id - expect(link).not_to be_reflection + link = topic.topic_links.first + expect(link).to be_present + expect(link).to be_internal + expect(link.url).to eq(url) + expect(link.domain).to eq(test_uri.host) + link.link_topic_id == other_topic.id + expect(link).not_to be_reflection - reflection = other_topic.topic_links.first + reflection = other_topic.topic_links.first - expect(reflection).to be_present - expect(reflection).to be_reflection - expect(reflection.post_id).to be_present - expect(reflection.domain).to eq(test_uri.host) - expect(reflection.url).to eq("http://#{test_uri.host}/t/unique-topic-name/#{topic.id}/#{linked_post.post_number}") - expect(reflection.link_topic_id).to eq(topic.id) - expect(reflection.link_post_id).to eq(linked_post.id) + expect(reflection).to be_present + expect(reflection).to be_reflection + expect(reflection.post_id).to be_present + expect(reflection.domain).to eq(test_uri.host) + expect(reflection.url).to eq("http://#{test_uri.host}/t/unique-topic-name/#{topic.id}/#{linked_post.post_number}") + expect(reflection.link_topic_id).to eq(topic.id) + expect(reflection.link_post_id).to eq(linked_post.id) - expect(reflection.user_id).to eq(link.user_id) + expect(reflection.user_id).to eq(link.user_id) + 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