A platform for community discussion. Free, open, simple.
FIX: reactions count when there are deprecated bookmarks (#36138)
Régis Hanol864e7b79b106
diff --git a/plugins/discourse-reactions/plugin.rb b/plugins/discourse-reactions/plugin.rbindex b5dd8f2aa09..aa830058277 100644--- a/plugins/discourse-reactions/plugin.rb+++ b/plugins/discourse-reactions/plugin.rb@@ -87,28 +87,45 @@ after_initialize doendend- likes =- post.post_actions.reject do |post_action|- # Get rid of any PostAction records that match up to a ReactionUser- # that is NOT main_reaction_id and is NOT excluded, otherwise we double- # up on the count/reaction shown in the UI.- next true if reaction_users_counting_as_like.include?(post_action.user_id)-- # Also get rid of any PostAction records that match up to a ReactionUser- # that is now the main_reaction_id and has historical data.- post- .post_actions_with_reaction_users- &.dig(post_action.id)- &.reaction_user- &.reaction- &.reaction_value == DiscourseReactions::Reaction.main_reaction_id- end+ likes_query =+ post.post_actions.where(post_action_type_id: PostActionType::LIKE_POST_ACTION_ID)++ # Get rid of any PostAction records that match up to a ReactionUser+ # that is NOT main_reaction_id and is NOT excluded, otherwise we double+ # up on the count/reaction shown in the UI.+ if reaction_users_counting_as_like.any?+ likes_query = likes_query.where.not(user_id: reaction_users_counting_as_like.to_a)+ end++ # Also get rid of any PostAction records that match up to a ReactionUser+ # that is now the main_reaction_id and has historical data.+ # This subquery checks if there's a matching ReactionUser with main_reaction_id.+ likes_query =+ likes_query.where(+ <<~SQL,+ post_actions.id NOT IN (+ SELECT post_actions.id+ FROM post_actions+ INNER JOIN discourse_reactions_reaction_users+ ON discourse_reactions_reaction_users.post_id = post_actions.post_id+ AND discourse_reactions_reaction_users.user_id = post_actions.user_id+ INNER JOIN discourse_reactions_reactions+ ON discourse_reactions_reactions.id = discourse_reactions_reaction_users.reaction_id+ WHERE post_actions.post_id = :post_id+ AND post_actions.post_action_type_id = :like_type+ AND discourse_reactions_reactions.reaction_value = :main_reaction+ )+ SQL+ post_id: post.id,+ like_type: PostActionType::LIKE_POST_ACTION_ID,+ main_reaction: DiscourseReactions::Reaction.main_reaction_id,+ )++ likes = likes_query.count# Likes will only be blank if there are only reactions where the reaction is in# discourse_reactions_excluded_from_like. All other reactions will have a `PostAction` record.- if likes.blank?- return(reactions.sort_by { |reaction| [-reaction[:count].to_i, reaction[:id]] })- end+ return reactions.sort_by { |reaction| [-reaction[:count].to_i, reaction[:id]] } if likes.zero?# Reactions using main_reaction_id only have a `PostAction` record,# not any `ReactionUser` records, as long as the main_reaction_id was never@@ -119,7 +136,7 @@ after_initialize doreactions << {id: DiscourseReactions::Reaction.main_reaction_id,type: :emoji,- count: likes.size + reaction_likes.sum { |r| r[:count] },+ count: likes + reaction_likes.sum { |r| r[:count] },}reactions.sort_by { |reaction| [-reaction[:count].to_i, reaction[:id]] }diff --git a/plugins/discourse-reactions/spec/serializers/post_serializer_spec.rb b/plugins/discourse-reactions/spec/serializers/post_serializer_spec.rbindex 223edecb16b..43515dabe50 100644--- a/plugins/discourse-reactions/spec/serializers/post_serializer_spec.rb+++ b/plugins/discourse-reactions/spec/serializers/post_serializer_spec.rb@@ -54,12 +54,10 @@ describe PostSerializer doit "renders custom reactions which should be sorted by count" dojson = PostSerializer.new(post_1, scope: Guardian.new(user_1), root: false).as_json- expect(json[:reactions]).to eq(- [- { id: "otter", type: :emoji, count: 2 },- { id: "+1", type: :emoji, count: 1 },- { id: "heart", type: :emoji, count: 1 },- ],+ expect(json[:reactions]).to contain_exactly(+ { id: "+1", type: :emoji, count: 1 },+ { id: "heart", type: :emoji, count: 1 },+ { id: "otter", type: :emoji, count: 2 },)expect(json[:current_user_reaction]).to eq({ type: :emoji, id: "otter", can_undo: true })@@ -72,12 +70,10 @@ describe PostSerializer doit "renders custom reactions sorted alphabetically if count is equal" dojson = PostSerializer.new(post_1, scope: Guardian.new(user_1), root: false).as_json- expect(json[:reactions]).to eq(- [- { id: "otter", type: :emoji, count: 2 },- { id: "+1", type: :emoji, count: 1 },- { id: "heart", type: :emoji, count: 1 },- ],+ expect(json[:reactions]).to contain_exactly(+ { id: "+1", type: :emoji, count: 1 },+ { id: "heart", type: :emoji, count: 1 },+ { id: "otter", type: :emoji, count: 2 },)end@@ -85,12 +81,22 @@ describe PostSerializer doSiteSetting.discourse_reactions_enabled_reactions = "+1"json = PostSerializer.new(post_1, scope: Guardian.new(user_1), root: false).as_json- expect(json[:reactions]).to eq(- [- { id: "otter", type: :emoji, count: 2 },- { id: "+1", type: :emoji, count: 1 },- { id: "heart", type: :emoji, count: 1 },- ],+ expect(json[:reactions]).to contain_exactly(+ { id: "+1", type: :emoji, count: 1 },+ { id: "heart", type: :emoji, count: 1 },+ { id: "otter", type: :emoji, count: 2 },+ )+ end++ it "does not count deprecated bookmarks as likes" do+ Fabricate(:post_action, post: post_1, user: Fabricate(:user), post_action_type_id: 1)++ json = PostSerializer.new(post_1, scope: Guardian.new, root: false).as_json++ expect(json[:reactions]).to contain_exactly(+ { id: "+1", type: :emoji, count: 1 },+ { id: "heart", type: :emoji, count: 1 },+ { id: "otter", type: :emoji, count: 2 },)end@@ -117,13 +123,11 @@ describe PostSerializer doit "renders the right custom reactions including custom emoji" dojson = PostSerializer.new(post_1, scope: Guardian.new(user_5), root: false).as_json- expect(json[:reactions]).to eq(- [- { id: "otter", type: :emoji, count: 2 },- { id: "+1", type: :emoji, count: 1 },- { id: "heart", type: :emoji, count: 1 },- { id: "some_custom_emoji", type: :emoji, count: 1 },- ],+ expect(json[:reactions]).to contain_exactly(+ { id: "+1", type: :emoji, count: 1 },+ { id: "heart", type: :emoji, count: 1 },+ { id: "otter", type: :emoji, count: 2 },+ { id: "some_custom_emoji", type: :emoji, count: 1 },)end@@ -133,12 +137,10 @@ describe PostSerializer dojson = PostSerializer.new(post_1.reload, scope: Guardian.new(user_5), root: false).as_json- expect(json[:reactions]).to eq(- [- { id: "otter", type: :emoji, count: 2 },- { id: "+1", type: :emoji, count: 1 },- { id: "heart", type: :emoji, count: 1 },- ],+ expect(json[:reactions]).to contain_exactly(+ { id: "+1", type: :emoji, count: 1 },+ { id: "heart", type: :emoji, count: 1 },+ { id: "otter", type: :emoji, count: 2 },)endend@@ -157,8 +159,9 @@ describe PostSerializer doit "merges the newly matching custom reaction into likes" dojson = PostSerializer.new(post_1, scope: Guardian.new(user_1), root: false).as_json- expect(json[:reactions]).to eq(- [{ id: "otter", type: :emoji, count: 3 }, { id: "+1", type: :emoji, count: 1 }],+ expect(json[:reactions]).to contain_exactly(+ { id: "otter", type: :emoji, count: 3 },+ { id: "+1", type: :emoji, count: 1 },)endend