FEATURE: add first_post_id to TopicListItemSerializer (#31254)

Will be used in this feature:
https://github.com/discourse/discourse-topic-cards/pull/36
This commit is contained in:
Arpit Jalan 2025-02-10 21:53:05 +05:30 committed by GitHub
parent 50136ee4e6
commit 85a95e9aa1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -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_username
posters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username)
end

View File

@ -149,5 +149,23 @@ RSpec.describe TopicListItemSerializer do
expect(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)
end
end
end