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? def can_chat?
return false if anonymous? 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 end
def can_direct_message? def can_direct_message?
@ -140,7 +140,7 @@ module Chat
def can_flag_chat_messages? def can_flag_chat_messages?
return false if @user.silenced? 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) @user.in_any_groups?(SiteSetting.chat_message_flag_allowed_groups_map)
end end

View File

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

View File

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