mirror of
https://github.com/glitch-soc/mastodon
synced 2025-04-25 01:54:50 +00:00
Add REST API for fetching an account's endorsed accounts (#34421)
This commit is contained in:
parent
ba0bd3da4a
commit
ff0990ec9f
5 changed files with 98 additions and 37 deletions
66
app/controllers/api/v1/accounts/endorsements_controller.rb
Normal file
66
app/controllers/api/v1/accounts/endorsements_controller.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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)
|
Loading…
Add table
Reference in a new issue