A platform for community discussion. Free, open, simple.
DEV: Add topic_count to UserSerializer staff attributes (#37632)
Régis Hanola1cc7a2ba572
diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rbindex e5c1075b6e5..4146df06474 100644--- a/app/serializers/user_serializer.rb+++ b/app/serializers/user_serializer.rb@@ -32,7 +32,7 @@ class UserSerializer < UserCardSerializercan_editend- staff_attributes :post_count, :can_be_deleted, :can_delete_all_posts+ staff_attributes :post_count, :topic_count, :can_be_deleted, :can_delete_all_postsprivate_attributes :locale,:muted_category_ids,@@ -230,6 +230,10 @@ class UserSerializer < UserCardSerializerobject.user_stat.try(:post_count)end+ def topic_count+ object.user_stat.try(:topic_count)+ end+def can_be_deletedscope.can_delete_user?(object)enddiff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.jsonindex 122980ec31a..e21ead93da2 100644--- a/spec/requests/api/schemas/json/user_get_response.json+++ b/spec/requests/api/schemas/json/user_get_response.json@@ -216,6 +216,9 @@"post_count": {"type": "integer"},+ "topic_count": {+ "type": "integer"+ },"can_be_deleted": {"type": "boolean"},@@ -915,6 +918,7 @@"can_upload_profile_header","can_upload_user_card_background","post_count",+ "topic_count","can_be_deleted","can_delete_all_posts","locale",diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rbindex 39ca971c0a5..768c5208261 100644--- a/spec/serializers/user_serializer_spec.rb+++ b/spec/serializers/user_serializer_spec.rb@@ -3,7 +3,7 @@RSpec.describe UserSerializer dofab!(:user) { Fabricate(:user, trust_level: 0) }- before { user.user_stat.update!(post_count: 1) }+ before { user.user_stat.update!(post_count: 1, topic_count: 2) }context "with a TL0 user seen as anonymous" dolet(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }@@ -180,6 +180,26 @@ RSpec.describe UserSerializer doendend+ context "with staff attributes" do+ it "includes staff attributes for staff" do+ json = UserSerializer.new(user, scope: Guardian.new(Fabricate(:admin)), root: false).as_json++ expect(json[:post_count]).to eq(1)+ expect(json[:topic_count]).to eq(2)+ expect(json).to have_key(:can_be_deleted)+ expect(json).to have_key(:can_delete_all_posts)+ end++ it "does not include staff attributes for non-staff" do+ json = UserSerializer.new(user, scope: Guardian.new, root: false).as_json++ expect(json).not_to have_key(:post_count)+ expect(json).not_to have_key(:topic_count)+ expect(json).not_to have_key(:can_be_deleted)+ expect(json).not_to have_key(:can_delete_all_posts)+ end+ end+context "with `enable_names` true" dobefore { SiteSetting.enable_names = true }diff --git a/spec/serializers/web_hook_user_serializer_spec.rb b/spec/serializers/web_hook_user_serializer_spec.rbindex f3d4e5900fc..807fc9be451 100644--- a/spec/serializers/web_hook_user_serializer_spec.rb+++ b/spec/serializers/web_hook_user_serializer_spec.rb@@ -66,6 +66,7 @@ RSpec.describe WebHookUserSerializer do:system_avatar_template,:time_read,:title,+ :topic_count,:tracked_category_ids,:tracked_tags,:trust_level,