闭社主体 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.

146 lines
3.8 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 :current_theme
  12. helper_method :single_user_mode?
  13. rescue_from ActionController::RoutingError, with: :not_found
  14. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  15. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  16. rescue_from Mastodon::NotPermittedError, with: :forbidden
  17. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  18. before_action :check_suspension, if: :user_signed_in?
  19. def raise_not_found
  20. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  21. end
  22. private
  23. def https_enabled?
  24. Rails.env.production?
  25. end
  26. def store_current_location
  27. store_location_for(:user, request.url)
  28. end
  29. def require_admin!
  30. redirect_to root_path unless current_user&.admin?
  31. end
  32. def require_staff!
  33. redirect_to root_path unless current_user&.staff?
  34. end
  35. def check_suspension
  36. forbidden if current_user.account.suspended?
  37. end
  38. def after_sign_out_path_for(_resource_or_scope)
  39. new_user_session_path
  40. end
  41. protected
  42. def forbidden
  43. respond_with_error(403)
  44. end
  45. def not_found
  46. respond_with_error(404)
  47. end
  48. def gone
  49. respond_with_error(410)
  50. end
  51. def unprocessable_entity
  52. respond_with_error(422)
  53. end
  54. def single_user_mode?
  55. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.exists?
  56. end
  57. def current_account
  58. @current_account ||= current_user.try(:account)
  59. end
  60. def current_session
  61. @current_session ||= SessionActivation.find_by(session_id: cookies.signed['_session_id'])
  62. end
  63. def current_theme
  64. return Setting.default_settings['theme'] unless Themes.instance.names.include? current_user&.setting_theme
  65. current_user.setting_theme
  66. end
  67. def cache_collection(raw, klass)
  68. return raw unless klass.respond_to?(:with_includes)
  69. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  70. uncached_ids = []
  71. cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
  72. raw.each do |item|
  73. uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
  74. end
  75. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  76. unless uncached_ids.empty?
  77. uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
  78. uncached.each_value do |item|
  79. Rails.cache.write(item.cache_key, item)
  80. end
  81. end
  82. raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact
  83. end
  84. def respond_with_error(code)
  85. respond_to do |format|
  86. format.any { head code }
  87. format.html do
  88. set_locale
  89. render "errors/#{code}", layout: 'error', status: code
  90. end
  91. end
  92. end
  93. def render_cached_json(cache_key, **options)
  94. options[:expires_in] ||= 3.minutes
  95. cache_key = cache_key.join(':') if cache_key.is_a?(Enumerable)
  96. cache_public = options.key?(:public) ? options.delete(:public) : true
  97. content_type = options.delete(:content_type) || 'application/json'
  98. data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do
  99. yield.to_json
  100. end
  101. expires_in options[:expires_in], public: cache_public
  102. render json: data, content_type: content_type
  103. end
  104. def set_cache_headers
  105. response.headers['Vary'] = 'Accept'
  106. end
  107. def skip_session!
  108. request.session_options[:skip] = true
  109. end
  110. end