From c7d603046e1d7d64e570856e6045966c75ae5c44 Mon Sep 17 00:00:00 2001 From: Jan Cernik Date: Tue, 4 Jun 2024 00:49:13 -0300 Subject: [PATCH 1/2] UX: Show message and chat buttons on hidden profiles --- app/serializers/hidden_profile_serializer.rb | 6 +++++- plugins/chat/plugin.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/serializers/hidden_profile_serializer.rb b/app/serializers/hidden_profile_serializer.rb index f0b8e00d3b615..5755171bd7ecb 100644 --- a/app/serializers/hidden_profile_serializer.rb +++ b/app/serializers/hidden_profile_serializer.rb @@ -1,12 +1,16 @@ # frozen_string_literal: true class HiddenProfileSerializer < BasicUserSerializer - attributes(:profile_hidden?, :title, :primary_group_name) + attributes(:profile_hidden?, :title, :primary_group_name, :can_send_private_message_to_user) def profile_hidden? true end + def can_send_private_message_to_user + scope.can_send_private_message?(object) + end + def primary_group_name object.primary_group.try(:name) end diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb index fd73a0d0040e8..f0c6745faffc0 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -150,6 +150,14 @@ module ::Chat scope.can_direct_message? && Guardian.new(object).can_chat? end + add_to_serializer(:hidden_profile, :can_chat_user) do + return false if !SiteSetting.chat_enabled + 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.can_direct_message? && Guardian.new(object).can_chat? + end + add_to_serializer( :current_user, :can_chat, From 827522fccb31ec23e7f530e4b35367268e940bfb Mon Sep 17 00:00:00 2001 From: Jan Cernik Date: Fri, 7 Jun 2024 04:10:16 -0300 Subject: [PATCH 2/2] Specs --- .../core_ext/users_controller_spec.rb | 30 +++++++++++++++++++ spec/requests/users_controller_spec.rb | 27 ++++++++++++----- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/plugins/chat/spec/requests/core_ext/users_controller_spec.rb b/plugins/chat/spec/requests/core_ext/users_controller_spec.rb index d6cbe07224a85..274134f3d2ba8 100644 --- a/plugins/chat/spec/requests/core_ext/users_controller_spec.rb +++ b/plugins/chat/spec/requests/core_ext/users_controller_spec.rb @@ -53,4 +53,34 @@ expect(notifications.size).to eq(0) end end + + describe "#show_card" do + fab!(:user) { Fabricate(:user) } + fab!(:another_user) { Fabricate(:user) } + context "when hidden users" do + before do + sign_in(another_user) + SiteSetting.chat_enabled = true + SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] + SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone] + user.user_option.update!(hide_profile_and_presence: true) + end + + it "returns the correct partial response when the user has chat enabled" do + user.user_option.update!(chat_enabled: true) + get "/u/#{user.username}/card.json" + expect(response).to be_successful + expect(response.parsed_body["user"]["profile_hidden"]).to eq(true) + expect(response.parsed_body["user"]["can_chat_user"]).to eq(true) + end + + it "returns the correct partial response when the user has chat disabled" do + user.user_option.update!(chat_enabled: false) + get "/u/#{user.username}/card.json" + expect(response).to be_successful + expect(response.parsed_body["user"]["profile_hidden"]).to eq(true) + expect(response.parsed_body["user"]["can_chat_user"]).to eq(false) + end + end + end end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index b43f104a37d68..17b14190df2c1 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -4792,18 +4792,31 @@ def create_and_like_post(likee, liker) expect(response.parsed_body["user"]["inactive"]).to eq(true) end - it "returns partial response when hidden users" do - user.user_option.update!(hide_profile_and_presence: true) - get "/u/#{user.username}/card.json" - expect(response).to be_successful - expect(response.parsed_body["user"]["profile_hidden"]).to eq(true) - end - it "raises an error on invalid access" do Guardian.any_instance.expects(:can_see?).with(user).returns(false) get "/u/#{user.username}/card.json" expect(response).to be_forbidden end + + context "when hidden users" do + before { user.user_option.update!(hide_profile_and_presence: true) } + + it "returns the correct partial response when the user has messages enabled" do + user.user_option.update!(allow_private_messages: true) + get "/u/#{user.username}/card.json" + expect(response).to be_successful + expect(response.parsed_body["user"]["profile_hidden"]).to eq(true) + expect(response.parsed_body["user"]["can_send_private_message_to_user"]).to eq(true) + end + + it "returns the correct partial response when the user has messages disabled" do + user.user_option.update!(allow_private_messages: false) + get "/u/#{user.username}/card.json" + expect(response).to be_successful + expect(response.parsed_body["user"]["profile_hidden"]).to eq(true) + expect(response.parsed_body["user"]["can_send_private_message_to_user"]).to eq(false) + end + end end end