Skip to content

A platform for community discussion. Free, open, simple.

FIX: Inconsistent op_like_count serialization with serialize_topic_op_likes_data (#36950)

Martin Brennan4f6c09875ca5
diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb
index b1c1aac0c98..bbb9d58d88c 100644
--- a/app/models/topic_list.rb
+++ b/app/models/topic_list.rb
@@ -145,6 +145,8 @@ class TopicList
topic_preloader_associations << :localizations if SiteSetting.content_localization_enabled
+ topic_preloader_associations << :first_post
+
DiscoursePluginRegistry.topic_preloader_associations.each do |a|
fields = a[:fields]
condition = a[:condition]
diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rb
index 2734c91b246..0e5194b1e69 100644
--- a/app/serializers/topic_list_item_serializer.rb
+++ b/app/serializers/topic_list_item_serializer.rb
@@ -119,13 +119,6 @@ class TopicListItemSerializer < ListableTopicSerializer
object.private_message?
end
- def include_op_like_count?
- # PERF: long term we probably want a cheaper way of looking stuff up
- # this is rather odd code, but we need to have op_likes loaded somehow
- # simplest optimisation is adding a cache column on topic.
- object.association(:first_post).loaded?
- end
-
def include_featured_link?
SiteSetting.topic_featured_link_enabled
end
diff --git a/spec/models/topic_list_spec.rb b/spec/models/topic_list_spec.rb
index be85d5c1ca5..fde5e45c35c 100644
--- a/spec/models/topic_list_spec.rb
+++ b/spec/models/topic_list_spec.rb
@@ -56,6 +56,16 @@ RSpec.describe TopicList do
expect(topic.category_user_data).to eq(category_user)
end
+
+ it "preloads first_post association" do
+ first_post = Fabricate(:post, topic: topic, post_number: 1)
+ topic.update!(first_post: first_post)
+
+ loaded_topic = topic_list.load_topics.first
+
+ expect(loaded_topic.association(:first_post).loaded?).to eq(true)
+ expect(loaded_topic.first_post).to eq(first_post)
+ end
end
describe "#top_tags" do
diff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rb
index 53b60996579..75fc09613d4 100644
--- a/spec/serializers/topic_list_item_serializer_spec.rb
+++ b/spec/serializers/topic_list_item_serializer_spec.rb
@@ -206,6 +206,11 @@ RSpec.describe TopicListItemSerializer do
ensure
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)
end
+
+ it "serializes op_like_count" do
+ json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json
+ expect(json[:op_like_count]).to eq(first_post.like_count)
+ end
end
describe "#is_hot" do