docbrown/app/controllers/async_info_controller.rb
Jeremy Friesen 8c2b1276a4
Adding copy to assist with space toggle decision (#17147)
This commit adds instruction as to what's happening when a person
toggles on this feature.  So as to not repeat knowlege, I added a
`NUMBER_OF_MINUTES_FOR_CACHE_EXPIRY` constant so the UI can reference that
instead of dropping a hard-coded 15 in the UI.

Closes forem/forem#17097
2022-04-06 11:07:21 -04:00

64 lines
1.8 KiB
Ruby

# @note No pundit policy. All actions are unrestricted.
class AsyncInfoController < ApplicationController
NUMBER_OF_MINUTES_FOR_CACHE_EXPIRY = 15
def base_data
flash.discard(:notice)
if user_signed_in?
@user = current_user.decorate
respond_to do |format|
format.json do
render json: {
broadcast: broadcast_data,
param: request_forgery_protection_token,
token: form_authenticity_token,
user: user_data,
creator: user_is_a_creator
}
end
end
else
render json: {
broadcast: broadcast_data,
param: request_forgery_protection_token,
token: form_authenticity_token
}
end
end
def broadcast_data
broadcast = Broadcast.announcement.active.first.presence
return unless broadcast
{
title: broadcast&.title,
html: broadcast&.processed_html,
banner_class: helpers.banner_class(broadcast)
}.to_json
end
# @note The `user_cache_key` uses `current_user` and this method assumes `@user` which is a
# decorated version of the user. It would be nice if we were using the same "variable" for
# the cache key and for that which we cache.
def user_data
Rails.cache.fetch(user_cache_key, expires_in: NUMBER_OF_MINUTES_FOR_CACHE_EXPIRY.minutes) do
AsyncInfo.to_hash(user: @user, context: self)
end.to_json
end
def user_is_a_creator
@user.creator?
end
def user_cache_key
"user-info-#{current_user&.id}__
#{current_user&.last_sign_in_at}__
#{current_user&.following_tags_count}__
#{current_user&.last_followed_at}__
#{current_user&.last_reacted_at}__
#{current_user&.updated_at}__
#{current_user&.reactions_count}__
#{current_user&.articles_count}__
#{current_user&.blocking_others_count}__"
end
end