Skip to content

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

DEV: Add post_action_users_list modifier for PostActionUsersController (#25740)

Martin Brennan3894ee6cb69d
diff --git a/app/controllers/post_action_users_controller.rb b/app/controllers/post_action_users_controller.rb
index 36e87734809..9da3155a911 100644
--- a/app/controllers/post_action_users_controller.rb
+++ b/app/controllers/post_action_users_controller.rb
@@ -12,19 +12,9 @@ class PostActionUsersController < ApplicationController
page_size = fetch_limit_from_params(default: INDEX_LIMIT, max: INDEX_LIMIT)
# Find the post, and then determine if they can see the post (if deleted)
- post = Post.with_deleted.where(id: params[:id].to_i).first
+ post = Post.with_deleted.find_by(id: params[:id].to_i)
guardian.ensure_can_see!(post)
- unknown_user_ids = Set.new
- if current_user.present?
- result = DB.query_single(<<~SQL, user_id: current_user.id)
- SELECT mu.muted_user_id AS id FROM muted_users AS mu WHERE mu.user_id = :user_id
- UNION
- SELECT iu.ignored_user_id AS id FROM ignored_users AS iu WHERE iu.user_id = :user_id
- SQL
- unknown_user_ids.merge(result)
- end
-
post_actions =
post
.post_actions
@@ -34,20 +24,23 @@ class PostActionUsersController < ApplicationController
.order("post_actions.created_at ASC")
.limit(page_size)
+ post_actions =
+ DiscoursePluginRegistry.apply_modifier(:post_action_users_list, post_actions, post)
+
if !guardian.can_see_post_actors?(post.topic, post_action_type_id)
- raise Discourse::InvalidAccess unless current_user
+ raise Discourse::InvalidAccess if current_user.blank?
post_actions = post_actions.where(user_id: current_user.id)
end
action_type = PostActionType.types.key(post_action_type_id)
total_count = post["#{action_type}_count"].to_i
-
+ post_actions = post_actions.to_a
data = {
post_action_users:
serialize_data(
- post_actions.to_a,
+ post_actions,
PostActionUserSerializer,
- unknown_user_ids: unknown_user_ids,
+ unknown_user_ids: current_user_muting_or_ignoring_users(post_actions.map(&:user_id)),
),
}
@@ -55,4 +48,14 @@ class PostActionUsersController < ApplicationController
render_json_dump(data)
end
+
+ private
+
+ def current_user_muting_or_ignoring_users(user_ids)
+ return [] if current_user.blank?
+ UserCommScreener.new(
+ acting_user: current_user,
+ target_user_ids: user_ids,
+ ).actor_preventing_communication
+ end
end
diff --git a/spec/requests/post_action_users_controller_spec.rb b/spec/requests/post_action_users_controller_spec.rb
index 079cfc24bd9..fc3f28683dc 100644
--- a/spec/requests/post_action_users_controller_spec.rb
+++ b/spec/requests/post_action_users_controller_spec.rb
@@ -138,4 +138,41 @@ RSpec.describe PostActionUsersController do
expect(users.length).to eq(0)
expect(total).to be_nil
end
+
+ describe "when a plugin registers the :post_action_users_list modifier" do
+ before do
+ @post_action_1 = PostActionCreator.like(Fabricate(:user), post).post_action
+ @post_action_2 = PostActionCreator.like(Fabricate(:user), post).post_action
+ end
+
+ after { DiscoursePluginRegistry.clear_modifiers! }
+
+ it "allows the plugin to modify the post action query" do
+ excluded_post_action_ids = [@post_action_1.id]
+ Plugin::Instance
+ .new
+ .register_modifier(:post_action_users_list) do |query, modifier_post|
+ expect(modifier_post.id).to eq(post.id)
+ query.where("post_actions.id NOT IN (?)", excluded_post_action_ids)
+ end
+
+ get "/post_action_users.json",
+ params: {
+ id: post.id,
+ post_action_type_id: PostActionType.types[:like],
+ }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["post_action_users"].count).to eq(1)
+
+ DiscoursePluginRegistry.clear_modifiers!
+
+ get "/post_action_users.json",
+ params: {
+ id: post.id,
+ post_action_type_id: PostActionType.types[:like],
+ }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["post_action_users"].count).to eq(2)
+ end
+ end
end