]> cat aescling's git repositories - mastodon.git/blob - app/controllers/application_controller.rb
Extract user tracking into concern (#2600)
[mastodon.git] / app / controllers / application_controller.rb
1 # frozen_string_literal: true
2
3 class ApplicationController < ActionController::Base
4 # Prevent CSRF attacks by raising an exception.
5 # For APIs, you may want to use :null_session instead.
6 protect_from_forgery with: :exception
7
8 force_ssl if: :https_enabled?
9
10 include Localized
11 include UserTrackingConcern
12
13 helper_method :current_account
14 helper_method :single_user_mode?
15
16 rescue_from ActionController::RoutingError, with: :not_found
17 rescue_from ActiveRecord::RecordNotFound, with: :not_found
18 rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
19
20 before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
21 before_action :check_suspension, if: :user_signed_in?
22
23 def raise_not_found
24 raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
25 end
26
27 private
28
29 def https_enabled?
30 Rails.env.production? && ENV['LOCAL_HTTPS'] == 'true'
31 end
32
33 def store_current_location
34 store_location_for(:user, request.url)
35 end
36
37 def require_admin!
38 redirect_to root_path unless current_user&.admin?
39 end
40
41 def check_suspension
42 head 403 if current_user.account.suspended?
43 end
44
45 protected
46
47 def not_found
48 respond_to do |format|
49 format.any { head 404 }
50 format.html { respond_with_error(404) }
51 end
52 end
53
54 def gone
55 respond_to do |format|
56 format.any { head 410 }
57 format.html { respond_with_error(410) }
58 end
59 end
60
61 def forbidden
62 respond_to do |format|
63 format.any { head 403 }
64 format.html { render 'errors/403', layout: 'error', status: 403 }
65 end
66 end
67
68 def unprocessable_entity
69 respond_to do |format|
70 format.any { head 422 }
71 format.html { respond_with_error(422) }
72 end
73 end
74
75 def single_user_mode?
76 @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.first
77 end
78
79 def current_account
80 @current_account ||= current_user.try(:account)
81 end
82
83 def cache_collection(raw, klass)
84 return raw unless klass.respond_to?(:with_includes)
85
86 raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
87 uncached_ids = []
88 cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
89
90 raw.each do |item|
91 uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
92 end
93
94 klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
95
96 unless uncached_ids.empty?
97 uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
98
99 uncached.values.each do |item|
100 Rails.cache.write(item.cache_key, item)
101 end
102 end
103
104 raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact
105 end
106
107 def respond_with_error(code)
108 set_locale
109 render "errors/#{code}", layout: 'error', status: code
110 end
111 end
This page took 0.099656 seconds and 4 git commands to generate.