FIX: topic link reflections deleted on second save

This commit is contained in:
Sam 2016-06-10 17:24:30 +10:00
parent a496574e93
commit 65f466cf8c
2 changed files with 35 additions and 18 deletions

View File

@ -178,7 +178,17 @@ class TopicLink < ActiveRecord::Base
prefix = Discourse.base_url_no_prefix prefix = Discourse.base_url_no_prefix
reflected_url = "#{prefix}#{post.topic.relative_url(post.post_number)}" 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, topic_id: topic_id,
post_id: reflected_post.try(:id), post_id: reflected_post.try(:id),
url: reflected_url, url: reflected_url,
@ -188,6 +198,8 @@ class TopicLink < ActiveRecord::Base
link_topic_id: post.topic_id, link_topic_id: post.topic_id,
link_post_id: post.id) link_post_id: post.id)
end
reflected_ids << tl.try(:id) reflected_ids << tl.try(:id)
end end
end end

View File

@ -92,27 +92,32 @@ http://b.com/#{'a'*500}
topic.posts.create(user: user, raw: 'initial post') topic.posts.create(user: user, raw: 'initial post')
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}") 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 link = topic.topic_links.first
expect(link).to be_present expect(link).to be_present
expect(link).to be_internal expect(link).to be_internal
expect(link.url).to eq(url) expect(link.url).to eq(url)
expect(link.domain).to eq(test_uri.host) expect(link.domain).to eq(test_uri.host)
link.link_topic_id == other_topic.id link.link_topic_id == other_topic.id
expect(link).not_to be_reflection 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_present
expect(reflection).to be_reflection expect(reflection).to be_reflection
expect(reflection.post_id).to be_present expect(reflection.post_id).to be_present
expect(reflection.domain).to eq(test_uri.host) 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.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_topic_id).to eq(topic.id)
expect(reflection.link_post_id).to eq(linked_post.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" }) 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 expect(other_topic.topic_links.where(link_post_id: linked_post.id)).to be_blank