2022-06-01 17:31:36 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Admin::DomainBlocksController < Api::BaseController
|
|
|
|
include Authorization
|
|
|
|
include AccountableConcern
|
|
|
|
|
|
|
|
LIMIT = 100
|
2024-08-02 08:54:48 +02:00
|
|
|
MAX_LIMIT = 500
|
2022-06-01 17:31:36 +02:00
|
|
|
|
|
|
|
before_action -> { authorize_if_got_token! :'admin:read', :'admin:read:domain_blocks' }, only: [:index, :show]
|
|
|
|
before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:domain_blocks' }, except: [:index, :show]
|
|
|
|
before_action :set_domain_blocks, only: :index
|
|
|
|
before_action :set_domain_block, only: [:show, :update, :destroy]
|
|
|
|
|
2022-07-05 02:41:40 +02:00
|
|
|
after_action :verify_authorized
|
2022-06-01 17:31:36 +02:00
|
|
|
after_action :insert_pagination_headers, only: :index
|
|
|
|
|
2023-04-30 06:46:39 +02:00
|
|
|
def index
|
|
|
|
authorize :domain_block, :index?
|
|
|
|
render json: @domain_blocks, each_serializer: REST::Admin::DomainBlockSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
authorize @domain_block, :show?
|
|
|
|
render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
|
|
|
|
end
|
|
|
|
|
2022-06-01 17:31:36 +02:00
|
|
|
def create
|
|
|
|
authorize :domain_block, :create?
|
|
|
|
|
2024-05-02 22:56:21 +02:00
|
|
|
@domain_block = DomainBlock.new(resource_params)
|
2022-06-01 17:31:36 +02:00
|
|
|
existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
|
2024-05-02 22:56:21 +02:00
|
|
|
return render json: existing_domain_block, serializer: REST::Admin::ExistingDomainBlockErrorSerializer, status: 422 if conflicts_with_existing_block?(@domain_block, existing_domain_block)
|
2022-06-01 17:31:36 +02:00
|
|
|
|
2024-05-02 22:56:21 +02:00
|
|
|
@domain_block.save!
|
2022-06-01 17:31:36 +02:00
|
|
|
DomainBlockWorker.perform_async(@domain_block.id)
|
|
|
|
log_action :create, @domain_block
|
|
|
|
render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize @domain_block, :update?
|
2022-12-15 17:45:02 +01:00
|
|
|
@domain_block.update!(domain_block_params)
|
|
|
|
DomainBlockWorker.perform_async(@domain_block.id, @domain_block.severity_previously_changed?)
|
2022-06-01 17:31:36 +02:00
|
|
|
log_action :update, @domain_block
|
|
|
|
render json: @domain_block, serializer: REST::Admin::DomainBlockSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize @domain_block, :destroy?
|
|
|
|
UnblockDomainService.new.call(@domain_block)
|
|
|
|
log_action :destroy, @domain_block
|
2022-10-30 02:43:57 +02:00
|
|
|
render_empty
|
2022-06-01 17:31:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-05-02 22:56:21 +02:00
|
|
|
def conflicts_with_existing_block?(domain_block, existing_domain_block)
|
|
|
|
existing_domain_block.present? && (existing_domain_block.domain == TagManager.instance.normalize_domain(domain_block.domain) || !domain_block.stricter_than?(existing_domain_block))
|
|
|
|
end
|
|
|
|
|
2022-06-01 17:31:36 +02:00
|
|
|
def set_domain_blocks
|
2024-08-02 08:54:48 +02:00
|
|
|
@domain_blocks = DomainBlock.order(id: :desc).to_a_paginated_by_id(limit_param(LIMIT, MAX_LIMIT), params_slice(:max_id, :since_id, :min_id))
|
2022-06-01 17:31:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_domain_block
|
|
|
|
@domain_block = DomainBlock.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain_block_params
|
|
|
|
params.permit(:severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_path
|
|
|
|
api_v1_admin_domain_blocks_url(pagination_params(max_id: pagination_max_id)) if records_continue?
|
|
|
|
end
|
|
|
|
|
|
|
|
def prev_path
|
|
|
|
api_v1_admin_domain_blocks_url(pagination_params(min_id: pagination_since_id)) unless @domain_blocks.empty?
|
|
|
|
end
|
|
|
|
|
2024-03-13 09:51:44 +01:00
|
|
|
def pagination_collection
|
|
|
|
@domain_blocks
|
2022-06-01 17:31:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def records_continue?
|
2024-08-02 08:54:48 +02:00
|
|
|
@domain_blocks.size == limit_param(LIMIT, MAX_LIMIT)
|
2022-06-01 17:31:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def resource_params
|
|
|
|
params.permit(:domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
|
|
|
|
end
|
|
|
|
end
|