A platform for community discussion. Free, open, simple.
FEATURE: add first_post_id to TopicListItemSerializer (#31254)
Arpit Jalan85a95e9aa14f
diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rbindex 37e74db6d3a..55a03994cdf 100644--- a/app/serializers/topic_list_item_serializer.rb+++ b/app/serializers/topic_list_item_serializer.rb@@ -12,6 +12,7 @@ class TopicListItemSerializer < ListableTopicSerializer:op_like_count,:op_can_like,:op_liked,+ :first_post_id,:pinned_globally,:liked_post_numbers,:featured_link,@@ -70,6 +71,15 @@ class TopicListItemSerializer < ListableTopicSerializer).exists?end+ def include_first_post_id?+ theme_modifier_helper.serialize_topic_op_likes_data+ end++ def first_post_id+ return false if !object.first_post+ object.first_post.id+ end+def last_poster_usernameposters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username)enddiff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rbindex 56b26916ea7..1d22952a77c 100644--- a/spec/serializers/topic_list_item_serializer_spec.rb+++ b/spec/serializers/topic_list_item_serializer_spec.rb@@ -149,5 +149,23 @@ RSpec.describe TopicListItemSerializer doexpect(json.key?(:op_liked)).to eq(false)end++ it "serializes first_post_id" do+ allow_any_instance_of(ThemeModifierHelper).to receive(+ :serialize_topic_op_likes_data,+ ).and_return(true)+ json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json++ expect(json[:first_post_id]).to eq(first_post.id)+ end++ it "does not include op_can_like when theme modifier disallows" do+ allow_any_instance_of(ThemeModifierHelper).to receive(+ :serialize_topic_op_likes_data,+ ).and_return(false)+ json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json++ expect(json.key?(:first_post_id)).to eq(false)+ endendend