logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

application_controller.rb (4083B)


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