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