domain_blocks_controller.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # frozen_string_literal: true
  2. class Api::V1::Instances::DomainBlocksController < Api::V1::Instances::BaseController
  3. before_action :require_enabled_api!
  4. before_action :set_domain_blocks
  5. vary_by '', if: -> { Setting.show_domain_blocks == 'all' }
  6. def index
  7. if Setting.show_domain_blocks == 'all'
  8. cache_even_if_authenticated!
  9. else
  10. cache_if_unauthenticated!
  11. end
  12. render json: @domain_blocks, each_serializer: REST::DomainBlockSerializer, with_comment: show_rationale_in_response?
  13. end
  14. private
  15. def require_enabled_api!
  16. head 404 unless api_enabled?
  17. end
  18. def api_enabled?
  19. show_domain_blocks_for_all? || show_domain_blocks_to_user?
  20. end
  21. def show_domain_blocks_for_all?
  22. Setting.show_domain_blocks == 'all'
  23. end
  24. def show_domain_blocks_to_user?
  25. Setting.show_domain_blocks == 'users' && user_signed_in?
  26. end
  27. def set_domain_blocks
  28. @domain_blocks = DomainBlock.with_user_facing_limitations.by_severity
  29. end
  30. def show_rationale_in_response?
  31. always_show_rationale? || show_rationale_for_user?
  32. end
  33. def always_show_rationale?
  34. Setting.show_domain_blocks_rationale == 'all'
  35. end
  36. def show_rationale_for_user?
  37. Setting.show_domain_blocks_rationale == 'users' && user_signed_in?
  38. end
  39. end