FIX: Do not reset link counts when post is rebaked

This was an indentation mistake introduced in 44eba0b. Pretty understandable, considering we are indented 8 levels deep in this method. Will follow-up with a refactor to improve this.
This commit is contained in:
David Taylor 2018-12-05 17:16:27 +00:00 committed by Régis Hanol
parent 978f0db109
commit 37249c9a32
2 changed files with 36 additions and 23 deletions

View File

@ -223,34 +223,35 @@ SQL
# If we can't find the route, no big deal
end
end
end
# Remove links that aren't there anymore
if added_urls.present?
# Remove links that aren't there anymore
if added_urls.present?
TopicLink.where(
"(url not in (:urls)) AND (post_id = :post_id AND NOT reflection)",
urls: added_urls, post_id: post.id
).delete_all
reflected_ids.compact!
if reflected_ids.present?
TopicLink.where(
"(url not in (:urls)) AND (post_id = :post_id AND NOT reflection)",
urls: added_urls, post_id: post.id
"(id not in (:reflected_ids)) AND (link_post_id = :post_id AND reflection)",
reflected_ids: reflected_ids, post_id: post.id
).delete_all
reflected_ids.compact!
if reflected_ids.present?
TopicLink.where(
"(id not in (:reflected_ids)) AND (link_post_id = :post_id AND reflection)",
reflected_ids: reflected_ids, post_id: post.id
).delete_all
else
TopicLink
.where("link_post_id = :post_id AND reflection", post_id: post.id)
.delete_all
end
else
TopicLink
.where(
"(post_id = :post_id AND NOT reflection) OR (link_post_id = :post_id AND reflection)",
post_id: post.id
)
.where("link_post_id = :post_id AND reflection", post_id: post.id)
.delete_all
end
else
TopicLink
.where(
"(post_id = :post_id AND NOT reflection) OR (link_post_id = :post_id AND reflection)",
post_id: post.id
)
.delete_all
end
end
# Crawl a link's title after it's saved

View File

@ -26,15 +26,17 @@ describe TopicLink do
end
describe 'external links' do
before do
post = Fabricate(:post, raw: <<~RAW, user: user, topic: topic)
let(:post2) do
Fabricate(:post, raw: <<~RAW, user: user, topic: topic)
http://a.com/
https://b.com/b
http://#{'a' * 200}.com/invalid
//b.com/#{'a' * 500}
RAW
end
TopicLink.extract_from(post)
before do
TopicLink.extract_from(post2)
end
it 'works' do
@ -45,6 +47,16 @@ describe TopicLink do
)
end
it "doesn't reset them when rebaking" do
old_ids = topic.topic_links.pluck(:id)
TopicLink.extract_from(post2)
new_ids = topic.topic_links.pluck(:id)
expect(new_ids).to contain_exactly(*old_ids)
end
end
describe 'internal links' do