DEV: bots are always allowed to chat (#26948)

Bots should be allowed to chat regardless of their groups, just like staff. It makes configuring bots to work in chat much easier.
This commit is contained in:
Joffrey JAFFEUX 2024-05-09 12:05:31 +02:00 committed by GitHub
parent 20e049bcbc
commit 72aed56daf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 30 deletions

View File

@ -13,7 +13,7 @@ module Chat
def can_chat?
return false if anonymous?
@user.staff? || @user.in_any_groups?(Chat.allowed_group_ids)
is_staff? || @user.bot? || @user.in_any_groups?(Chat.allowed_group_ids)
end
def can_direct_message?
@ -140,7 +140,7 @@ module Chat
def can_flag_chat_messages?
return false if @user.silenced?
return true if @user.staff?
return true if is_staff?
@user.in_any_groups?(SiteSetting.chat_message_flag_allowed_groups_map)
end

View File

@ -12,28 +12,52 @@ RSpec.describe Chat::GuardianExtensions do
before { SiteSetting.chat_allowed_groups = [chatters] }
it "cannot chat if the user is not in the Chat.allowed_group_ids" do
SiteSetting.chat_allowed_groups = ""
expect(guardian.can_chat?).to eq(false)
end
describe "#can_chat?" do
context "when the user is not in allowed to chat" do
before { SiteSetting.chat_allowed_groups = "" }
it "staff can always chat regardless of chat_allowed_grups" do
SiteSetting.chat_allowed_groups = ""
expect(staff_guardian.can_chat?).to eq(true)
end
it "cannot chat" do
expect(guardian.can_chat?).to eq(false)
end
it "allows TL1 to chat by default and by extension higher trust levels" do
expect(guardian.can_chat?).to eq(true)
user.change_trust_level!(TrustLevel[3])
expect(guardian.can_chat?).to eq(true)
end
context "when the user is a bot" do
let(:guardian) { Discourse.system_user.guardian }
it "allows user in specific group to chat" do
SiteSetting.chat_allowed_groups = chat_group.id
expect(guardian.can_chat?).to eq(false)
chat_group.add(user)
user.reload
expect(guardian.can_chat?).to eq(true)
it "can chat" do
expect(guardian.can_chat?).to eq(true)
end
end
context "when user is staff" do
let(:guardian) { staff_guardian }
it "can chat" do
expect(guardian.can_chat?).to eq(true)
end
end
end
context "when user is anonymous" do
let(:guardian) { Guardian.new }
it "cannot chat" do
expect(guardian.can_chat?).to eq(false)
end
end
it "allows TL1 to chat by default and by extension higher trust levels" do
expect(guardian.can_chat?).to eq(true)
user.change_trust_level!(TrustLevel[3])
expect(guardian.can_chat?).to eq(true)
end
it "allows user in specific group to chat" do
SiteSetting.chat_allowed_groups = chat_group.id
expect(guardian.can_chat?).to eq(false)
chat_group.add(user)
user.reload
expect(guardian.can_chat?).to eq(true)
end
end
describe "chat channel" do

View File

@ -3,9 +3,9 @@
describe Chat::DirectMessageSerializer do
describe "#user" do
it "returns you when there are two of us" do
me = Fabricate.build(:user)
you = Fabricate.build(:user)
direct_message = Fabricate.build(:direct_message, users: [me, you])
me = Fabricate(:user)
you = Fabricate(:user)
direct_message = Fabricate(:direct_message, users: [me, you])
serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false)
json = serializer.as_json
@ -14,10 +14,10 @@ describe Chat::DirectMessageSerializer do
end
it "returns you both if there are three of us" do
me = Fabricate.build(:user)
you = Fabricate.build(:user)
other_you = Fabricate.build(:user)
direct_message = Fabricate.build(:direct_message, users: [me, you, other_you])
me = Fabricate(:user)
you = Fabricate(:user)
other_you = Fabricate(:user)
direct_message = Fabricate(:direct_message, users: [me, you, other_you])
serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false)
json = serializer.as_json
@ -28,8 +28,8 @@ describe Chat::DirectMessageSerializer do
end
it "returns me if there is only me" do
me = Fabricate.build(:user)
direct_message = Fabricate.build(:direct_message, users: [me])
me = Fabricate(:user)
direct_message = Fabricate(:direct_message, users: [me])
serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false)
json = serializer.as_json