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

122 lines
3.3 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. helper_method :current_account
  9. helper_method :single_user_mode?
  10. rescue_from ActionController::RoutingError, with: :not_found
  11. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  12. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  13. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  14. before_action :set_user_activity
  15. before_action :check_suspension, if: :user_signed_in?
  16. def raise_not_found
  17. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  18. end
  19. private
  20. def https_enabled?
  21. Rails.env.production? && ENV['LOCAL_HTTPS'] == 'true'
  22. end
  23. def store_current_location
  24. store_location_for(:user, request.url)
  25. end
  26. def require_admin!
  27. redirect_to root_path unless current_user&.admin?
  28. end
  29. def set_user_activity
  30. return unless !current_user.nil? && (current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < 24.hours.ago)
  31. # Mark user as signed-in today
  32. current_user.update_tracked_fields(request)
  33. # If the sign in is after a two week break, we need to regenerate their feed
  34. RegenerationWorker.perform_async(current_user.account_id) if current_user.last_sign_in_at < 14.days.ago
  35. end
  36. def check_suspension
  37. head 403 if current_user.account.suspended?
  38. end
  39. protected
  40. def not_found
  41. respond_to do |format|
  42. format.any { head 404 }
  43. format.html { respond_with_error(404) }
  44. end
  45. end
  46. def gone
  47. respond_to do |format|
  48. format.any { head 410 }
  49. format.html { respond_with_error(410) }
  50. end
  51. end
  52. def forbidden
  53. respond_to do |format|
  54. format.any { head 403 }
  55. format.html { render 'errors/403', layout: 'error', status: 403 }
  56. end
  57. end
  58. def unprocessable_entity
  59. respond_to do |format|
  60. format.any { head 422 }
  61. format.html { respond_with_error(422) }
  62. end
  63. end
  64. def single_user_mode?
  65. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.first
  66. end
  67. def current_account
  68. @current_account ||= current_user.try(:account)
  69. end
  70. def cache_collection(raw, klass)
  71. return raw unless klass.respond_to?(:with_includes)
  72. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  73. uncached_ids = []
  74. cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
  75. raw.each do |item|
  76. uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
  77. end
  78. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  79. unless uncached_ids.empty?
  80. uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
  81. uncached.values.each do |item|
  82. Rails.cache.write(item.cache_key, item)
  83. end
  84. end
  85. raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact
  86. end
  87. def respond_with_error(code)
  88. set_locale do
  89. render "errors/#{code}", layout: 'error', status: code
  90. end
  91. end
  92. end