Skip to content

A platform for community discussion. Free, open, simple.

FIX: Cakeday birthdate not visible to users with hidden profiles (#39129)

Régis HANOLb554ecc35244
diff --git a/plugins/discourse-cakeday/plugin.rb b/plugins/discourse-cakeday/plugin.rb
index 1dffd7eeab8..9b9e7efbb66 100644
--- a/plugins/discourse-cakeday/plugin.rb
+++ b/plugins/discourse-cakeday/plugin.rb
@@ -35,7 +35,10 @@ after_initialize do
add_to_serializer(
serializer,
:cakedate,
- include_condition: -> { scope.user.present? && object.user_option&.hide_profile != true },
+ include_condition: -> do
+ scope.user.present? &&
+ (object.user_option&.hide_profile != true || scope.user.id == object.id)
+ end,
) do
timezone = scope.user.user_option&.timezone.presence || "UTC"
object.created_at.in_time_zone(timezone).strftime("%Y-%m-%d")
@@ -46,7 +49,7 @@ after_initialize do
:birthdate,
include_condition: -> do
SiteSetting.cakeday_birthday_enabled && scope.user.present? &&
- object.user_option&.hide_profile != true
+ (object.user_option&.hide_profile != true || scope.user.id == object.id)
end,
) { object.date_of_birth }
end
@@ -58,7 +61,7 @@ after_initialize do
:user_cakedate,
include_condition: -> do
scope.user.present? && object.user&.created_at.present? &&
- object.user.user_option&.hide_profile != true
+ (object.user.user_option&.hide_profile != true || scope.user.id == object.user_id)
end,
) do
timezone = scope.user.user_option&.timezone.presence || "UTC"
@@ -70,7 +73,8 @@ after_initialize do
:user_birthdate,
include_condition: -> do
SiteSetting.cakeday_birthday_enabled && scope.user.present? &&
- object.user&.date_of_birth.present? && object.user.user_option&.hide_profile != true
+ object.user&.date_of_birth.present? &&
+ (object.user.user_option&.hide_profile != true || scope.user.id == object.user_id)
end,
) { object.user.date_of_birth }
end
diff --git a/plugins/discourse-cakeday/spec/serializers/post_serializer_spec.rb b/plugins/discourse-cakeday/spec/serializers/post_serializer_spec.rb
index 0cb33f7a891..1a061331a0a 100644
--- a/plugins/discourse-cakeday/spec/serializers/post_serializer_spec.rb
+++ b/plugins/discourse-cakeday/spec/serializers/post_serializer_spec.rb
@@ -33,12 +33,16 @@ RSpec.describe PostSerializer do
user.user_option.update!(hide_profile: true)
end
- it "does not include the user's cakedate" do
- expect(serializer.as_json.has_key?(:user_cakedate)).to eq(false)
+ it "still includes cakedate and birthdate for the user's own posts" do
+ expect(serializer.as_json[:user_cakedate]).to eq(user.created_at.strftime("%Y-%m-%d"))
+ expect(serializer.as_json[:user_birthdate]).to eq(user.date_of_birth)
end
- it "does not include the user's birthdate" do
- expect(serializer.as_json.has_key?(:user_birthdate)).to eq(false)
+ it "does not include cakedate or birthdate for other users viewing the post" do
+ other_user = Fabricate(:user)
+ other_serializer = described_class.new(post, scope: Guardian.new(other_user), root: false)
+ expect(other_serializer.as_json.has_key?(:user_cakedate)).to eq(false)
+ expect(other_serializer.as_json.has_key?(:user_birthdate)).to eq(false)
end
end
end
diff --git a/plugins/discourse-cakeday/spec/serializers/user_serializer_spec.rb b/plugins/discourse-cakeday/spec/serializers/user_serializer_spec.rb
index 616dd70d9ae..b061048c883 100644
--- a/plugins/discourse-cakeday/spec/serializers/user_serializer_spec.rb
+++ b/plugins/discourse-cakeday/spec/serializers/user_serializer_spec.rb
@@ -32,12 +32,16 @@ RSpec.describe UserSerializer do
user.user_option.update!(hide_profile: true)
end
- it "does not include the user's cakedate" do
- expect(serializer.as_json.has_key?(:user_cakedate)).to eq(false)
+ it "still includes birthdate and cakedate for the user themselves" do
+ expect(serializer.as_json[:birthdate]).to eq(user.date_of_birth)
+ expect(serializer.as_json[:cakedate]).to eq(user.created_at.strftime("%Y-%m-%d"))
end
- it "does not include the user's birthdate" do
- expect(serializer.as_json.has_key?(:user_birthdate)).to eq(false)
+ it "does not include cakedate or birthdate for other users" do
+ other_user = Fabricate(:user)
+ other_serializer = described_class.new(user, scope: Guardian.new(other_user), root: false)
+ expect(other_serializer.as_json.has_key?(:cakedate)).to eq(false)
+ expect(other_serializer.as_json.has_key?(:birthdate)).to eq(false)
end
end
end