Added followers count to /api/users/me endpoint (#20378)

* Added followers_count to /api/users/me

* Updated the api user docs

* Rubocop fixes fro swagger_helper

* Added me jbuilder view for api/v0
This commit is contained in:
Anna Buianova 2023-11-22 23:22:06 +03:00 committed by GitHub
parent 81dd1b867e
commit df0b134ec4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 867 additions and 442 deletions

View file

@ -19,9 +19,7 @@ module Api
not_found unless @user.registered not_found unless @user.registered
end end
def me def me; end
render :show
end
def unpublish def unpublish
authorize(@user, :unpublish_all_articles?) authorize(@user, :unpublish_all_articles?)

View file

@ -0,0 +1 @@
json.partial! "api/v0/shared/user_show", user: @user

View file

@ -0,0 +1,2 @@
json.partial! "api/v1/shared/user_show_extended", user: @user
json.followers_count @user.followers_count

View file

@ -28,7 +28,7 @@ RSpec.describe "Api::V1::Docs::Users" do
response 200, "successful" do response 200, "successful" do
let(:"api-key") { api_secret.secret } let(:"api-key") { api_secret.secret }
schema type: :object, schema type: :object,
items: { "$ref": "#/components/schemas/User" } items: { "$ref": "#/components/schemas/MyUser" }
add_examples add_examples
run_test! run_test!
end end
@ -58,7 +58,7 @@ For complete documentation, see the v0 API docs: https://developers.forem.com/ap
let(:"api-key") { api_secret.secret } let(:"api-key") { api_secret.secret }
let(:id) { user.id } let(:id) { user.id }
schema type: :object, schema type: :object,
items: { "$ref": "#/components/schemas/User" } items: { "$ref": "#/components/schemas/ExtendedUser" }
run_test! run_test!
end end

View file

@ -60,6 +60,7 @@ RSpec.describe "Api::V1::Users" do
expect(response_user["joined_at"]).to eq(user.created_at.strftime("%b %e, %Y")) expect(response_user["joined_at"]).to eq(user.created_at.strftime("%b %e, %Y"))
expect(response_user["profile_image"]).to eq(user.profile_image_url_for(length: 320)) expect(response_user["profile_image"]).to eq(user.profile_image_url_for(length: 320))
expect(response_user["badge_ids"]).to eq(user.badge_ids) expect(response_user["badge_ids"]).to eq(user.badge_ids)
expect(response_user.key?("followers_count")).to be false
end end
it "includes email if display_email_on_profile is set to true" do it "includes email if display_email_on_profile is set to true" do
@ -109,7 +110,6 @@ RSpec.describe "Api::V1::Users" do
get me_api_users_path, headers: auth_headers get me_api_users_path, headers: auth_headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
response_user = response.parsed_body response_user = response.parsed_body
expect(response_user["type_of"]).to eq("user") expect(response_user["type_of"]).to eq("user")
@ -126,6 +126,7 @@ RSpec.describe "Api::V1::Users" do
expect(response_user["profile_image"]).to eq(user.profile_image_url_for(length: 320)) expect(response_user["profile_image"]).to eq(user.profile_image_url_for(length: 320))
expect(response_user["badge_ids"]).to eq(user.badge_ids) expect(response_user["badge_ids"]).to eq(user.badge_ids)
expect(response_user["followers_count"]).to eq(user.followers_count)
end end
it "returns 200 if no authentication and the Forem instance is set to private but user is authenticated" do it "returns 200 if no authentication and the Forem instance is set to private but user is authenticated" do
@ -147,6 +148,13 @@ RSpec.describe "Api::V1::Users" do
expect(response_user["joined_at"]).to eq(user.created_at.strftime("%b %e, %Y")) expect(response_user["joined_at"]).to eq(user.created_at.strftime("%b %e, %Y"))
expect(response_user["profile_image"]).to eq(user.profile_image_url_for(length: 320)) expect(response_user["profile_image"]).to eq(user.profile_image_url_for(length: 320))
end end
it "returns followers_count" do
create(:follow, followable: user)
get me_api_users_path, headers: auth_headers
response_user = response.parsed_body
expect(response_user["followers_count"]).to eq(1)
end
end end
end end

View file

@ -355,7 +355,7 @@ The default maximum value can be overridden by \"API_PER_PAGE_MAX\" environment
} }
}, },
User: { User: {
description: "The representation of a user", description: "The representation of a user returned in a list",
type: :object, type: :object,
properties: { properties: {
type_of: { type: :string }, type_of: { type: :string },
@ -371,6 +371,49 @@ The default maximum value can be overridden by \"API_PER_PAGE_MAX\" environment
profile_image: { type: :string } profile_image: { type: :string }
} }
}, },
ExtendedUser: {
description: "The representation of a user",
type: :object,
properties: {
type_of: { type: :string },
id: { type: :integer, format: :int64 },
username: { type: :string },
name: { type: :string },
summary: { type: :string, nullable: true },
twitter_username: { type: :string },
github_username: { type: :string },
email: { type: :string, nullable: true, description: "Email (if user allows displaying email on their profile) or nil" },
website_url: { type: :string, nullable: true },
location: { type: :string, nullable: true },
joined_at: { type: :string },
profile_image: { type: :string },
badge_ids: { type: :array,
items: { type: :integer },
description: "ids of the badges awarded to the user" }
}
},
MyUser: {
description: "The representation of a user when accessed by themselves",
type: :object,
properties: {
type_of: { type: :string },
id: { type: :integer, format: :int64 },
username: { type: :string },
name: { type: :string },
summary: { type: :string, nullable: true },
twitter_username: { type: :string },
github_username: { type: :string },
email: { type: :string, nullable: true, description: "Email (if user allows displaying email on their profile) or nil" },
website_url: { type: :string, nullable: true },
location: { type: :string, nullable: true },
joined_at: { type: :string },
profile_image: { type: :string },
badge_ids: { type: :array,
items: { type: :integer },
description: "ids of the badges awarded to the user" },
followers_count: { type: :integer }
}
},
SharedPodcast: { SharedPodcast: {
description: "The podcast that the resource belongs to", description: "The podcast that the resource belongs to",
type: :object, type: :object,

File diff suppressed because it is too large Load diff