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,14 +12,37 @@ 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
before { SiteSetting.chat_allowed_groups = "" }
it "cannot chat" do
expect(guardian.can_chat?).to eq(false) expect(guardian.can_chat?).to eq(false)
end end
it "staff can always chat regardless of chat_allowed_grups" do context "when the user is a bot" do
SiteSetting.chat_allowed_groups = "" let(:guardian) { Discourse.system_user.guardian }
expect(staff_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 end
it "allows TL1 to chat by default and by extension higher trust levels" do it "allows TL1 to chat by default and by extension higher trust levels" do
@ -35,6 +58,7 @@ RSpec.describe Chat::GuardianExtensions do
user.reload user.reload
expect(guardian.can_chat?).to eq(true) expect(guardian.can_chat?).to eq(true)
end end
end
describe "chat channel" do describe "chat channel" do
it "only staff can create channels" do it "only staff can create channels" 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