From 2a37be701f1930b7a6b30d524f04c4075f112d22 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Wed, 20 Mar 2024 16:24:34 +0800 Subject: [PATCH] FIX: hide chat btn from user card when chat disabled (#26237) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The issue: When the current user disables chat from within user preferences, the chat button still appears when clicking another user’s profile picture to open the user card. This is also the case when the current user has chat enabled but the target user has disabled chat. After this change: - when a user disables chat in preferences, the chat button should not be displayed when opening a user card or visiting profiles of other users. - when chat is enabled in preferences but another user disables chat, the chat button should not appear on their user card or profile --- plugins/chat/plugin.rb | 7 ++++--- plugins/chat/spec/plugin_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb index ec39914dd7f..ab8b6fdc13c 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -147,9 +147,10 @@ after_initialize do add_to_serializer(:user_card, :can_chat_user) do return false if !SiteSetting.chat_enabled - return false if scope.user.blank? + return false if scope.user.blank? || scope.user.id == object.id + return false if !scope.user.user_option.chat_enabled || !object.user_option.chat_enabled - scope.user.id != object.id && scope.can_chat? && Guardian.new(object).can_chat? + scope.can_direct_message? && Guardian.new(object).can_chat? end add_to_serializer( @@ -166,7 +167,7 @@ after_initialize do :can_direct_message, include_condition: -> do return @can_direct_message if defined?(@can_direct_message) - @can_direct_message = SiteSetting.chat_enabled && scope.can_direct_message? + @can_direct_message = include_has_chat_enabled? && scope.can_direct_message? end, ) { true } diff --git a/plugins/chat/spec/plugin_spec.rb b/plugins/chat/spec/plugin_spec.rb index 515979e4df6..8a011e82321 100644 --- a/plugins/chat/spec/plugin_spec.rb +++ b/plugins/chat/spec/plugin_spec.rb @@ -82,6 +82,7 @@ describe Chat do it "returns true if the target user and the guardian user is in the Chat.allowed_group_ids" do SiteSetting.chat_allowed_groups = group.id + SiteSetting.direct_message_enabled_groups = group.id GroupUser.create(user: target_user, group: group) GroupUser.create(user: user, group: group) expect(serializer.can_chat_user).to eq(true) @@ -114,6 +115,34 @@ describe Chat do expect(serializer.can_chat_user).to eq(false) end end + + context "when both users are in Chat.allowed_group_ids" do + before do + SiteSetting.chat_allowed_groups = group.id + SiteSetting.direct_message_enabled_groups = group.id + GroupUser.create(user: target_user, group: group) + GroupUser.create(user: user, group: group) + end + + it "returns true when both users are valid" do + expect(serializer.can_chat_user).to eq(true) + end + + it "returns false if current user has chat disabled" do + user.user_option.update!(chat_enabled: false) + expect(serializer.can_chat_user).to eq(false) + end + + it "returns false if target user has chat disabled" do + target_user.user_option.update!(chat_enabled: false) + expect(serializer.can_chat_user).to eq(false) + end + + it "returns false if user is not in dm allowed group" do + SiteSetting.direct_message_enabled_groups = 3 + expect(serializer.can_chat_user).to eq(false) + end + end end context "when chat not enabled" do