From dee9e9a51f7b15c9a139cbec2fb5965970affd99 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Wed, 13 Feb 2013 15:22:04 -0500
Subject: [PATCH] Fix bug where links to posts weren't being tracked

---
 app/models/topic_link.rb            |  9 +++++++++
 lib/pretty_text.rb                  | 12 ++++++++++++
 spec/components/pretty_text_spec.rb |  8 ++++++++
 spec/models/topic_link_spec.rb      | 30 ++++++++++++++++++++++++++++-
 4 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb
index d4215ae81..2d63c1281 100644
--- a/app/models/topic_link.rb
+++ b/app/models/topic_link.rb
@@ -53,6 +53,15 @@ class TopicLink < ActiveRecord::Base
 
             topic_id = route[:topic_id]
             post_number = route[:post_number] || 1
+
+            # Store the canonical URL
+            topic = Topic.where(id: topic_id).first
+
+            if topic.present?
+              url = "#{Discourse.base_url}#{topic.relative_url}"
+              url << "/#{post_number}" if post_number.to_i > 1
+            end
+
           end
 
           # Skip linking to ourselves
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 075bc543b..af8b2d221 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -216,6 +216,18 @@ module PrettyText
     doc.css("a").each do |l|
       links << l.attributes["href"].to_s
     end
+
+    doc.css("aside.quote").each do |a|
+      topic_id = a.attributes['data-topic']
+      
+      url = "/t/topic/#{topic_id}"
+      if post_number = a.attributes['data-post']
+        url << "/#{post_number}"
+      end
+
+      links << url
+    end
+
     links
   end
 
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index 5aaa9330e..bcd5875b8 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -148,6 +148,14 @@ test
       PrettyText.extract_links("<a href='http://cnn.com'>http://bla.com</a>").to_a.should == ["http://cnn.com"]
     end
 
+    it "should extract links to topics" do
+      PrettyText.extract_links("<aside class=\"quote\" data-topic=\"321\">aside</aside>").to_a.should == ["/t/topic/321"]
+    end
+
+    it "should extract links to posts" do
+      PrettyText.extract_links("<aside class=\"quote\" data-topic=\"1234\" data-post=\"4567\">aside</aside>").to_a.should == ["/t/topic/1234/4567"]
+    end
+
     it "should not preserve tags in code blocks" do 
       PrettyText.excerpt("<pre><code class='handlebars'>&lt;h3&gt;Hours&lt;/h3&gt;</code></pre>",100).should == "&lt;h3&gt;Hours&lt;/h3&gt;"
     end
diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb
index b158ccf86..ba11abdf0 100644
--- a/spec/models/topic_link_spec.rb
+++ b/spec/models/topic_link_spec.rb
@@ -46,12 +46,40 @@ describe TopicLink do
 
   describe 'internal links' do
 
+    context "rendered onebox" do
+
+      before do
+        @other_topic = Fabricate(:topic, user: @user)
+        @other_topic.posts.create(user: @user, raw: "some content for the first post")
+        @other_post = @other_topic.posts.create(user: @user, raw: "some content for the second post")
+
+        @url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}/#{@other_post.post_number}"
+
+        @topic.posts.create(user: @user, raw: 'initial post')
+        @post = @topic.posts.create(user: @user, raw: "Link to another topic:\n\n#{@url}\n\n")
+        @post.reload
+        TopicLink.extract_from(@post)
+
+        @link = @topic.topic_links.first
+      end
+
+      it "should have a link" do
+        @link.should be_present
+      end
+
+      it "should be the canonical URL" do
+        @link.url.should == @url
+      end      
+
+
+    end
+
     context 'topic link' do
       before do
         @other_topic = Fabricate(:topic, user: @user)
         @other_post = @other_topic.posts.create(user: @user, raw: "some content")
 
-        @url = "http://#{test_uri.host}/t/topic-slug/#{@other_topic.id}"
+        @url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}"
 
         @topic.posts.create(user: @user, raw: 'initial post')
         @post = @topic.posts.create(user: @user, raw: "Link to another topic: #{@url}")