diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index b35ec1c76..4ba19ef7b 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -233,7 +233,8 @@ class TopicLink < ActiveRecord::Base results = TopicLink .includes(:post => :user) - .where(topic_id: topic.id).limit(200) + .where(topic_id: topic.id, reflection: false) + .limit(200) lookup = {} results.each do |tl| diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb index 487c6852f..5fb86c7fc 100644 --- a/spec/models/topic_link_spec.rb +++ b/spec/models/topic_link_spec.rb @@ -16,6 +16,8 @@ describe TopicLink do topic.user end + let(:post) { Fabricate(:post) } + it "can't link to the same topic" do ftl = TopicLink.new(url: "/t/#{topic.id}", topic_id: topic.id, @@ -320,6 +322,31 @@ http://b.com/#{'a'*500} end end + + describe ".duplicate_lookup" do + let(:user) { Fabricate(:user, username: "junkrat") } + + let(:post_with_internal_link) do + Fabricate(:post, user: user, raw: "Check out this topic #{post.topic.url}/122131") + end + + it "should return the right response" do + TopicLink.extract_from(post_with_internal_link) + + result = TopicLink.duplicate_lookup(post_with_internal_link.topic) + expect(result.count).to eq(1) + + lookup = result["test.localhost/t/#{post.topic.slug}/#{post.topic.id}/122131"] + + expect(lookup[:domain]).to eq("test.localhost") + expect(lookup[:username]).to eq("junkrat") + expect(lookup[:posted_at].to_s).to eq(post_with_internal_link.created_at.to_s) + expect(lookup[:post_number]).to eq(1) + + result = TopicLink.duplicate_lookup(post.topic) + expect(result.count).to eq(0) + end + end end end