From 4b23ecf4253ccf2340e7e8687791757b96fec811 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 10 Apr 2025 21:00:42 +0200 Subject: [PATCH] Add REST API for fetching an account's endorsed accounts --- .../v1/accounts/endorsements_controller.rb | 66 +++++++++++++++++++ .../v1/accounts/featured_tags_controller.rb | 2 +- .../api/v1/accounts/pins_controller.rb | 30 --------- config/routes/api.rb | 7 +- .../{pins_spec.rb => endorsements_spec.rb} | 30 +++++++-- 5 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 app/controllers/api/v1/accounts/endorsements_controller.rb delete mode 100644 app/controllers/api/v1/accounts/pins_controller.rb rename spec/requests/api/v1/accounts/{pins_spec.rb => endorsements_spec.rb} (57%) diff --git a/app/controllers/api/v1/accounts/endorsements_controller.rb b/app/controllers/api/v1/accounts/endorsements_controller.rb new file mode 100644 index 00000000000..1e21994a907 --- /dev/null +++ b/app/controllers/api/v1/accounts/endorsements_controller.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +class Api::V1::Accounts::EndorsementsController < Api::BaseController + include Authorization + + before_action -> { authorize_if_got_token! :read, :'read:accounts' }, only: :index + before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, except: :index + before_action :require_user!, except: :index + before_action :set_account + before_action :set_endorsed_accounts, only: :index + after_action :insert_pagination_headers, only: :index + + def index + cache_if_unauthenticated! + render json: @endorsed_accounts, each_serializer: REST::AccountSerializer + end + + def create + AccountPin.find_or_create_by!(account: current_account, target_account: @account) + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships_presenter + end + + def destroy + pin = AccountPin.find_by(account: current_account, target_account: @account) + pin&.destroy! + render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships_presenter + end + + private + + def set_account + @account = Account.find(params[:account_id]) + end + + def set_endorsed_accounts + @endorsed_accounts = @account.unavailable? ? [] : paginated_endorsed_accounts + end + + def paginated_endorsed_accounts + @account.endorsed_accounts.without_suspended.includes(:account_stat, :user).paginate_by_max_id( + limit_param(DEFAULT_ACCOUNTS_LIMIT), + params[:max_id], + params[:since_id] + ) + end + + def relationships_presenter + AccountRelationshipsPresenter.new([@account], current_user.account_id) + end + + def next_path + api_v1_account_endorsements_url pagination_params(max_id: pagination_max_id) if records_continue? + end + + def prev_path + api_v1_account_endorsements_url pagination_params(since_id: pagination_since_id) unless @endorsed_accounts.empty? + end + + def pagination_collection + @endorsed_accounts + end + + def records_continue? + @endorsed_accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + end +end diff --git a/app/controllers/api/v1/accounts/featured_tags_controller.rb b/app/controllers/api/v1/accounts/featured_tags_controller.rb index 0101fb469b8..f95846366c8 100644 --- a/app/controllers/api/v1/accounts/featured_tags_controller.rb +++ b/app/controllers/api/v1/accounts/featured_tags_controller.rb @@ -17,6 +17,6 @@ class Api::V1::Accounts::FeaturedTagsController < Api::BaseController end def set_featured_tags - @featured_tags = @account.suspended? ? [] : @account.featured_tags + @featured_tags = @account.unavailable? ? [] : @account.featured_tags end end diff --git a/app/controllers/api/v1/accounts/pins_controller.rb b/app/controllers/api/v1/accounts/pins_controller.rb deleted file mode 100644 index 0eb13c048ce..00000000000 --- a/app/controllers/api/v1/accounts/pins_controller.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -class Api::V1::Accounts::PinsController < Api::BaseController - include Authorization - - before_action -> { doorkeeper_authorize! :write, :'write:accounts' } - before_action :require_user! - before_action :set_account - - def create - AccountPin.find_or_create_by!(account: current_account, target_account: @account) - render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships_presenter - end - - def destroy - pin = AccountPin.find_by(account: current_account, target_account: @account) - pin&.destroy! - render json: @account, serializer: REST::RelationshipSerializer, relationships: relationships_presenter - end - - private - - def set_account - @account = Account.find(params[:account_id]) - end - - def relationships_presenter - AccountRelationshipsPresenter.new([@account], current_user.account_id) - end -end diff --git a/config/routes/api.rb b/config/routes/api.rb index c7be8c8e454..8fb8f5d0af4 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -190,6 +190,7 @@ namespace :api, format: false do resources :lists, only: :index resources :identity_proofs, only: :index resources :featured_tags, only: :index + resources :endorsements, only: :index end member do @@ -203,8 +204,10 @@ namespace :api, format: false do end scope module: :accounts do - resource :pin, only: :create - post :unpin, to: 'pins#destroy' + post :pin, to: 'endorsements#create' + post :endorse, to: 'endorsements#create' + post :unpin, to: 'endorsements#destroy' + post :unendorse, to: 'endorsements#destroy' resource :note, only: :create end end diff --git a/spec/requests/api/v1/accounts/pins_spec.rb b/spec/requests/api/v1/accounts/endorsements_spec.rb similarity index 57% rename from spec/requests/api/v1/accounts/pins_spec.rb rename to spec/requests/api/v1/accounts/endorsements_spec.rb index 8ebcb27d282..6e0996a1f1b 100644 --- a/spec/requests/api/v1/accounts/pins_spec.rb +++ b/spec/requests/api/v1/accounts/endorsements_spec.rb @@ -13,8 +13,30 @@ RSpec.describe 'Accounts Pins API' do kevin.account.followers << user.account end - describe 'POST /api/v1/accounts/:account_id/pin' do - subject { post "/api/v1/accounts/#{kevin.account.id}/pin", headers: headers } + describe 'GET /api/v1/accounts/:account_id/endorsements' do + subject { get "/api/v1/accounts/#{user.account.id}/endorsements", headers: headers } + + let(:scopes) { 'read:accounts' } + + before do + user.account.endorsed_accounts << kevin.account + end + + it 'returns the expected accounts', :aggregate_failures do + subject + + expect(response).to have_http_status(200) + expect(response.content_type) + .to start_with('application/json') + expect(response.parsed_body) + .to contain_exactly( + hash_including(id: kevin.account_id.to_s) + ) + end + end + + describe 'POST /api/v1/accounts/:account_id/endorse' do + subject { post "/api/v1/accounts/#{kevin.account.id}/endorse", headers: headers } it 'creates account_pin', :aggregate_failures do expect do @@ -26,8 +48,8 @@ RSpec.describe 'Accounts Pins API' do end end - describe 'POST /api/v1/accounts/:account_id/unpin' do - subject { post "/api/v1/accounts/#{kevin.account.id}/unpin", headers: headers } + describe 'POST /api/v1/accounts/:account_id/unendorse' do + subject { post "/api/v1/accounts/#{kevin.account.id}/unendorse", headers: headers } before do Fabricate(:account_pin, account: user.account, target_account: kevin.account)