Skip to content

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.rb
index e5c1075b6e5..4146df06474 100644
--- a/app/serializers/user_serializer.rb
+++ b/app/serializers/user_serializer.rb
@@ -32,7 +32,7 @@ class UserSerializer < UserCardSerializer
can_edit
end
- staff_attributes :post_count, :can_be_deleted, :can_delete_all_posts
+ staff_attributes :post_count, :topic_count, :can_be_deleted, :can_delete_all_posts
private_attributes :locale,
:muted_category_ids,
@@ -230,6 +230,10 @@ class UserSerializer < UserCardSerializer
object.user_stat.try(:post_count)
end
+ def topic_count
+ object.user_stat.try(:topic_count)
+ end
+
def can_be_deleted
scope.can_delete_user?(object)
end
diff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.json
index 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.rb
index 39ca971c0a5..768c5208261 100644
--- a/spec/serializers/user_serializer_spec.rb
+++ b/spec/serializers/user_serializer_spec.rb
@@ -3,7 +3,7 @@
RSpec.describe UserSerializer do
fab!(: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" do
let(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }
@@ -180,6 +180,26 @@ RSpec.describe UserSerializer do
end
end
+ 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" do
before { SiteSetting.enable_names = true }
diff --git a/spec/serializers/web_hook_user_serializer_spec.rb b/spec/serializers/web_hook_user_serializer_spec.rb
index 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,