Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/serializers/hidden_profile_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions plugins/chat/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want the button on own user card? (you can message yourself so…)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think so. While you can message yourself, showing a button in your profile to message yourself seems more like a bug to me than something I would expect. scope.user.id == object.id is redundant anyway since we don't use the HiddenProfile serializer for our own profile

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,
Expand Down
30 changes: 30 additions & 0 deletions plugins/chat/spec/requests/core_ext/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 20 additions & 7 deletions spec/requests/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down