FIX: Handle actor not having preferences in UserCommScreener (#17790)

Followup to d66115d918

* Makes sure the `actor_preferences` all initialize with an empty array instead of nil if there are no preferences e.g. the actor is not ignoring anyone
* If the actor has disabled all PMs make `actor_disallowing_pms?` always return true
This commit is contained in:
Martin Brennan 2022-08-04 10:16:54 +10:00 committed by GitHub
parent 28968d9977
commit 7cab189b1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View File

@ -172,6 +172,7 @@ class UserCommScreener
def actor_disallowing_pms?(user_id)
validate_user_id!(user_id)
return true if actor_disallowing_all_pms?
return false if !acting_user.user_option.enable_allowed_pm_users
actor_preferences[:disallowed_pms_from].include?(user_id)
end
@ -244,10 +245,16 @@ class UserCommScreener
hash[pref.preference_type] << pref.target_user_id
hash
end
disallowed_pms_from = \
if acting_user.user_option.enable_allowed_pm_users
(user_ids_by_preference_type["disallowed_pm"] || [])
else
[]
end
{
muting: user_ids_by_preference_type["muted"],
ignoring: user_ids_by_preference_type["ignored"],
disallowed_pms_from: user_ids_by_preference_type["disallowed_pm"]
muting: user_ids_by_preference_type["muted"] || [],
ignoring: user_ids_by_preference_type["ignored"] || [],
disallowed_pms_from: disallowed_pms_from
}
end
end

View File

@ -195,6 +195,23 @@ RSpec.describe UserCommScreener do
target_user1.id, target_user2.id, target_user3.id, target_user5.id
])
end
it "does not include users the actor is disallowing PMs from if they have not set enable_allowed_pm_users" do
expect(subject.actor_preventing_communication).to match_array([
target_user1.id, target_user2.id
])
end
describe "when the actor has no preferences" do
before do
muted_user.destroy
ignored_user.destroy
end
it "returns an empty array and does not error" do
expect(subject.actor_preventing_communication).to match_array([])
end
end
end
describe "#actor_allowing_communication" do
@ -202,6 +219,19 @@ RSpec.describe UserCommScreener do
acting_user.user_option.update!(enable_allowed_pm_users: true)
expect(subject.actor_allowing_communication).to match_array([target_user4.id])
end
describe "when the actor has no preferences" do
before do
muted_user.destroy
ignored_user.destroy
end
it "returns an array of the target users and does not error" do
expect(subject.actor_allowing_communication).to match_array([
target_user1.id, target_user2.id, target_user3.id, target_user4.id, target_user5.id
])
end
end
end
describe "#actor_ignoring?" do
@ -233,6 +263,12 @@ RSpec.describe UserCommScreener do
expect(subject.actor_disallowing_pms?(target_user1.id)).to eq(false)
end
it "returns true if the actor has disallowed all PMs" do
acting_user.user_option.update!(allow_private_messages: false)
expect(subject.actor_disallowing_pms?(target_user3.id)).to eq(true)
expect(subject.actor_disallowing_pms?(target_user1.id)).to eq(true)
end
it "raises a NotFound error if the user_id passed in is not part of the target users" do
expect { subject.actor_disallowing_pms?(other_user.id) }.to raise_error(Discourse::NotFound)
end