From 98f74304801c94eec7a09ad2945ded9e1a500f01 Mon Sep 17 00:00:00 2001 From: pangbo13 <51732678+pangbo13@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:20:21 +0800 Subject: [PATCH] fix: allow staff and `direct message enabled groups` to create personal chats --- .../serializers/chat/chatable_user_serializer.rb | 2 +- .../chat/chatable_user_serializer_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/chat/app/serializers/chat/chatable_user_serializer.rb b/plugins/chat/app/serializers/chat/chatable_user_serializer.rb index 73e174fef40..d21c973e097 100644 --- a/plugins/chat/app/serializers/chat/chatable_user_serializer.rb +++ b/plugins/chat/app/serializers/chat/chatable_user_serializer.rb @@ -5,7 +5,7 @@ module Chat attributes :can_chat, :has_chat_enabled def can_chat - SiteSetting.chat_enabled && object.guardian.can_chat? && object.guardian.can_direct_message? + SiteSetting.chat_enabled && object.guardian.can_chat? && scope.can_create_direct_message? end def has_chat_enabled diff --git a/plugins/chat/spec/serializer/chat/chatable_user_serializer_spec.rb b/plugins/chat/spec/serializer/chat/chatable_user_serializer_spec.rb index cacd5c3a592..fe3b3250bc0 100644 --- a/plugins/chat/spec/serializer/chat/chatable_user_serializer_spec.rb +++ b/plugins/chat/spec/serializer/chat/chatable_user_serializer_spec.rb @@ -54,4 +54,19 @@ RSpec.describe Chat::ChatableUserSerializer do expect(serializer.as_json[:can_chat]).to eq(false) end end + + context "when staff creates direct messages" do + let(:admin) { Fabricate(:admin) } + subject(:serializer) { described_class.new(user, scope: Guardian.new(admin), root: false) } + before { SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:trust_level_4] } + + it "can chat" do + expect(serializer.as_json[:can_chat]).to eq(true) + end + + it "can't chat if user has chat disabled" do + user.user_option.update!(chat_enabled: false) + expect(serializer.as_json[:has_chat_enabled]).to eq(false) + end + end end