闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class ApplicationController < ActionController::Base
  3. # Prevent CSRF attacks by raising an exception.
  4. # For APIs, you may want to use :null_session instead.
  5. protect_from_forgery with: :exception
  6. force_ssl if: :https_enabled?
  7. include Localized
  8. include UserTrackingConcern
  9. helper_method :current_account
  10. helper_method :current_session
  11. helper_method :single_user_mode?
  12. rescue_from ActionController::RoutingError, with: :not_found
  13. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  14. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  15. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  16. before_action :check_suspension, if: :user_signed_in?
  17. def raise_not_found
  18. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  19. end
  20. private
  21. def https_enabled?
  22. Rails.env.production? && ENV['LOCAL_HTTPS'] == 'true'
  23. end
  24. def store_current_location
  25. store_location_for(:user, request.url)
  26. end
  27. def require_admin!
  28. redirect_to root_path unless current_user&.admin?
  29. end
  30. def check_suspension
  31. forbidden if current_user.account.suspended?
  32. end
  33. def after_sign_out_path_for(_resource_or_scope)
  34. new_user_session_path
  35. end
  36. protected
  37. def forbidden
  38. respond_with_error(403)
  39. end
  40. def not_found
  41. respond_with_error(404)
  42. end
  43. def gone
  44. respond_with_error(410)
  45. end
  46. def unprocessable_entity
  47. respond_with_error(422)
  48. end
  49. def single_user_mode?
  50. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.exists?
  51. end
  52. def current_account
  53. @current_account ||= current_user.try(:account)
  54. end
  55. def current_session
  56. @current_session ||= SessionActivation.find_by(session_id: cookies.signed['_session_id'])
  57. end
  58. def cache_collection(raw, klass)
  59. return raw unless klass.respond_to?(:with_includes)
  60. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  61. uncached_ids = []
  62. cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
  63. raw.each do |item|
  64. uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
  65. end
  66. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  67. unless uncached_ids.empty?
  68. uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
  69. uncached.values.each do |item|
  70. Rails.cache.write(item.cache_key, item)
  71. end
  72. end
  73. raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact
  74. end
  75. def respond_with_error(code)
  76. respond_to do |format|
  77. format.any { head code }
  78. format.html do
  79. set_locale
  80. render "errors/#{code}", layout: 'error', status: code
  81. end
  82. end
  83. end
  84. end