From 1f6f1604c9e9c0a61cc9cb4992cb7e5db8c4e546 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Fri, 26 Jun 2020 22:25:38 +0300 Subject: [PATCH] FIX: Serialize an empty array if no suggested topics exist (#10134) It used to return nil, which was ambiguous (empty vs absent result). --- app/serializers/suggested_topics_mixin.rb | 4 +-- .../serializers/topic_view_serializer_spec.rb | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/serializers/suggested_topics_mixin.rb b/app/serializers/suggested_topics_mixin.rb index 9fca424b0f5..74d3d65a9cf 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 55a9652fddc..e28182765e4 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