about summary refs log tree commit diff
path: root/app/controllers/admin/instances_controller.rb
blob: 306ec1f532133321b5807a21bc2f9b3ab162f798 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

module Admin
  class InstancesController < BaseController
    before_action :set_instances, only: :index
    before_action :set_instance, except: :index
    before_action :set_exhausted_deliveries_days, only: :show

    def index
      authorize :instance, :index?
    end

    def show
      authorize :instance, :show?
    end

    def destroy
      authorize :instance, :destroy?

      Admin::DomainPurgeWorker.perform_async(@instance.domain)

      log_action :destroy, @instance
      redirect_to admin_instances_path, notice: I18n.t('admin.instances.destroyed_msg', domain: @instance.domain)
    end

    def clear_delivery_errors
      authorize :delivery, :clear_delivery_errors?

      @instance.delivery_failure_tracker.clear_failures!
      redirect_to admin_instance_path(@instance.domain)
    end

    def restart_delivery
      authorize :delivery, :restart_delivery?

      last_unavailable_domain = unavailable_domain

      if last_unavailable_domain.present?
        @instance.delivery_failure_tracker.track_success!
        log_action :destroy, last_unavailable_domain
      end

      redirect_to admin_instance_path(@instance.domain)
    end

    def stop_delivery
      authorize :delivery, :stop_delivery?

      UnavailableDomain.create(domain: @instance.domain)
      log_action :create, unavailable_domain
      redirect_to admin_instance_path(@instance.domain)
    end

    private

    def set_instance
      @instance = Instance.find(params[:id])
    end

    def set_exhausted_deliveries_days
      @exhausted_deliveries_days = @instance.delivery_failure_tracker.exhausted_deliveries_days
    end

    def set_instances
      @instances = filtered_instances.page(params[:page])
      warning_domains_map = DeliveryFailureTracker.warning_domains_map

      @instances.each do |instance|
        instance.failure_days = warning_domains_map[instance.domain]
      end
    end

    def unavailable_domain
      UnavailableDomain.find_by(domain: @instance.domain)
    end

    def filtered_instances
      InstanceFilter.new(whitelist_mode? ? { allowed: true } : filter_params).results
    end

    def filter_params
      params.slice(*InstanceFilter::KEYS).permit(*InstanceFilter::KEYS)
    end
  end
end