FIX: correctly shows as disabled a user who can't chat (#26010)

Prior to this fix we were checking if user was not part of a group which allows to chat, but we were not checking if this user was part of groups who can use direct messages.
This commit is contained in:
Joffrey JAFFEUX 2024-03-05 09:13:42 +01:00 committed by GitHub
parent 00e76efb7c
commit 27407a25b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 49 additions and 9 deletions

View File

@ -5,7 +5,7 @@ module Chat
attributes :can_chat, :has_chat_enabled attributes :can_chat, :has_chat_enabled
def can_chat def can_chat
SiteSetting.chat_enabled && scope.can_chat? SiteSetting.chat_enabled && object.guardian.can_chat? && object.guardian.can_direct_message?
end end
def has_chat_enabled def has_chat_enabled

View File

@ -199,7 +199,7 @@ module Chat
.where(id: scoped_channels) .where(id: scoped_channels)
.includes( .includes(
last_message: [:uploads], last_message: [:uploads],
chatable: [{ direct_message_users: [user: :user_option] }, :users], chatable: [{ direct_message_users: [user: %i[user_option group_users]] }, :users],
) )
.joins( .joins(
"LEFT JOIN chat_messages last_message ON last_message.id = chat_channels.last_message_id", "LEFT JOIN chat_messages last_message ON last_message.id = chat_channels.last_message_id",

View File

@ -25,7 +25,7 @@ module Chat
end end
def can_create_direct_message? def can_create_direct_message?
is_staff? || @user.in_any_groups?(SiteSetting.direct_message_enabled_groups_map) is_staff? || can_direct_message?
end end
def hidden_tag_names def hidden_tag_names

View File

@ -4,6 +4,7 @@ RSpec.describe "Outgoing chat webhooks" do
before do before do
SiteSetting.chat_enabled = true SiteSetting.chat_enabled = true
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone]
end end
describe "chat messages" do describe "chat messages" do

View File

@ -1,12 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe Chat::ChatableUserSerializer do RSpec.describe Chat::ChatableUserSerializer do
fab!(:user) { Fabricate(:user) } fab!(:user)
subject(:serializer) { described_class.new(user, scope: Guardian.new(user), root: false) } subject(:serializer) { described_class.new(user, scope: Guardian.new(user), root: false) }
it "serializes a user" do before do
json = serializer.as_json SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone]
end
expect(json).to eq( it "serializes a user" do
expect(serializer.as_json).to eq(
{ {
id: user.id, id: user.id,
username: user.username, username: user.username,
@ -14,9 +17,41 @@ RSpec.describe Chat::ChatableUserSerializer do
avatar_template: user.avatar_template, avatar_template: user.avatar_template,
custom_fields: { custom_fields: {
}, },
can_chat: false, can_chat: true,
has_chat_enabled: false, has_chat_enabled: true,
}, },
) )
end end
context "when chat is disabled" do
before { SiteSetting.chat_enabled = false }
it "can't chat" do
expect(serializer.as_json[:can_chat]).to eq(false)
end
end
context "when user is not allowed to chat" do
before { SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_4] }
it "can't chat" do
expect(serializer.as_json[:can_chat]).to eq(false)
end
end
context "when user has chat disabled" do
before { user.user_option.update!(chat_enabled: false) }
it "has chat disabled" do
expect(serializer.as_json[:has_chat_enabled]).to eq(false)
end
end
context "when user can't use direct messages" do
before { SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:trust_level_4] }
it "can't chat" do
expect(serializer.as_json[:can_chat]).to eq(false)
end
end
end end

View File

@ -7,6 +7,8 @@ RSpec.describe "Channel - Info - Members page", type: :system do
fab!(:channel_1) { Fabricate(:category_channel) } fab!(:channel_1) { Fabricate(:category_channel) }
before do before do
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone]
chat_system_bootstrap chat_system_bootstrap
sign_in(current_user) sign_in(current_user)
end end

View File

@ -6,6 +6,8 @@ RSpec.describe "Flag message", type: :system do
fab!(:current_user) { Fabricate(:user) } fab!(:current_user) { Fabricate(:user) }
before do before do
SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone]
SiteSetting.chat_max_direct_message_users = 3 SiteSetting.chat_max_direct_message_users = 3
chat_system_bootstrap chat_system_bootstrap
sign_in(current_user) sign_in(current_user)