Skip to content

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

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

Martin Brennan7cab189b1ed4
diff --git a/lib/user_comm_screener.rb b/lib/user_comm_screener.rb
index 5c3e9433bea..7d575f80724 100644
--- a/lib/user_comm_screener.rb
+++ b/lib/user_comm_screener.rb
@@ -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
diff --git a/spec/lib/user_comm_screener_spec.rb b/spec/lib/user_comm_screener_spec.rb
index 474a34f7b54..8ce9eb74725 100644
--- a/spec/lib/user_comm_screener_spec.rb
+++ b/spec/lib/user_comm_screener_spec.rb
@@ -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