Files
danbooru/app/logical/current_user.rb
evazion 52013eac1f posts: use low quality thumbnails when Save-Data header is set.
When the Save-Data HTTP header is present, disable high quality (2x
pixel density) thumbnails. This is normally set when "Data Saver mode"
is enabled on Android, or "Lite mode" is enabled in Chrome.

This setting can also be set using the `save_data` URL param or HTTP
cookie. This is mainly for testing.

The <body> tag has a `current-user-save-data` data attribute that
indicates whether save data mode is on.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Save-Data
https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/save-data/#the_save-data_request_header
https://source.android.com/devices/tech/connect/data-saver
2021-12-09 20:08:27 -06:00

31 lines
1.2 KiB
Ruby

# A global variable containing the current user, the current IP address, the
# current user's country code, whether safe mode is enabled, and whether
# save-data mode is enabled.
#
# The current user is set during a request by {ApplicationController#set_current_user},
# which calls {SessionLoader#load}. The current user will not be set outside of
# the request cycle, for example, during background jobs, cron jobs, or on the
# console. For this reason, code outside of controllers and views, such as
# models, shouldn't rely on or assume the current user exists.
#
# @see https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html
# @see ApplicationController#set_current_user
# @see SessionLoader#load
class CurrentUser < ActiveSupport::CurrentAttributes
attribute :user, :ip_addr, :country, :safe_mode, :save_data
alias_method :safe_mode?, :safe_mode
delegate :id, to: :user, allow_nil: true
delegate_missing_to :user
# Run a block of code as another user.
# @param user [User] the user to run as
# @param ip_addr [String] the IP address to run as
# @yield the block
def self.scoped(user, ip_addr = "127.0.0.1", &block)
set(user: user, ip_addr: ip_addr) do
yield user
end
end
end