FIX: UserCommScreener filter acting user ID from target user IDs (#17702)

Fixes edge case from fa5f3e228c.
In case the acting user is sent in with the target_user_ids,
we do not need to load those preferences, because even if the
acting user is preventing PMs or muting etc they need to always be able to
send themselves messages.
This commit is contained in:
Martin Brennan 2022-07-28 13:04:24 +10:00 committed by GitHub
parent 493d437e79
commit f4b45df83f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -99,6 +99,7 @@ class UserCommScreener
def initialize(acting_user: nil, acting_user_id: nil, target_user_ids:) def initialize(acting_user: nil, acting_user_id: nil, target_user_ids:)
raise ArgumentError if acting_user.blank? && acting_user_id.blank? raise ArgumentError if acting_user.blank? && acting_user_id.blank?
@acting_user = acting_user.present? ? acting_user : User.find(acting_user_id) @acting_user = acting_user.present? ? acting_user : User.find(acting_user_id)
target_user_ids = Array.wrap(target_user_ids) - [@acting_user.id]
@target_users = User.where(id: target_user_ids).pluck(:id, :username).to_h @target_users = User.where(id: target_user_ids).pluck(:id, :username).to_h
@preferences = load_preference_map @preferences = load_preference_map
end end

View File

@ -31,6 +31,12 @@ RSpec.describe UserCommScreener do
expect(screener.allowing_actor_communication).to eq([target_user1.id]) expect(screener.allowing_actor_communication).to eq([target_user1.id])
end end
it "filters out the acting user from target_user_ids" do
acting_user = Fabricate(:user)
screener = described_class.new(acting_user: acting_user, target_user_ids: [target_user1.id, acting_user.id])
expect(screener.allowing_actor_communication).to eq([target_user1.id])
end
context "when the actor is not staff" do context "when the actor is not staff" do
fab!(:acting_user) { Fabricate(:user) } fab!(:acting_user) { Fabricate(:user) }
fab!(:muted_user) { Fabricate(:muted_user, user: target_user1, muted_user: acting_user) } fab!(:muted_user) { Fabricate(:muted_user, user: target_user1, muted_user: acting_user) }