FIX: Reflected links weren't being cleaned up properly

This commit is contained in:
Robin Ward 2016-06-08 16:08:41 -04:00
parent aaafd9f26e
commit e38f17524b
3 changed files with 24 additions and 30 deletions

View File

@ -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

View File

@ -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

View File

@ -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