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

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