domain_blocks_controller.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. class Api::V1::DomainBlocksController < Api::BaseController
  3. BLOCK_LIMIT = 100
  4. before_action -> { doorkeeper_authorize! :follow, :read, :'read:blocks' }, only: :show
  5. before_action -> { doorkeeper_authorize! :follow, :write, :'write:blocks' }, except: :show
  6. before_action :require_user!
  7. after_action :insert_pagination_headers, only: :show
  8. def show
  9. @blocks = load_domain_blocks
  10. render json: @blocks.map(&:domain)
  11. end
  12. def create
  13. current_account.block_domain!(domain_block_params[:domain])
  14. AfterAccountDomainBlockWorker.perform_async(current_account.id, domain_block_params[:domain])
  15. render_empty
  16. end
  17. def destroy
  18. current_account.unblock_domain!(domain_block_params[:domain])
  19. render_empty
  20. end
  21. private
  22. def load_domain_blocks
  23. account_domain_blocks.paginate_by_max_id(
  24. limit_param(BLOCK_LIMIT),
  25. params[:max_id],
  26. params[:since_id]
  27. )
  28. end
  29. def account_domain_blocks
  30. current_account.domain_blocks
  31. end
  32. def insert_pagination_headers
  33. set_pagination_headers(next_path, prev_path)
  34. end
  35. def next_path
  36. api_v1_domain_blocks_url pagination_params(max_id: pagination_max_id) if records_continue?
  37. end
  38. def prev_path
  39. api_v1_domain_blocks_url pagination_params(since_id: pagination_since_id) unless @blocks.empty?
  40. end
  41. def pagination_max_id
  42. @blocks.last.id
  43. end
  44. def pagination_since_id
  45. @blocks.first.id
  46. end
  47. def records_continue?
  48. @blocks.size == limit_param(BLOCK_LIMIT)
  49. end
  50. def pagination_params(core_params)
  51. params.slice(:limit).permit(:limit).merge(core_params)
  52. end
  53. def domain_block_params
  54. params.permit(:domain)
  55. end
  56. end