A platform for community discussion. Free, open, simple.
DEV: implements register_modifier(:serialize_topic_op_likes_data) (#31376)
Arpit Jalan9bb6d09d1a2e
diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rbindex 55a03994cdf..3230b198dcb 100644--- a/app/serializers/topic_list_item_serializer.rb+++ b/app/serializers/topic_list_item_serializer.rb@@ -36,7 +36,7 @@ class TopicListItemSerializer < ListableTopicSerializerenddef include_op_can_like?- theme_modifier_helper.serialize_topic_op_likes_data+ serialize_topic_op_likes_data_enabled?enddef op_can_like@@ -58,7 +58,7 @@ class TopicListItemSerializer < ListableTopicSerializerenddef include_op_liked?- theme_modifier_helper.serialize_topic_op_likes_data+ serialize_topic_op_likes_data_enabled?enddef op_liked@@ -72,7 +72,7 @@ class TopicListItemSerializer < ListableTopicSerializerenddef include_first_post_id?- theme_modifier_helper.serialize_topic_op_likes_data+ serialize_topic_op_likes_data_enabled?enddef first_post_id@@ -144,6 +144,13 @@ class TopicListItemSerializer < ListableTopicSerializerprivate+ def serialize_topic_op_likes_data_enabled?+ theme_enabled = theme_modifier_helper.serialize_topic_op_likes_data+ plugin_enabled = DiscoursePluginRegistry.apply_modifier(:serialize_topic_op_likes_data, false)++ theme_enabled || plugin_enabled+ end+def theme_modifier_helper@theme_modifier_helper ||= ThemeModifierHelper.new(request: scope.request)enddiff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rbindex 1d22952a77c..fd7f3054d4c 100644--- a/spec/serializers/topic_list_item_serializer_spec.rb+++ b/spec/serializers/topic_list_item_serializer_spec.rb@@ -101,10 +101,11 @@ RSpec.describe TopicListItemSerializer dolet(:user) { Fabricate(:user) }let(:moderator) { Fabricate(:moderator) }let(:first_post) { Fabricate(:post, topic: topic, user: user) }+ let(:plugin) { Plugin::Instance.new }before { topic.update!(first_post: first_post) }- it "serializes op_can_like" do+ it "serializes op_can_like when theme modifies the serialize_topic_op_likes_data to true" doallow_any_instance_of(ThemeModifierHelper).to receive(:serialize_topic_op_likes_data,).and_return(true)@@ -122,7 +123,18 @@ RSpec.describe TopicListItemSerializer doexpect(json.key?(:op_can_like)).to eq(false)end- it "serializes op_liked" do+ it "serializes op_can_like when plugin modifies the serialize_topic_op_likes_data to true" do+ modifier = :serialize_topic_op_likes_data+ proc = Proc.new { true }+ DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)+ json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json++ expect(json.key?(:op_can_like)).to eq(true)+ ensure+ DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)+ end++ it "serializes op_liked when theme modifies the serialize_topic_op_likes_data to true" doallow_any_instance_of(ThemeModifierHelper).to receive(:serialize_topic_op_likes_data,).and_return(true)@@ -150,7 +162,23 @@ RSpec.describe TopicListItemSerializer doexpect(json.key?(:op_liked)).to eq(false)end- it "serializes first_post_id" do+ it "serializes op_liked when plugin modifies the serialize_topic_op_likes_data to true" do+ modifier = :serialize_topic_op_likes_data+ proc = Proc.new { true }+ DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)+ PostAction.create!(+ user: user,+ post: first_post,+ post_action_type_id: PostActionType.types[:like],+ )+ json = TopicListItemSerializer.new(topic, scope: Guardian.new(user), root: false).as_json++ expect(json[:op_liked]).to eq(true)+ ensure+ DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)+ end++ it "serializes first_post_id when theme modifies the serialize_topic_op_likes_data to true" doallow_any_instance_of(ThemeModifierHelper).to receive(:serialize_topic_op_likes_data,).and_return(true)@@ -159,7 +187,7 @@ RSpec.describe TopicListItemSerializer doexpect(json[:first_post_id]).to eq(first_post.id)end- it "does not include op_can_like when theme modifier disallows" do+ it "does not include first_post_id when theme modifier disallows" doallow_any_instance_of(ThemeModifierHelper).to receive(:serialize_topic_op_likes_data,).and_return(false)@@ -167,5 +195,16 @@ RSpec.describe TopicListItemSerializer doexpect(json.key?(:first_post_id)).to eq(false)end++ it "serializes first_post_id when plugin modifies the serialize_topic_op_likes_data to true" do+ modifier = :serialize_topic_op_likes_data+ proc = Proc.new { true }+ DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)+ json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json++ expect(json[:first_post_id]).to eq(first_post.id)+ ensure+ DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)+ endendend