FIX: Serialize an empty array if no suggested topics exist (#10134)

FIX: Serialize an empty array if no suggested topics exist (#10134)

It used to return nil, which was ambiguous (empty vs absent result).

diff --git a/app/serializers/suggested_topics_mixin.rb b/app/serializers/suggested_topics_mixin.rb
index 9fca424..74d3d65 100644
--- a/app/serializers/suggested_topics_mixin.rb
+++ b/app/serializers/suggested_topics_mixin.rb
@@ -7,11 +7,11 @@ module SuggestedTopicsMixin
   end
 
   def include_related_messages?
-    object.next_page.nil? && object.related_messages&.topics.present?
+    object.next_page.nil? && object.related_messages&.topics
   end
 
   def include_suggested_topics?
-    object.next_page.nil? && object.suggested_topics&.topics.present?
+    object.next_page.nil? && object.suggested_topics&.topics
   end
 
   def related_messages
diff --git a/spec/serializers/topic_view_serializer_spec.rb b/spec/serializers/topic_view_serializer_spec.rb
index 55a9652..e281827 100644
--- a/spec/serializers/topic_view_serializer_spec.rb
+++ b/spec/serializers/topic_view_serializer_spec.rb
@@ -118,6 +118,40 @@ describe TopicViewSerializer do
         expect(json[:suggested_topics]).to eq(nil)
       end
     end
+
+    describe 'with private messages' do
+      let!(:topic) do
+        Fabricate(:private_message_topic,
+          highest_post_number: 1,
+          topic_allowed_users: [
+            Fabricate.build(:topic_allowed_user, user: user)
+          ]
+        )
+      end
+
+      let!(:topic2) do
+        Fabricate(:private_message_topic,
+          highest_post_number: 1,
+          topic_allowed_users: [
+            Fabricate.build(:topic_allowed_user, user: user)
+          ]
+        )
+      end
+
+      it 'includes suggested topics' do
+        TopicUser.change(user, topic2.id, notification_level: TopicUser.notification_levels[:tracking])
+
+        json = serialize_topic(topic, user)
+        expect(json[:suggested_topics].map { |t| t[:id] }).to contain_exactly(topic2.id)
+      end
+
+      it 'does not include suggested topics if all PMs are read' do
+        TopicUser.update_last_read(user, topic2.id, 1, 1, 0)
+
+        json = serialize_topic(topic, user)
+        expect(json[:suggested_topics]).to eq([])
+      end
+    end
   end
 
   describe 'when tags added to private message topics' do

GitHub sha: 4efc1266

This commit appears in #10134 which was approved by eviltrout. It was merged by udan11.

Are we able to fab! here?