about summary refs log tree commit diff
path: root/app/controllers/api/v1/domain_blocks_controller.rb
blob: f223dd16ee50ecd183a321c6b00ee1db520d0e7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

class Api::V1::DomainBlocksController < ApiController
  before_action -> { doorkeeper_authorize! :follow }
  before_action :require_user!

  respond_to :json

  def show
    @blocks = AccountDomainBlock.where(account: current_account).paginate_by_max_id(limit_param(100), params[:max_id], params[:since_id])

    next_path = api_v1_domain_blocks_url(pagination_params(max_id: @blocks.last.id))    if @blocks.size == limit_param(100)
    prev_path = api_v1_domain_blocks_url(pagination_params(since_id: @blocks.first.id)) unless @blocks.empty?

    set_pagination_headers(next_path, prev_path)
    render json: @blocks.map(&:domain)
  end

  def create
    BlockDomainFromAccountService.new.call(current_account, domain_block_params[:domain])
    render_empty
  end

  def destroy
    current_account.unblock_domain!(domain_block_params[:domain])
    render_empty
  end

  private

  def pagination_params(core_params)
    params.permit(:limit).merge(core_params)
  end

  def domain_block_params
    params.permit(:domain)
  end
end