FIX: don't raise an exception when a link was already extracted

This commit is contained in:
Régis Hanol 2016-11-30 18:05:59 +01:00
parent dec8a861f0
commit 57d0369894
1 changed files with 17 additions and 20 deletions

View File

@ -37,7 +37,7 @@ class TopicLink < ActiveRecord::Base
def self.topic_map(guardian, topic_id) def self.topic_map(guardian, topic_id)
# Sam: complicated reports are really hard in AR # Sam: complicated reports are really hard in AR
builder = SqlBuilder.new <<SQL builder = SqlBuilder.new <<-SQL
SELECT ftl.url, SELECT ftl.url,
COALESCE(ft.title, ftl.title) AS title, COALESCE(ft.title, ftl.title) AS title,
ftl.link_topic_id, ftl.link_topic_id,
@ -163,20 +163,20 @@ SQL
added_urls << url added_urls << url
topic_link = TopicLink.find_by(topic_id: post.topic_id, unless TopicLink.find_by(topic_id: post.topic_id, post_id: post.id, url: url)
post_id: post.id, begin
url: url) TopicLink.create!(post_id: post.id,
user_id: post.user_id,
unless topic_link topic_id: post.topic_id,
TopicLink.create!(post_id: post.id, url: url,
user_id: post.user_id, domain: parsed.host || Discourse.current_hostname,
topic_id: post.topic_id, internal: internal,
url: url, link_topic_id: topic_id,
domain: parsed.host || Discourse.current_hostname, link_post_id: reflected_post.try(:id),
internal: internal, quote: link.is_quote)
link_topic_id: topic_id, rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
link_post_id: reflected_post.try(:id), # it's fine
quote: link.is_quote) end
end end
# Create the reflection if we can # Create the reflection if we can
@ -184,17 +184,14 @@ SQL
topic = Topic.find_by(id: topic_id) topic = Topic.find_by(id: topic_id)
if topic && post.topic && post.topic.archetype != 'private_message' && topic.archetype != 'private_message' if topic && post.topic && post.topic.archetype != 'private_message' && topic.archetype != 'private_message'
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.find_by(topic_id: topic_id, tl = TopicLink.find_by(topic_id: topic_id,
post_id: reflected_post.try(:id), post_id: reflected_post.try(:id),
url: reflected_url) url: reflected_url)
unless tl unless tl
tl = TopicLink.create!(user_id: post.user_id, 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,
@ -206,7 +203,7 @@ SQL
end end
reflected_ids << tl.try(:id) reflected_ids << tl.id if tl.persisted?
end end
end end