Skip to content

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

FIX: prevent shadow likes leaking to post reaction modal (#39373)

David Battersby4980c4148e36
diff --git a/plugins/discourse-reactions/app/controllers/discourse_reactions/custom_reactions_controller.rb b/plugins/discourse-reactions/app/controllers/discourse_reactions/custom_reactions_controller.rb
index 5f63d6a7902..387942fb093 100644
--- a/plugins/discourse-reactions/app/controllers/discourse_reactions/custom_reactions_controller.rb
+++ b/plugins/discourse-reactions/app/controllers/discourse_reactions/custom_reactions_controller.rb
@@ -11,14 +11,7 @@ class DiscourseReactions::CustomReactionsController < ApplicationController
post = fetch_post_from_params
reaction = params[:reaction]
- invalid_reaction =
- if SiteSetting.discourse_reactions_allow_any_emoji
- !Emoji.exists?(reaction)
- else
- DiscourseReactions::Reaction.valid_reactions.exclude?(params[:reaction])
- end
-
- return render_json_error(post) if invalid_reaction
+ return render_json_error(post) unless DiscourseReactions::Reaction.valid?(reaction)
begin
manager =
@@ -161,13 +154,14 @@ class DiscourseReactions::CustomReactionsController < ApplicationController
post = fetch_post_from_params
page = params[:page].to_i.clamp(0..)
limit = params[:limit].present? ? params[:limit].to_i.clamp(1, 50) : 30
- offset = page * limit
- main_reaction = DiscourseReactions::Reaction.main_reaction_id
- like_type = PostActionType::LIKE_POST_ACTION_ID
- reaction_filter = params[:reaction_value]
rows, total =
- fetch_reaction_rows(post, reaction_filter, main_reaction, like_type, limit, offset)
+ DiscourseReactions::PostReactionsQuery.call(
+ post: post,
+ reaction_filter: params[:reaction_value],
+ limit: limit,
+ offset: page * limit,
+ )
users =
rows.map do |row|
@@ -185,104 +179,6 @@ class DiscourseReactions::CustomReactionsController < ApplicationController
private
- def plain_likes_scope(post, like_type)
- PostAction.where(post_id: post.id).where(
- DiscourseReactions::PostActionExtension.filter_reaction_likes_sql,
- like: like_type,
- valid_reactions: DiscourseReactions::Reaction.valid_reactions.to_a,
- )
- end
-
- def fetch_reaction_rows(post, reaction_filter, main_reaction, like_type, limit, offset)
- if reaction_filter.present? && reaction_filter != main_reaction
- total =
- DiscourseReactions::ReactionUser
- .joins(:reaction)
- .where(
- post_id: post.id,
- discourse_reactions_reactions: {
- reaction_value: reaction_filter,
- },
- )
- .count
-
- rows =
- DB.query(
- <<~SQL,
- SELECT u.id, u.username, u.name, u.uploaded_avatar_id,
- dr.reaction_value AS reaction, drru.created_at
- FROM discourse_reactions_reaction_users drru
- INNER JOIN discourse_reactions_reactions dr ON dr.id = drru.reaction_id
- INNER JOIN users u ON u.id = drru.user_id
- WHERE drru.post_id = :post_id AND dr.reaction_value = :reaction_filter
- ORDER BY drru.created_at ASC
- LIMIT :limit OFFSET :offset
- SQL
- post_id: post.id,
- reaction_filter: reaction_filter,
- limit: limit,
- offset: offset,
- )
- elsif reaction_filter == main_reaction
- total = plain_likes_scope(post, like_type).count
-
- rows =
- DB.query(
- <<~SQL,
- SELECT u.id, u.username, u.name, u.uploaded_avatar_id,
- :main_reaction AS reaction, post_actions.created_at
- FROM post_actions
- INNER JOIN users u ON u.id = post_actions.user_id
- WHERE post_actions.post_id = :post_id
- AND (#{DiscourseReactions::PostActionExtension.filter_reaction_likes_sql})
- ORDER BY post_actions.created_at ASC
- LIMIT :limit OFFSET :offset
- SQL
- post_id: post.id,
- like: like_type,
- valid_reactions: DiscourseReactions::Reaction.valid_reactions.to_a,
- main_reaction: main_reaction,
- limit: limit,
- offset: offset,
- )
- else
- reaction_user_count = DiscourseReactions::ReactionUser.where(post_id: post.id).count
- plain_like_count = plain_likes_scope(post, like_type).count
- total = reaction_user_count + plain_like_count
-
- rows =
- DB.query(
- <<~SQL,
- SELECT * FROM (
- SELECT u.id, u.username, u.name, u.uploaded_avatar_id,
- dr.reaction_value AS reaction, drru.created_at
- FROM discourse_reactions_reaction_users drru
- INNER JOIN discourse_reactions_reactions dr ON dr.id = drru.reaction_id
- INNER JOIN users u ON u.id = drru.user_id
- WHERE drru.post_id = :post_id
- UNION ALL
- SELECT u.id, u.username, u.name, u.uploaded_avatar_id,
- :main_reaction AS reaction, post_actions.created_at
- FROM post_actions
- INNER JOIN users u ON u.id = post_actions.user_id
- WHERE post_actions.post_id = :post_id
- AND (#{DiscourseReactions::PostActionExtension.filter_reaction_likes_sql})
- ) combined
- ORDER BY created_at ASC
- LIMIT :limit OFFSET :offset
- SQL
- post_id: post.id,
- like: like_type,
- valid_reactions: DiscourseReactions::Reaction.valid_reactions.to_a,
- main_reaction: main_reaction,
- limit: limit,
- offset: offset,
- )
- end
-
- [rows, total]
- end
-
def post_serializer(post)
PostSerializer.new(post, scope: guardian, root: false)
end
diff --git a/plugins/discourse-reactions/app/models/discourse_reactions/reaction.rb b/plugins/discourse-reactions/app/models/discourse_reactions/reaction.rb
index ebe81905e60..652652a5a95 100644
--- a/plugins/discourse-reactions/app/models/discourse_reactions/reaction.rb
+++ b/plugins/discourse-reactions/app/models/discourse_reactions/reaction.rb
@@ -24,6 +24,16 @@ module DiscourseReactions
]
end
+ def self.valid?(reaction_value)
+ return false if reaction_value.blank?
+
+ if SiteSetting.discourse_reactions_allow_any_emoji
+ Emoji.exists?(reaction_value)
+ else
+ valid_reactions.include?(reaction_value)
+ end
+ end
+
def self.main_reaction_id
SiteSetting.discourse_reactions_reaction_for_like.gsub("-", "")
end
diff --git a/plugins/discourse-reactions/lib/discourse_reactions/post_action_extension.rb b/plugins/discourse-reactions/lib/discourse_reactions/post_action_extension.rb
index a8e2e2770a5..9180bdd5446 100644
--- a/plugins/discourse-reactions/lib/discourse_reactions/post_action_extension.rb
+++ b/plugins/discourse-reactions/lib/discourse_reactions/post_action_extension.rb
@@ -30,4 +30,20 @@ module DiscourseReactions::PostActionExtension
AND discourse_reactions_reactions.reaction_value IN (:valid_reactions)
SQL
end
+
+ # Suppresses shadow likes for any user who has a reaction on the post, regardless of
+ # whether the reaction's value is currently in discourse_reactions_enabled_reactions
+ # Required if `SiteSetting.discourse_reactions_allow_any_emoji` is enabled
+ def self.strict_filter_reaction_likes_sql
+ <<~SQL
+ post_actions.post_action_type_id = :like
+ AND post_actions.deleted_at IS NULL
+ AND NOT EXISTS (
+ SELECT 1
+ FROM discourse_reactions_reaction_users
+ WHERE discourse_reactions_reaction_users.user_id = post_actions.user_id
+ AND discourse_reactions_reaction_users.post_id = post_actions.post_id
+ )
+ SQL
+ end
end
diff --git a/plugins/discourse-reactions/lib/discourse_reactions/post_reactions_query.rb b/plugins/discourse-reactions/lib/discourse_reactions/post_reactions_query.rb
new file mode 100644
index 00000000000..372a5d61eca
--- /dev/null
+++ b/plugins/discourse-reactions/lib/discourse_reactions/post_reactions_query.rb
@@ -0,0 +1,124 @@
+# frozen_string_literal: true
+
+module DiscourseReactions
+ class PostReactionsQuery
+ def self.call(post:, reaction_filter: nil, limit: 30, offset: 0)
+ query = new(post: post, reaction_filter: reaction_filter, limit: limit, offset: offset)
+ [query.rows, query.total]
+ end
+
+ def initialize(post:, reaction_filter: nil, limit: 30, offset: 0)
+ @post = post
+ @reaction_filter = reaction_filter
+ @limit = limit
+ @offset = offset
+ end
+
+ def rows
+ @rows ||=
+ if specific_reaction?
+ DB.query(<<~SQL, **bindings, reaction_filter: reaction_filter)
+ #{reactions_select_sql}
+ AND dr.reaction_value = :reaction_filter
+ ORDER BY drru.created_at ASC
+ LIMIT :limit OFFSET :offset
+ SQL
+ elsif main_reaction_filter?
+ DB.query(<<~SQL, **bindings)
+ #{plain_likes_select_sql}
+ ORDER BY post_actions.created_at ASC
+ LIMIT :limit OFFSET :offset
+ SQL
+ else
+ DB.query(<<~SQL, **bindings)
+ SELECT * FROM (
+ #{reactions_select_sql}
+ UNION ALL
+ #{plain_likes_select_sql}
+ ) combined
+ ORDER BY created_at ASC
+ LIMIT :limit OFFSET :offset
+ SQL
+ end
+ end
+
+ def total
+ @total ||=
+ if specific_reaction?
+ ReactionUser
+ .joins(:reaction)
+ .where(
+ post_id: post.id,
+ discourse_reactions_reactions: {
+ reaction_value: reaction_filter,
+ },
+ )
+ .count
+ elsif main_reaction_filter?
+ plain_likes_scope.count
+ else
+ ReactionUser.where(post_id: post.id).count + plain_likes_scope.count
+ end
+ end
+
+ private
+
+ attr_reader :post, :reaction_filter, :limit, :offset
+
+ def main_reaction
+ @main_reaction ||= Reaction.main_reaction_id
+ end
+
+ def like_type
+ PostActionType::LIKE_POST_ACTION_ID
+ end
+
+ def shadow_like_filter
+ PostActionExtension.strict_filter_reaction_likes_sql
+ end
+
+ def specific_reaction?
+ reaction_filter.present? && reaction_filter != main_reaction
+ end
+
+ def main_reaction_filter?
+ reaction_filter == main_reaction
+ end
+
+ def bindings
+ {
+ post_id: post.id,
+ like: like_type,
+ main_reaction: main_reaction,
+ limit: limit,
+ offset: offset,
+ }
+ end
+
+ def plain_likes_scope
+ PostAction.where(post_id: post.id).where(shadow_like_filter, like: like_type)
+ end
+
+ def reactions_select_sql
+ <<~SQL
+ SELECT u.id, u.username, u.name, u.uploaded_avatar_id,
+ dr.reaction_value AS reaction, drru.created_at
+ FROM discourse_reactions_reaction_users drru
+ INNER JOIN discourse_reactions_reactions dr ON dr.id = drru.reaction_id
+ INNER JOIN users u ON u.id = drru.user_id
+ WHERE drru.post_id = :post_id
+ SQL
+ end
+
+ def plain_likes_select_sql
+ <<~SQL
+ SELECT u.id, u.username, u.name, u.uploaded_avatar_id,
+ :main_reaction AS reaction, post_actions.created_at
+ FROM post_actions
+ INNER JOIN users u ON u.id = post_actions.user_id
+ WHERE post_actions.post_id = :post_id
+ AND (#{shadow_like_filter})
+ SQL
+ end
+ end
+end
diff --git a/plugins/discourse-reactions/plugin.rb b/plugins/discourse-reactions/plugin.rb
index 9aa6a0df735..3fed861d898 100644
--- a/plugins/discourse-reactions/plugin.rb
+++ b/plugins/discourse-reactions/plugin.rb
@@ -46,6 +46,7 @@ after_initialize do
lib/discourse_reactions/topic_view_serializer_extension.rb
lib/discourse_reactions/topic_view_posts_serializer_extension.rb
lib/discourse_reactions/migration_report.rb
+ lib/discourse_reactions/post_reactions_query.rb
app/jobs/regular/discourse_reactions/like_synchronizer.rb
app/jobs/scheduled/discourse_reactions/scheduled_like_synchronizer.rb
].each { |path| require_relative path }
diff --git a/plugins/discourse-reactions/spec/requests/custom_reactions_controller_spec.rb b/plugins/discourse-reactions/spec/requests/custom_reactions_controller_spec.rb
index b58b847b120..d1727395a0b 100644
--- a/plugins/discourse-reactions/spec/requests/custom_reactions_controller_spec.rb
+++ b/plugins/discourse-reactions/spec/requests/custom_reactions_controller_spec.rb
@@ -491,6 +491,17 @@ describe DiscourseReactions::CustomReactionsController do
get "/discourse-reactions/posts/#{private_post.id}/reactions-users-list.json"
expect(response.status).to eq(200)
end
+
+ it "does not duplicate a user whose reaction value is no longer in enabled_reactions" do
+ SiteSetting.discourse_reactions_enabled_reactions = "laughing|open_mouth"
+
+ get "/discourse-reactions/posts/#{post_2.id}/reactions-users-list.json"
+
+ expect(response.status).to eq(200)
+ user_4_rows = response.parsed_body["users"].select { |u| u["username"] == user_4.username }
+ expect(user_4_rows.size).to eq(1)
+ expect(user_4_rows.first["reaction"]).to eq("hugs")
+ end
end
describe "positive notifications" do