docbrown/app/controllers/async_info_controller.rb
rhymes 6553f08d94 Rubocop cleanups (#1415)
* Update rubocop-todo.yml with new violations

* Fix Layout/EmptyLine* rules

* Fix Layout/Indentation* rules

* Fix remaining Layout/* rules

* Fix Lint/DuplicateMethods by removing unused accessor

* Fix Lint/IneffectiveAccessModifier

* Fix Lint/MissingCopEnableDirective

* Re-run rubocop auto gen config

* Fix Layout/RescueEnsureAlignment

* Fix Naming/* rules

* Fix some RSpec/* rules

* Fix typos

* Fix RSpec/LetBeforeExamples

* Series should only be an attr_writer, not an attr_accessor

* Fix RSpec/InstanceVariable

* Fix RSpec/InstanceVariable

* Fix RSpec/RepeatedDescription and RSpec/RepeatedExample

* Fix Style/ClassAndModuleChildren

* Fix Style/ConditionalAssignment

* Fix some Style/* rules

* Trigger Travis CI build because failing tests are not failing locally

* Revert "Fix Style/ClassAndModuleChildren"

This reverts commit 1686801d8a1516ba1894f79e24401a20dea65f99.
2019-01-02 11:20:02 -05:00

65 lines
2.1 KiB
Ruby

class AsyncInfoController < ApplicationController
include Devise::Controllers::Rememberable
# No pundit policy. All actions are unrestricted.
def base_data
flash.discard(:notice)
unless user_signed_in?
render json: {
param: request_forgery_protection_token,
token: form_authenticity_token
}
return
end
if cookies[:remember_user_token].blank?
current_user.remember_me = true
current_user.remember_me!
remember_me(current_user)
end
@user = current_user.decorate
# Updates article analytics periodically:
ArticleAnalyticsFetcher.new.delay.update_analytics(@user.id) if rand(20) == 1
respond_to do |format|
format.json do
render json: {
param: request_forgery_protection_token,
token: form_authenticity_token,
user: user_data.to_json
}
end
end
end
def user_data
Rails.cache.fetch(user_cache_key, expires_in: 15.minutes) do
{
id: @user.id,
name: @user.name,
username: @user.username,
profile_image_90: ProfileImage.new(@user).get(90),
followed_tag_names: @user.cached_followed_tag_names,
followed_tags: @user.cached_followed_tags.to_json(only: %i[id name bg_color_hex text_color_hex], methods: [:points]),
followed_user_ids: @user.cached_following_users_ids,
followed_organization_ids: @user.cached_following_organizations_ids,
reading_list_ids: ReadingList.new(@user).cached_ids_of_articles,
saw_onboarding: @user.saw_onboarding,
checked_code_of_conduct: @user.checked_code_of_conduct,
number_of_comments: @user.comments.count,
display_sponsors: @user.display_sponsors,
trusted: @user.trusted
}
end
end
def user_cache_key
"#{current_user&.id}__
#{current_user&.last_sign_in_at}__
#{current_user&.updated_at}__
#{current_user&.reactions_count}__
#{current_user&.comments_count}__
#{current_user&.saw_onboarding}__
#{current_user&.checked_code_of_conduct}__
#{current_user&.articles_count}__
#{cookies[:remember_user_token]}"
end
end