domain_blocks_controller.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: true
  2. class Api::V1::DomainBlocksController < Api::BaseController
  3. BLOCK_LIMIT = 100
  4. before_action -> { doorkeeper_authorize! :follow, :'read:blocks' }, only: :show
  5. before_action -> { doorkeeper_authorize! :follow, :'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. if records_continue?
  37. api_v1_domain_blocks_url pagination_params(max_id: pagination_max_id)
  38. end
  39. end
  40. def prev_path
  41. unless @blocks.empty?
  42. api_v1_domain_blocks_url pagination_params(since_id: pagination_since_id)
  43. end
  44. end
  45. def pagination_max_id
  46. @blocks.last.id
  47. end
  48. def pagination_since_id
  49. @blocks.first.id
  50. end
  51. def records_continue?
  52. @blocks.size == limit_param(BLOCK_LIMIT)
  53. end
  54. def pagination_params(core_params)
  55. params.slice(:limit).permit(:limit).merge(core_params)
  56. end
  57. def domain_block_params
  58. params.permit(:domain)
  59. end
  60. end