DEV: Add post_action_users_list modifier for PostActionUsersController (#25740)

This commit adds another plugin modifier related to post
actions, similar to ae24e04a5e.

This will be used to exclude users who liked _and_ reacted to
the post, since now in discourse-reactions we make a Like when
a user reacts too. This will affect the display of the post footer.
This commit is contained in:
Martin Brennan 2024-02-20 09:48:09 +10:00 committed by GitHub
parent 09b1db8c3c
commit 3894ee6cb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 15 deletions

View File

@ -12,19 +12,9 @@ class PostActionUsersController < ApplicationController
page_size = fetch_limit_from_params(default: INDEX_LIMIT, max: INDEX_LIMIT) 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) # 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) 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_actions =
post post
.post_actions .post_actions
@ -34,20 +24,23 @@ class PostActionUsersController < ApplicationController
.order("post_actions.created_at ASC") .order("post_actions.created_at ASC")
.limit(page_size) .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) 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) post_actions = post_actions.where(user_id: current_user.id)
end end
action_type = PostActionType.types.key(post_action_type_id) action_type = PostActionType.types.key(post_action_type_id)
total_count = post["#{action_type}_count"].to_i total_count = post["#{action_type}_count"].to_i
post_actions = post_actions.to_a
data = { data = {
post_action_users: post_action_users:
serialize_data( serialize_data(
post_actions.to_a, post_actions,
PostActionUserSerializer, 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) render_json_dump(data)
end 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 end

View File

@ -138,4 +138,41 @@ RSpec.describe PostActionUsersController do
expect(users.length).to eq(0) expect(users.length).to eq(0)
expect(total).to be_nil expect(total).to be_nil
end 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 end