domain_blocks_controller.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. respond_to :json
  9. def show
  10. @blocks = load_domain_blocks
  11. render json: @blocks.map(&:domain)
  12. end
  13. def create
  14. current_account.block_domain!(domain_block_params[:domain])
  15. AfterAccountDomainBlockWorker.perform_async(current_account.id, domain_block_params[:domain])
  16. render_empty
  17. end
  18. def destroy
  19. current_account.unblock_domain!(domain_block_params[:domain])
  20. render_empty
  21. end
  22. private
  23. def load_domain_blocks
  24. account_domain_blocks.paginate_by_max_id(
  25. limit_param(BLOCK_LIMIT),
  26. params[:max_id],
  27. params[:since_id]
  28. )
  29. end
  30. def account_domain_blocks
  31. current_account.domain_blocks
  32. end
  33. def insert_pagination_headers
  34. set_pagination_headers(next_path, prev_path)
  35. end
  36. def next_path
  37. if records_continue?
  38. api_v1_domain_blocks_url pagination_params(max_id: pagination_max_id)
  39. end
  40. end
  41. def prev_path
  42. unless @blocks.empty?
  43. api_v1_domain_blocks_url pagination_params(since_id: pagination_since_id)
  44. end
  45. end
  46. def pagination_max_id
  47. @blocks.last.id
  48. end
  49. def pagination_since_id
  50. @blocks.first.id
  51. end
  52. def records_continue?
  53. @blocks.size == limit_param(BLOCK_LIMIT)
  54. end
  55. def pagination_params(core_params)
  56. params.slice(:limit).permit(:limit).merge(core_params)
  57. end
  58. def domain_block_params
  59. params.permit(:domain)
  60. end
  61. end