FIX: In reply to would sometimes have a broken link

FIX: In reply to would sometimes have a broken link

diff --git a/app/assets/javascripts/discourse/templates/components/reviewable-topic-link.hbs b/app/assets/javascripts/discourse/templates/components/reviewable-topic-link.hbs
index 30d2dc8..4b1704a 100644
--- a/app/assets/javascripts/discourse/templates/components/reviewable-topic-link.hbs
+++ b/app/assets/javascripts/discourse/templates/components/reviewable-topic-link.hbs
@@ -1,7 +1,7 @@
 <div class='post-topic'>
   {{#if reviewable.topic}}
     {{topic-status topic=reviewable.topic}}
-    <a href={{reviewable.topic_url}} class='title-text'>{{reviewable.topic.title}}</a>
+    <a href={{reviewable.target_url}} class='title-text'>{{reviewable.topic.title}}</a>
     {{category-badge reviewable.category}}
     {{reviewable-tags tags=reviewable.topic_tags tagName=''}}
   {{else if (has-block)}}
diff --git a/app/serializers/reviewable_serializer.rb b/app/serializers/reviewable_serializer.rb
index 8969f47..d1a99c8 100644
--- a/app/serializers/reviewable_serializer.rb
+++ b/app/serializers/reviewable_serializer.rb
@@ -13,6 +13,7 @@ class ReviewableSerializer < ApplicationSerializer
     :type,
     :topic_id,
     :topic_url,
+    :target_url,
     :topic_tags,
     :category_id,
     :created_at,
@@ -107,13 +108,21 @@ class ReviewableSerializer < ApplicationSerializer
     object.topic.present? && SiteSetting.tagging_enabled?
   end
 
+  def target_url
+    return object.target.url if object.target.is_a?(Post) && object.target.present?
+    topic_url
+  end
+
+  def include_target_url?
+    target_url.present?
+  end
+
   def topic_url
-    return object.target.url if object.target.is_a?(Post)
-    return object.topic.url
+    return object.topic&.url
   end
 
   def include_topic_url?
-    object.topic.present?
+    topic_url.present?
   end
 
   def include_topic_id?
diff --git a/spec/serializers/reviewable_serializer_spec.rb b/spec/serializers/reviewable_serializer_spec.rb
index 6ce23e0..8c2e95f 100644
--- a/spec/serializers/reviewable_serializer_spec.rb
+++ b/spec/serializers/reviewable_serializer_spec.rb
@@ -22,9 +22,7 @@ describe ReviewableSerializer do
 
   it 'Includes the removed topic id when the topis was deleted' do
     reviewable.topic.trash!(admin)
-
     json = described_class.new(reviewable.reload, scope: Guardian.new(admin), root: nil).as_json
-
     expect(json[:removed_topic_id]).to eq reviewable.topic_id
   end
 
@@ -33,4 +31,29 @@ describe ReviewableSerializer do
     json = ReviewableQueuedPostSerializer.new(reviewable, scope: Guardian.new(admin), root: nil).as_json
     expect(json['payload']).to be_blank
   end
+
+  describe "urls" do
+
+    it "links to the flagged post" do
+      fp = Fabricate(:reviewable_flagged_post)
+      json = described_class.new(fp, scope: Guardian.new(admin), root: nil).as_json
+      expect(json[:target_url]).to eq(fp.post.url)
+      expect(json[:topic_url]).to eq(fp.topic.url)
+    end
+
+    it "supports deleted topics" do
+      fp = Fabricate(:reviewable_flagged_post)
+      fp.topic.trash!(admin)
+      fp.reload
+
+      json = described_class.new(fp, scope: Guardian.new(admin), root: nil).as_json
+      expect(json[:topic_url]).to be_blank
+    end
+
+    it "links to the queued post" do
+      json = described_class.new(reviewable, scope: Guardian.new(admin), root: nil).as_json
+      expect(json[:target_url]).to eq(reviewable.topic.url)
+      expect(json[:topic_url]).to eq(reviewable.topic.url)
+    end
+  end
 end

GitHub sha: 8c4e16ea

1 Like