mirror of
https://github.com/mastodon/mastodon.git
synced 2025-05-29 04:57:12 +00:00
Add REST API for fetching an account's endorsed accounts (#34421)
This commit is contained in:
parent
ba0bd3da4a
commit
ff0990ec9f
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
|
end
|
||||||
|
|
||||||
def set_featured_tags
|
def set_featured_tags
|
||||||
@featured_tags = @account.suspended? ? [] : @account.featured_tags
|
@featured_tags = @account.unavailable? ? [] : @account.featured_tags
|
||||||
end
|
end
|
||||||
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 :lists, only: :index
|
||||||
resources :identity_proofs, only: :index
|
resources :identity_proofs, only: :index
|
||||||
resources :featured_tags, only: :index
|
resources :featured_tags, only: :index
|
||||||
|
resources :endorsements, only: :index
|
||||||
end
|
end
|
||||||
|
|
||||||
member do
|
member do
|
||||||
|
@ -203,8 +204,10 @@ namespace :api, format: false do
|
||||||
end
|
end
|
||||||
|
|
||||||
scope module: :accounts do
|
scope module: :accounts do
|
||||||
resource :pin, only: :create
|
post :pin, to: 'endorsements#create'
|
||||||
post :unpin, to: 'pins#destroy'
|
post :endorse, to: 'endorsements#create'
|
||||||
|
post :unpin, to: 'endorsements#destroy'
|
||||||
|
post :unendorse, to: 'endorsements#destroy'
|
||||||
resource :note, only: :create
|
resource :note, only: :create
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,30 @@ RSpec.describe 'Accounts Pins API' do
|
||||||
kevin.account.followers << user.account
|
kevin.account.followers << user.account
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/:account_id/pin' do
|
describe 'GET /api/v1/accounts/:account_id/endorsements' do
|
||||||
subject { post "/api/v1/accounts/#{kevin.account.id}/pin", headers: headers }
|
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
|
it 'creates account_pin', :aggregate_failures do
|
||||||
expect do
|
expect do
|
||||||
|
@ -26,8 +48,8 @@ RSpec.describe 'Accounts Pins API' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/:account_id/unpin' do
|
describe 'POST /api/v1/accounts/:account_id/unendorse' do
|
||||||
subject { post "/api/v1/accounts/#{kevin.account.id}/unpin", headers: headers }
|
subject { post "/api/v1/accounts/#{kevin.account.id}/unendorse", headers: headers }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
Fabricate(:account_pin, account: user.account, target_account: kevin.account)
|
Fabricate(:account_pin, account: user.account, target_account: kevin.account)
|
Loading…
Reference in New Issue
Block a user