mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 20:24:05 -06:00
fa5f3e228c
The idea behind this refactor is to centralise all of the user ignoring / muting / disallow PM checks in a single place, so they can be used consistently in core as well as for plugins like chat, while improving the main bulk of the checks to run in a single fast non-AR query. Also fixed up the invite error when someone is muting/ignoring the user that is trying to invite them to the topic.
154 lines
6.6 KiB
Ruby
154 lines
6.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
describe UserCommScreener do
|
|
fab!(:target_user1) { Fabricate(:user, username: "bobscreen") }
|
|
fab!(:target_user2) { Fabricate(:user, username: "hughscreen") }
|
|
fab!(:target_user3) do
|
|
user = Fabricate(:user, username: "alicescreen")
|
|
user.user_option.update(allow_private_messages: false)
|
|
user
|
|
end
|
|
fab!(:target_user4) { Fabricate(:user, username: "janescreen") }
|
|
fab!(:target_user5) { Fabricate(:user, username: "maryscreen") }
|
|
|
|
subject do
|
|
described_class.new(
|
|
acting_user: acting_user, target_usernames: ["bobscreen", "hughscreen", "alicescreen", "janescreen", "maryscreen"]
|
|
)
|
|
end
|
|
|
|
it "allows initializing the class with both an acting_user_id and an acting_user" do
|
|
acting_user = Fabricate(:user)
|
|
screener = described_class.new(acting_user: acting_user, target_usernames: ["bobscreen"])
|
|
expect(screener.allowing_actor_communication).to eq(["bobscreen"])
|
|
screener = described_class.new(acting_user_id: acting_user.id, target_usernames: ["bobscreen"])
|
|
expect(screener.allowing_actor_communication).to eq(["bobscreen"])
|
|
end
|
|
|
|
it "makes sure to lowercase target usernames" do
|
|
acting_user = Fabricate(:user)
|
|
screener = described_class.new(acting_user: acting_user, target_usernames: ["BoBscrEEN", "HUghSCreen"])
|
|
expect(screener.allowing_actor_communication).to eq(["bobscreen", "hughscreen"])
|
|
end
|
|
|
|
context "when the actor is not staff" do
|
|
fab!(:acting_user) { Fabricate(:user) }
|
|
fab!(:muted_user) { Fabricate(:muted_user, user: target_user1, muted_user: acting_user) }
|
|
fab!(:ignored_user) { Fabricate(:ignored_user, user: target_user2, ignored_user: acting_user, expiring_at: 2.days.from_now) }
|
|
|
|
describe "#allowing_actor_communication" do
|
|
it "returns the usernames of people not ignoring, muting, or disallowing PMs from the actor" do
|
|
expect(subject.allowing_actor_communication).to match_array(["janescreen", "maryscreen"])
|
|
end
|
|
end
|
|
|
|
describe "#preventing_actor_communication" do
|
|
it "returns the usernames of people ignoring, muting, or disallowing PMs from the actor" do
|
|
expect(subject.preventing_actor_communication).to match_array(["bobscreen", "hughscreen", "alicescreen"])
|
|
end
|
|
end
|
|
|
|
describe "#ignoring_or_muting_actor?" do
|
|
it "does not raise an error when looking for a user who has no communication preferences" do
|
|
expect { subject.ignoring_or_muting_actor?(target_user5.username) }.not_to raise_error
|
|
end
|
|
|
|
it "returns true for a user muting the actor" do
|
|
expect(subject.ignoring_or_muting_actor?(target_user1.username)).to eq(true)
|
|
end
|
|
|
|
it "returns true for a user ignoring the actor" do
|
|
expect(subject.ignoring_or_muting_actor?(target_user2.username)).to eq(true)
|
|
end
|
|
|
|
it "returns false for a user neither ignoring or muting the actor" do
|
|
expect(subject.ignoring_or_muting_actor?(target_user3.username)).to eq(false)
|
|
end
|
|
end
|
|
|
|
describe "#disallowing_pms_from_actor?" do
|
|
it "returns true for a user disallowing all PMs" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user3.username)).to eq(true)
|
|
end
|
|
|
|
it "returns true for a user allowing only PMs for certain users but not the actor" do
|
|
target_user4.user_option.update!(enable_allowed_pm_users: true)
|
|
expect(subject.disallowing_pms_from_actor?(target_user4.username)).to eq(true)
|
|
end
|
|
|
|
it "returns false for a user allowing only PMs for certain users which the actor allowed" do
|
|
target_user4.user_option.update!(enable_allowed_pm_users: true)
|
|
AllowedPmUser.create!(user: target_user4, allowed_pm_user: acting_user)
|
|
expect(subject.disallowing_pms_from_actor?(target_user4.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user not disallowing PMs or muting or ignoring" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user5.username)).to eq(false)
|
|
end
|
|
|
|
it "returns true for a user not disallowing PMs but still ignoring" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user1.username)).to eq(true)
|
|
end
|
|
|
|
it "returns true for a user not disallowing PMs but still muting" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user2.username)).to eq(true)
|
|
end
|
|
end
|
|
end
|
|
|
|
context "when the actor is staff" do
|
|
fab!(:acting_user) { Fabricate(:admin) }
|
|
fab!(:muted_user) { Fabricate(:muted_user, user: target_user1, muted_user: acting_user) }
|
|
fab!(:ignored_user) { Fabricate(:ignored_user, user: target_user1, ignored_user: acting_user, expiring_at: 2.days.from_now) }
|
|
|
|
describe "#allowing_actor_communication" do
|
|
it "returns all usernames since staff can communicate with anyone" do
|
|
expect(subject.allowing_actor_communication).to match_array(["bobscreen", "hughscreen", "alicescreen", "janescreen", "maryscreen"])
|
|
end
|
|
end
|
|
|
|
describe "#preventing_actor_communication" do
|
|
it "does not return any usernames since no users can prevent staff communicating with them" do
|
|
expect(subject.preventing_actor_communication).to eq([])
|
|
end
|
|
end
|
|
|
|
describe "#ignoring_or_muting_actor?" do
|
|
it "returns false for a user muting the staff" do
|
|
expect(subject.ignoring_or_muting_actor?(target_user1.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user ignoring the staff actor" do
|
|
expect(subject.ignoring_or_muting_actor?(target_user2.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user neither ignoring or muting the actor" do
|
|
expect(subject.ignoring_or_muting_actor?(target_user3.username)).to eq(false)
|
|
end
|
|
end
|
|
|
|
describe "#disallowing_pms_from_actor?" do
|
|
it "returns false for a user disallowing all PMs" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user3.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user allowing only PMs for certain users but not the actor" do
|
|
target_user4.user_option.update!(enable_allowed_pm_users: true)
|
|
expect(subject.disallowing_pms_from_actor?(target_user4.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user not disallowing PMs or muting or ignoring" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user5.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user not disallowing PMs but still ignoring" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user1.username)).to eq(false)
|
|
end
|
|
|
|
it "returns false for a user not disallowing PMs but still muting" do
|
|
expect(subject.disallowing_pms_from_actor?(target_user2.username)).to eq(false)
|
|
end
|
|
end
|
|
end
|
|
end
|