domain_blocks_controller.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. class Api::V1::Admin::DomainBlocksController < Api::BaseController
  3. include Authorization
  4. include AccountableConcern
  5. LIMIT = 100
  6. before_action -> { authorize_if_got_token! :'admin:read', :'admin:read:domain_blocks' }, only: [:index, :show]
  7. before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:domain_blocks' }, except: [:index, :show]
  8. before_action :set_domain_blocks, only: :index
  9. before_action :set_domain_block, only: [:show, :update, :destroy]
  10. after_action :verify_authorized
  11. after_action :insert_pagination_headers, only: :index
  12. PAGINATION_PARAMS = %i(limit).freeze
  13. def create
  14. authorize :domain_block, :create?
  15. existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
  16. return render json: existing_domain_block, serializer: REST::Admin::ExistingDomainBlockErrorSerializer, status: 422 if existing_domain_block.present?
  17. @domain_block = DomainBlock.create!(resource_params)
  18. DomainBlockWorker.perform_async(@domain_block.id)
  19. log_action :create, @domain_block
  20. render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
  21. end
  22. def index
  23. authorize :domain_block, :index?
  24. render json: @domain_blocks, each_serializer: REST::Admin::DomainBlockSerializer
  25. end
  26. def show
  27. authorize @domain_block, :show?
  28. render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
  29. end
  30. def update
  31. authorize @domain_block, :update?
  32. @domain_block.update(domain_block_params)
  33. severity_changed = @domain_block.severity_changed?
  34. @domain_block.save!
  35. DomainBlockWorker.perform_async(@domain_block.id, severity_changed)
  36. log_action :update, @domain_block
  37. render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
  38. end
  39. def destroy
  40. authorize @domain_block, :destroy?
  41. UnblockDomainService.new.call(@domain_block)
  42. log_action :destroy, @domain_block
  43. render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
  44. end
  45. private
  46. def set_domain_blocks
  47. @domain_blocks = filtered_domain_blocks.order(id: :desc).to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  48. end
  49. def set_domain_block
  50. @domain_block = DomainBlock.find(params[:id])
  51. end
  52. def filtered_domain_blocks
  53. # TODO: no filtering yet
  54. DomainBlock.all
  55. end
  56. def domain_block_params
  57. params.permit(:severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
  58. end
  59. def insert_pagination_headers
  60. set_pagination_headers(next_path, prev_path)
  61. end
  62. def next_path
  63. api_v1_admin_domain_blocks_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  64. end
  65. def prev_path
  66. api_v1_admin_domain_blocks_url(pagination_params(min_id: pagination_since_id)) unless @domain_blocks.empty?
  67. end
  68. def pagination_max_id
  69. @domain_blocks.last.id
  70. end
  71. def pagination_since_id
  72. @domain_blocks.first.id
  73. end
  74. def records_continue?
  75. @domain_blocks.size == limit_param(LIMIT)
  76. end
  77. def pagination_params(core_params)
  78. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  79. end
  80. def resource_params
  81. params.permit(:domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
  82. end
  83. end