instances_controller.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # frozen_string_literal: true
  2. module Admin
  3. class InstancesController < BaseController
  4. before_action :set_instances, only: :index
  5. before_action :set_instance, except: :index
  6. def index
  7. authorize :instance, :index?
  8. preload_delivery_failures!
  9. end
  10. def show
  11. authorize :instance, :show?
  12. @time_period = (6.days.ago.to_date...Time.now.utc.to_date)
  13. end
  14. def destroy
  15. authorize :instance, :destroy?
  16. Admin::DomainPurgeWorker.perform_async(@instance.domain)
  17. log_action :destroy, @instance
  18. redirect_to admin_instances_path, notice: I18n.t('admin.instances.destroyed_msg', domain: @instance.domain)
  19. end
  20. def clear_delivery_errors
  21. authorize :delivery, :clear_delivery_errors?
  22. @instance.delivery_failure_tracker.clear_failures!
  23. redirect_to admin_instance_path(@instance.domain)
  24. end
  25. def restart_delivery
  26. authorize :delivery, :restart_delivery?
  27. if @instance.unavailable?
  28. @instance.delivery_failure_tracker.track_success!
  29. log_action :destroy, @instance.unavailable_domain
  30. end
  31. redirect_to admin_instance_path(@instance.domain)
  32. end
  33. def stop_delivery
  34. authorize :delivery, :stop_delivery?
  35. unavailable_domain = UnavailableDomain.create!(domain: @instance.domain)
  36. log_action :create, unavailable_domain
  37. redirect_to admin_instance_path(@instance.domain)
  38. end
  39. private
  40. def set_instance
  41. @instance = Instance.find_or_initialize_by(domain: TagManager.instance.normalize_domain(params[:id]&.strip))
  42. end
  43. def set_instances
  44. @instances = filtered_instances.page(params[:page])
  45. end
  46. def preload_delivery_failures!
  47. warning_domains_map = DeliveryFailureTracker.warning_domains_map(@instances.map(&:domain))
  48. @instances.each do |instance|
  49. instance.failure_days = warning_domains_map[instance.domain]
  50. end
  51. end
  52. def filtered_instances
  53. InstanceFilter.new(limited_federation_mode? ? { allowed: true } : filter_params).results
  54. end
  55. def filter_params
  56. params.slice(*InstanceFilter::KEYS).permit(*InstanceFilter::KEYS)
  57. end
  58. end
  59. end