docbrown/app/controllers/application_controller.rb
2020-04-15 10:34:00 -04:00

104 lines
2.6 KiB
Ruby

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception, prepend: true
include SessionCurrentUser
include ValidRequest
include Pundit
include FastlyHeaders
rescue_from ActionView::MissingTemplate, with: :routing_error
def not_found
raise ActiveRecord::RecordNotFound, "Not Found"
end
def routing_error
raise ActionController::RoutingError, "Routing Error"
end
def not_authorized
render json: "Error: not authorized", status: :unauthorized
raise NotAuthorizedError, "Unauthorized"
end
def bad_request
render json: "Error: Bad Request", status: :bad_request
end
def authenticate_user!
return if current_user
respond_to do |format|
format.html { redirect_to "/enter" }
format.json { render json: { error: "Please sign in" }, status: :unauthorized }
end
end
def customize_params
params[:signed_in] = user_signed_in?.to_s
end
def after_sign_in_path_for(resource)
if current_user.saw_onboarding
path = request.env["omniauth.origin"] || stored_location_for(resource) || dashboard_path
signin_param = { "signin" => "true" } # the "signin" param is used by the service worker
uri = Addressable::URI.parse(path)
uri.query_values = if uri.query_values
uri.query_values.merge(signin_param)
else
signin_param
end
uri.to_s
else
referrer = request.env["omniauth.origin"] || "none"
onboarding_path(referrer: referrer)
end
end
def raise_suspended
raise "SUSPENDED" if current_user&.banned
end
def internal_navigation?
params[:i] == "i"
end
helper_method :internal_navigation?
def set_no_cache_header
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end
def touch_current_user
current_user.touch
end
def log_image_data_to_datadog
images = Array.wrap(
params.dig("user", "profile_image") ||
params.dig("podcast", "image") ||
params.dig("organization", "profile_image") ||
params["image"],
)
raise if images.empty?
images.each do |image|
tags = [
"controller:#{params['controller']}",
"action:#{params['action']}",
"content_type:#{image.content_type}",
"original_filename:#{image.original_filename}",
"tempfile:#{image.tempfile}",
"size:#{image.size}",
]
DatadogStatsClient.increment("image_upload_error", tags: tags)
end
raise
end
end