A platform for community discussion. Free, open, simple.
DEV: Add `op_reactions_data` to suggested topics (#36755)
Isaac Janzend1b489afd291
diff --git a/plugins/discourse-reactions/plugin.rb b/plugins/discourse-reactions/plugin.rbindex aa830058277..179ac4b1332 100644--- a/plugins/discourse-reactions/plugin.rb+++ b/plugins/discourse-reactions/plugin.rb@@ -207,6 +207,37 @@ after_initialize dolike_post_action.present? && !has_matching_reaction_userend++ def self.op_reactions_data_for_topic(topic, scope)+ return nil unless topic.first_post++ post = topic.first_post+ like_action =+ (+ if scope.user+ PostAction.find_by(+ user_id: scope.user.id,+ post_id: post.id,+ post_action_type_id: PostActionType.types[:like],+ )+ else+ nil+ end+ )++ {+ id: post.id,+ user_id: post.user_id,+ yours: post.user_id == scope.current_user&.id,+ reactions: reactions_for_post(post),+ current_user_reaction: current_user_reaction_for_post(post, scope),+ current_user_used_main_reaction: current_user_used_main_reaction_for_post(post, scope),+ reaction_users_count: reaction_users_count_for_post(post) || 0,+ likeAction: {+ canToggle: like_action ? scope.can_delete_post_action?(like_action) : true,+ },+ }+ endendadd_to_serializer(:post, :reactions) { ReactionsSerializerHelpers.reactions_for_post(object) }@@ -234,37 +265,20 @@ after_initialize doscope.user,)end,- ) do- return nil unless object.first_post-- post = object.first_post- like_action =- (- if scope.user- PostAction.find_by(- user_id: scope.user.id,- post_id: post.id,- post_action_type_id: PostActionType.types[:like],- )- else- nil- end- )+ ) { ReactionsSerializerHelpers.op_reactions_data_for_topic(object, scope) }- {- id: post.id,- user_id: post.user_id,- yours: post.user_id == scope.current_user&.id,- reactions: ReactionsSerializerHelpers.reactions_for_post(post),- current_user_reaction: ReactionsSerializerHelpers.current_user_reaction_for_post(post, scope),- current_user_used_main_reaction:- ReactionsSerializerHelpers.current_user_used_main_reaction_for_post(post, scope),- reaction_users_count: ReactionsSerializerHelpers.reaction_users_count_for_post(post) || 0,- likeAction: {- canToggle: like_action ? scope.can_delete_post_action?(like_action) : true,- },- }- end+ add_to_serializer(+ :suggested_topic,+ :op_reactions_data,+ include_condition: -> do+ object.association(:first_post).loaded? &&+ DiscoursePluginRegistry.apply_modifier(+ :include_discourse_reactions_data_on_suggested_topics,+ false,+ scope.user,+ )+ end,+ ) { ReactionsSerializerHelpers.op_reactions_data_for_topic(object, scope) }add_to_serializer(:topic_view, :valid_reactions) { DiscourseReactions::Reaction.valid_reactions }diff --git a/plugins/discourse-reactions/spec/serializers/topic_list_item_serializer_spec.rb b/plugins/discourse-reactions/spec/serializers/op_reactions_data_serializer_spec.rbsimilarity index 50%rename from plugins/discourse-reactions/spec/serializers/topic_list_item_serializer_spec.rbrename to plugins/discourse-reactions/spec/serializers/op_reactions_data_serializer_spec.rbindex ad5b47aad1b..730b116de0f 100644--- a/plugins/discourse-reactions/spec/serializers/topic_list_item_serializer_spec.rb+++ b/plugins/discourse-reactions/spec/serializers/op_reactions_data_serializer_spec.rb@@ -3,7 +3,7 @@require_relative "../fabricators/reaction_fabricator"require_relative "../fabricators/reaction_user_fabricator"-describe TopicListItemSerializer do+RSpec.shared_examples "op_reactions_data serializer" do |serializer_class, modifier_name|fab!(:user_1, :user)fab!(:user_2, :user)fab!(:user_3, :user)@@ -18,18 +18,11 @@ describe TopicListItemSerializer doSiteSetting.discourse_reactions_enabled_reactions = "otter|+1|tada"SiteSetting.discourse_reactions_like_icon = "heart"- plugin_instance.register_modifier(- :include_discourse_reactions_data_on_topic_list,- &modifier_block- )+ plugin_instance.register_modifier(modifier_name, &modifier_block)endafter do- DiscoursePluginRegistry.unregister_modifier(- plugin_instance,- :include_discourse_reactions_data_on_topic_list,- &modifier_block- )+ DiscoursePluginRegistry.unregister_modifier(plugin_instance, modifier_name, &modifier_block)enddescribe "#op_reactions_data" do@@ -37,7 +30,7 @@ describe TopicListItemSerializer doit "does not include op_reactions_data" dotopic_without_first_post = Fabricate(:topic)json =- TopicListItemSerializer.new(+ serializer_class.new(topic_without_first_post,scope: Guardian.new(user_1),root: false,@@ -52,33 +45,22 @@ describe TopicListItemSerializer dolet(:false_modifier_block) { Proc.new { false } }before do- DiscoursePluginRegistry.unregister_modifier(- plugin_instance,- :include_discourse_reactions_data_on_topic_list,- &modifier_block- )- false_plugin_instance.register_modifier(- :include_discourse_reactions_data_on_topic_list,- &false_modifier_block- )+ DiscoursePluginRegistry.unregister_modifier(plugin_instance, modifier_name, &modifier_block)+ false_plugin_instance.register_modifier(modifier_name, &false_modifier_block)endafter doDiscoursePluginRegistry.unregister_modifier(false_plugin_instance,- :include_discourse_reactions_data_on_topic_list,+ modifier_name,&false_modifier_block)- # Re-register the original modifier for subsequent tests- plugin_instance.register_modifier(- :include_discourse_reactions_data_on_topic_list,- &modifier_block- )+ plugin_instance.register_modifier(modifier_name, &modifier_block)endit "does not include op_reactions_data" dotopic.association(:first_post).target = first_post- json = TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json+ json = serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_jsonexpect(json[:op_reactions_data]).to be_nilend@@ -95,17 +77,22 @@ describe TopicListItemSerializer do]end- it "includes basic post information" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json+ it "includes all required fields" do+ json = serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_jsonexpect(json[:op_reactions_data]).to be_presentexpect(json[:op_reactions_data][:id]).to eq(first_post.id)expect(json[:op_reactions_data][:user_id]).to eq(user_1.id)expect(json[:op_reactions_data][:yours]).to eq(true)+ expect(json[:op_reactions_data]).to have_key(:reactions)+ expect(json[:op_reactions_data]).to have_key(:current_user_reaction)+ expect(json[:op_reactions_data]).to have_key(:current_user_used_main_reaction)+ expect(json[:op_reactions_data]).to have_key(:reaction_users_count)+ expect(json[:op_reactions_data]).to have_key(:likeAction)endit "shows yours as false for other users" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(user_2), root: false).as_json+ json = serializer_class.new(topic, scope: Guardian.new(user_2), root: false).as_jsonexpect(json[:op_reactions_data][:yours]).to eq(false)end@@ -128,43 +115,21 @@ describe TopicListItemSerializer do]end- it "includes reactions sorted by count" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json+ it "includes reactions sorted by count and current user data" do+ json = serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_json- # otter has 2 users, +1 has 1 userexpect(json[:op_reactions_data][:reactions]).to eq([{ id: "otter", type: :emoji, count: 2 }, { id: "+1", type: :emoji, count: 1 }],)- end-- it "includes current_user_reaction for reacting user" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-expect(json[:op_reactions_data][:current_user_reaction]).to eq({ id: "otter", type: :emoji, can_undo: true },)- end-- it "includes current_user_used_main_reaction" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- # User 1 used "otter" reaction, not the main reaction "heart"expect(json[:op_reactions_data][:current_user_used_main_reaction]).to eq(false)- end-- it "includes reaction_users_count" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- # 3 reaction users: user_1 and user_2 for otter, user_3 for +1expect(json[:op_reactions_data][:reaction_users_count]).to eq(3)endend- context "with only likes (no custom reactions)" do+ context "with only likes" dobefore doFabricate(:post_action,@@ -183,26 +148,20 @@ describe TopicListItemSerializer doendit "shows likes as main_reaction_id" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json+ json = serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_jsonexpect(json[:op_reactions_data][:reactions]).to eq([{ id: "heart", type: :emoji, count: 1 }],)end- it "includes current_user_reaction as nil for non-liking user" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- expect(json[:op_reactions_data][:current_user_reaction]).to be_nil- end-- it "includes current_user_reaction for liking user" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_2), root: false).as_json+ it "includes correct current_user_reaction based on user" do+ json_non_liker =+ serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_json+ json_liker = serializer_class.new(topic, scope: Guardian.new(user_2), root: false).as_json- expect(json[:op_reactions_data][:current_user_reaction]).to eq(+ expect(json_non_liker[:op_reactions_data][:current_user_reaction]).to be_nil+ expect(json_liker[:op_reactions_data][:current_user_reaction]).to eq({ id: "heart", type: :emoji, can_undo: true },)end@@ -228,52 +187,34 @@ describe TopicListItemSerializer do]end- it "shows canToggle as true when user has a like action they can delete" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_2), root: false).as_json-- expect(json[:op_reactions_data][:likeAction][:canToggle]).to eq(true)- end+ it "shows canToggle correctly based on permissions and undo window" do+ json_with_like =+ serializer_class.new(topic, scope: Guardian.new(user_2), root: false).as_json+ json_without_like =+ serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_json- it "shows canToggle as true when user has no like action" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json+ expect(json_with_like[:op_reactions_data][:likeAction][:canToggle]).to eq(true)+ expect(json_without_like[:op_reactions_data][:likeAction][:canToggle]).to eq(true)- expect(json[:op_reactions_data][:likeAction][:canToggle]).to eq(true)- end-- it "shows canToggle as false when user cannot delete their like action" doSiteSetting.post_undo_action_window_mins = 10like_action.update!(created_at: 11.minutes.ago)- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_2), root: false).as_json-- expect(json[:op_reactions_data][:likeAction][:canToggle]).to eq(false)+ json_expired =+ serializer_class.new(topic, scope: Guardian.new(user_2), root: false).as_json+ expect(json_expired[:op_reactions_data][:likeAction][:canToggle]).to eq(false)endendcontext "for anonymous users" do- it "returns nil for current_user_reaction" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json+ it "returns appropriate values for anonymous users" do+ json = serializer_class.new(topic, scope: Guardian.new(nil), root: false).as_jsonexpect(json[:op_reactions_data][:current_user_reaction]).to be_nilexpect(json[:op_reactions_data][:yours]).to eq(false)- expect(json[:op_reactions_data][:likeAction][:canToggle]).to eq(true)- end-- it "shows current_user_used_main_reaction as false" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-expect(json[:op_reactions_data][:current_user_used_main_reaction]).to eq(false)- end-- it "includes basic post data regardless of reactions" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-+ expect(json[:op_reactions_data][:likeAction][:canToggle]).to eq(true)expect(json[:op_reactions_data][:id]).to eq(first_post.id)expect(json[:op_reactions_data][:user_id]).to eq(user_1.id)- expect(json[:op_reactions_data][:yours]).to eq(false)endcontext "with existing reactions from other users" do@@ -295,31 +236,15 @@ describe TopicListItemSerializer do]end- it "can see all reactions from other users" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json+ it "can see all reactions and counts from other users" do+ json = serializer_class.new(topic, scope: Guardian.new(nil), root: false).as_jsonexpect(json[:op_reactions_data][:reactions]).to match_array([{ id: "otter", type: :emoji, count: 2 }, { id: "tada", type: :emoji, count: 1 }],)- end-- it "can see the reaction_users_count" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-expect(json[:op_reactions_data][:reaction_users_count]).to eq(3)- end-- it "does not show any current_user_reaction" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-expect(json[:op_reactions_data][:current_user_reaction]).to be_nilend-- it "shows current_user_used_main_reaction as false" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-- expect(json[:op_reactions_data][:current_user_used_main_reaction]).to eq(false)- endendcontext "with likes from other users" do@@ -348,22 +273,12 @@ describe TopicListItemSerializer doendit "can see likes as main_reaction_id" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json+ json = serializer_class.new(topic, scope: Guardian.new(nil), root: false).as_jsonexpect(json[:op_reactions_data][:reactions]).to eq([{ id: "heart", type: :emoji, count: 2 }],)- end-- it "shows canToggle as true even without being logged in" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-expect(json[:op_reactions_data][:likeAction][:canToggle]).to eq(true)- end-- it "does not show current_user_reaction" do- json = TopicListItemSerializer.new(topic, scope: Guardian.new(nil), root: false).as_json-expect(json[:op_reactions_data][:current_user_reaction]).to be_nilendend@@ -373,11 +288,7 @@ describe TopicListItemSerializer dolet(:reaction_otter) { Fabricate(:reaction, reaction_value: "otter", post: first_post) }before do- # User 1 uses custom reaction (otter counts as a like, so it will create a PostAction)Fabricate(:reaction_user, reaction: reaction_otter, user: user_1, post: first_post)-- # User 2 uses main reaction (like) - manually create just the PostAction- # We use skip_post_action: true to prevent the reaction_user fabricator from creating a duplicateFabricate(:post_action,post: first_post,@@ -394,100 +305,42 @@ describe TopicListItemSerializer do]end- it "correctly combines reactions and likes" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json+ it "correctly combines reactions and likes with proper user states" do+ json_custom_reaction =+ serializer_class.new(topic, scope: Guardian.new(user_1), root: false).as_json+ json_main_reaction =+ serializer_class.new(topic, scope: Guardian.new(user_2), root: false).as_json- # User 1 has otter reaction (counts as like)- # User 2 has main reaction (PostAction only, no ReactionUser)- # So we should see: otter (1) and heart (2 - includes both user 1's shadow like and user 2's direct like)- expect(json[:op_reactions_data][:reactions]).to match_array(+ expect(json_custom_reaction[:op_reactions_data][:reactions]).to match_array([{ id: "otter", type: :emoji, count: 1 }, { id: "heart", type: :emoji, count: 1 }],)- end-- it "shows correct current_user_reaction for user with custom reaction" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- expect(json[:op_reactions_data][:current_user_reaction]).to eq(+ expect(json_custom_reaction[:op_reactions_data][:current_user_reaction]).to eq({ id: "otter", type: :emoji, can_undo: true },)- # User 1 used otter, not the main reaction- expect(json[:op_reactions_data][:current_user_used_main_reaction]).to eq(false)- end-- it "shows correct current_user_reaction for user with main reaction" do- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_2), root: false).as_json-- expect(json[:op_reactions_data][:current_user_reaction]).to eq(- { id: "heart", type: :emoji, can_undo: true },- )- expect(json[:op_reactions_data][:current_user_used_main_reaction]).to eq(true)- end- end- context "when modifier returns false" do- let(:false_plugin_instance) { Plugin::Instance.new }- let(:false_modifier_block) { Proc.new { false } }-- before do- DiscoursePluginRegistry.unregister_modifier(- plugin_instance,- :include_discourse_reactions_data_on_topic_list,- &modifier_block- )- false_plugin_instance.register_modifier(- :include_discourse_reactions_data_on_topic_list,- &false_modifier_block+ expect(json_custom_reaction[:op_reactions_data][:current_user_used_main_reaction]).to eq(+ false,)- end- after do- DiscoursePluginRegistry.unregister_modifier(- false_plugin_instance,- :include_discourse_reactions_data_on_topic_list,- &false_modifier_block+ expect(json_main_reaction[:op_reactions_data][:current_user_reaction]).to eq(+ { id: "heart", type: :emoji, can_undo: true },)- # Re-register the original modifier for subsequent tests- plugin_instance.register_modifier(- :include_discourse_reactions_data_on_topic_list,- &modifier_block+ expect(json_main_reaction[:op_reactions_data][:current_user_used_main_reaction]).to eq(+ true,)end-- it "excludes op_reactions_data" do- topic.association(:first_post).target = first_post-- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- expect(json.key?(:op_reactions_data)).to eq(false)- end-- it "returns false" do- topic.association(:first_post).target = first_post-- json =- TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- expect(json.key?(:op_reactions_data)).to eq(false)- end- end-- it "returns true when first_post is loaded and modifier returns true" do- topic.association(:first_post).target = first_post- first_post.post_actions_with_reaction_users =- DiscourseReactions::TopicViewSerializerExtension.load_post_action_reaction_users_for_posts(- [first_post.id],- )[- first_post.id- ]-- json = TopicListItemSerializer.new(topic, scope: Guardian.new(user_1), root: false).as_json-- expect(json.key?(:op_reactions_data)).to eq(true)endendendend++describe TopicListItemSerializer do+ include_examples "op_reactions_data serializer",+ TopicListItemSerializer,+ :include_discourse_reactions_data_on_topic_list+end++describe SuggestedTopicSerializer do+ include_examples "op_reactions_data serializer",+ SuggestedTopicSerializer,+ :include_discourse_reactions_data_on_suggested_topics+end