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.

199 lines
6.2 KiB

8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 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_flavour
  12. helper_method :current_skin
  13. helper_method :single_user_mode?
  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? && ENV['LOCAL_HTTPS'] == 'true'
  26. end
  27. def store_current_location
  28. store_location_for(:user, request.url)
  29. end
  30. def require_admin!
  31. redirect_to root_path unless current_user&.admin?
  32. end
  33. def require_staff!
  34. redirect_to root_path 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. def pack(data, pack_name, skin = 'default')
  43. return nil unless pack?(data, pack_name)
  44. pack_data = {
  45. common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin),
  46. flavour: data['name'],
  47. pack: pack_name,
  48. preload: nil,
  49. skin: nil,
  50. supported_locales: data['locales'],
  51. }
  52. if data['pack'][pack_name].is_a?(Hash)
  53. pack_data[:common] = nil if data['pack'][pack_name]['use_common'] == false
  54. pack_data[:pack] = nil unless data['pack'][pack_name]['filename']
  55. if data['pack'][pack_name]['preload']
  56. pack_data[:preload] = [data['pack'][pack_name]['preload']] if data['pack'][pack_name]['preload'].is_a?(String)
  57. pack_data[:preload] = data['pack'][pack_name]['preload'] if data['pack'][pack_name]['preload'].is_a?(Array)
  58. end
  59. if skin != 'default' && data['skin'][skin]
  60. pack_data[:skin] = skin if data['skin'][skin].include?(pack_name)
  61. else # default skin
  62. pack_data[:skin] = 'default' if data['pack'][pack_name]['stylesheet']
  63. end
  64. end
  65. pack_data
  66. end
  67. def pack?(data, pack_name)
  68. if data['pack'].is_a?(Hash) && data['pack'].key?(pack_name)
  69. return true if data['pack'][pack_name].is_a?(String) || data['pack'][pack_name].is_a?(Hash)
  70. end
  71. false
  72. end
  73. def nil_pack(data, pack_name, skin = 'default')
  74. {
  75. common: pack_name == 'common' ? nil : resolve_pack(data['name'] ? Themes.instance.flavour(current_flavour) : Themes.instance.core, 'common', skin),
  76. flavour: data['name'],
  77. pack: nil,
  78. preload: nil,
  79. skin: nil,
  80. supported_locales: data['locales'],
  81. }
  82. end
  83. def resolve_pack(data, pack_name, skin = 'default')
  84. result = pack(data, pack_name, skin)
  85. unless result
  86. if data['name'] && data.key?('fallback')
  87. if data['fallback'].nil?
  88. return nil_pack(data, pack_name, skin)
  89. elsif data['fallback'].is_a?(String) && Themes.instance.flavour(data['fallback'])
  90. return resolve_pack(Themes.instance.flavour(data['fallback']), pack_name)
  91. elsif data['fallback'].is_a?(Array)
  92. data['fallback'].each do |fallback|
  93. return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback)
  94. end
  95. end
  96. return nil_pack(data, pack_name, skin)
  97. end
  98. return data.key?('name') && data['name'] != Setting.default_settings['flavour'] ? resolve_pack(Themes.instance.flavour(Setting.default_settings['flavour']), pack_name) : nil_pack(data, pack_name, skin)
  99. end
  100. result
  101. end
  102. def use_pack(pack_name)
  103. @core = resolve_pack(Themes.instance.core, pack_name)
  104. @theme = resolve_pack(Themes.instance.flavour(current_flavour), pack_name, current_skin)
  105. end
  106. protected
  107. def forbidden
  108. respond_with_error(403)
  109. end
  110. def not_found
  111. respond_with_error(404)
  112. end
  113. def gone
  114. respond_with_error(410)
  115. end
  116. def unprocessable_entity
  117. respond_with_error(422)
  118. end
  119. def single_user_mode?
  120. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.exists?
  121. end
  122. def current_account
  123. @current_account ||= current_user.try(:account)
  124. end
  125. def current_session
  126. @current_session ||= SessionActivation.find_by(session_id: cookies.signed['_session_id'])
  127. end
  128. def current_flavour
  129. return Setting.default_settings['flavour'] unless Themes.instance.flavours.include? current_user&.setting_flavour
  130. current_user.setting_flavour
  131. end
  132. def current_skin
  133. return 'default' unless Themes.instance.skins_for(current_flavour).include? current_user&.setting_skin
  134. current_user.setting_skin
  135. end
  136. def cache_collection(raw, klass)
  137. return raw unless klass.respond_to?(:with_includes)
  138. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  139. uncached_ids = []
  140. cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
  141. raw.each do |item|
  142. uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
  143. end
  144. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  145. unless uncached_ids.empty?
  146. uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
  147. uncached.each_value do |item|
  148. Rails.cache.write(item.cache_key, item)
  149. end
  150. end
  151. raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact
  152. end
  153. def respond_with_error(code)
  154. respond_to do |format|
  155. format.any { head code }
  156. format.html do
  157. set_locale
  158. render "errors/#{code}", layout: 'error', status: code
  159. end
  160. end
  161. end
  162. end