about summary refs log tree commit diff
path: root/app/controllers/admin/domain_blocks_controller.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/admin/domain_blocks_controller.rb')
-rw-r--r--app/controllers/admin/domain_blocks_controller.rb50
1 files changed, 30 insertions, 20 deletions
diff --git a/app/controllers/admin/domain_blocks_controller.rb b/app/controllers/admin/domain_blocks_controller.rb
index 47c2daa7a..d0858006a 100644
--- a/app/controllers/admin/domain_blocks_controller.rb
+++ b/app/controllers/admin/domain_blocks_controller.rb
@@ -2,7 +2,7 @@
 
 module Admin
   class DomainBlocksController < BaseController
-    before_action :set_domain_block, only: [:show, :destroy]
+    before_action :set_domain_block, only: [:show, :destroy, :update]
 
     def new
       authorize :domain_block, :create?
@@ -15,23 +15,17 @@ module Admin
       @domain_block = DomainBlock.new(resource_params)
       existing_domain_block = resource_params[:domain].present? ? DomainBlock.find_by(domain: resource_params[:domain]) : nil
 
-      if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
-        @domain_block.save
-        flash[:alert] = I18n.t('admin.domain_blocks.existing_domain_block_html', name: existing_domain_block.domain, unblock_url: admin_domain_block_path(existing_domain_block)).html_safe # rubocop:disable Rails/OutputSafety
-        @domain_block.errors[:domain].clear
-        render :new
+      if existing_domain_block.present?
+        @domain_block = existing_domain_block
+        @domain_block.update(resource_params.except(:undo))
+      end
+
+      if @domain_block.save
+        DomainBlockWorker.perform_async(@domain_block.id)
+        log_action :create, @domain_block
+        redirect_to admin_instance_path(id: @domain_block.domain, limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
       else
-        if existing_domain_block.present?
-          @domain_block = existing_domain_block
-          @domain_block.update(resource_params)
-        end
-        if @domain_block.save
-          DomainBlockWorker.perform_async(@domain_block.id)
-          log_action :create, @domain_block
-          redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
-        else
-          render :new
-        end
+        render :new
       end
     end
 
@@ -41,9 +35,25 @@ module Admin
 
     def destroy
       authorize @domain_block, :destroy?
-      UnblockDomainService.new.call(@domain_block)
+      DomainUnblockWorker.perform_async(@domain_block.id)
       log_action :destroy, @domain_block
-      redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.destroyed_msg')
+      flash[:notice] = I18n.t('admin.domain_blocks.destroyed_msg')
+      redirect_to controller: 'admin/instances', action: 'index', limited: '1'
+    end
+
+    def update
+      return destroy unless resource_params[:undo].to_i.zero?
+      authorize @domain_block, :update?
+      @domain_block.update(resource_params.except(:domain, :undo))
+      changed = @domain_block.changed
+      if @domain_block.save
+        DomainBlockWorker.perform_async(@domain_block.id) if (changed & %w(severity force_sensitive reject_media)).any?
+        log_action :update, @domain_block
+        flash[:notice] = I18n.t('admin.domain_blocks.updated_msg')
+      else
+        flash[:alert] = I18n.t('admin.domain_blocks.update_failed_msg')
+      end
+      redirect_to admin_instance_path(id: @domain_block.domain, limited: '1')
     end
 
     private
@@ -53,7 +63,7 @@ module Admin
     end
 
     def resource_params
-      params.require(:domain_block).permit(:domain, :severity, :force_sensitive, :reject_media, :reject_reports)
+      params.require(:domain_block).permit(:domain, :severity, :force_sensitive, :reject_media, :reject_reports, :reason, :undo)
     end
   end
 end