diff --git a/Gemfile b/Gemfile index dc935c551..7ecbcc737 100644 --- a/Gemfile +++ b/Gemfile @@ -33,7 +33,6 @@ gem 'activemodel-serializers-xml' gem 'webpacker', '>= 4.0.x' gem 'rake' gem 'redis' -gem 'request_store' gem 'builder' # gem 'did_you_mean' # github.com/yuki24/did_you_mean/issues/117 gem 'puma' diff --git a/Gemfile.lock b/Gemfile.lock index 5267140b2..5c41e20ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -301,8 +301,6 @@ GEM json redis (4.2.5) regexp_parser (1.8.2) - request_store (1.5.0) - rack (>= 1.4) responders (3.0.1) actionpack (>= 5.0) railties (>= 5.0) @@ -450,7 +448,6 @@ DEPENDENCIES rakismet recaptcha redis - request_store responders rubocop rubocop-rails diff --git a/app/controllers/favorite_groups_controller.rb b/app/controllers/favorite_groups_controller.rb index c3abe691f..3f3b02f9e 100644 --- a/app/controllers/favorite_groups_controller.rb +++ b/app/controllers/favorite_groups_controller.rb @@ -47,7 +47,7 @@ class FavoriteGroupsController < ApplicationController @favorite_group = authorize FavoriteGroup.find(params[:id]) @favorite_group.destroy! flash[:notice] = "Favorite group deleted" if request.format.html? - respond_with(@favorite_group, location: favorite_groups_path(search: { creator_name: CurrentUser.name })) + respond_with(@favorite_group, location: favorite_groups_path(search: { creator_name: CurrentUser.user.name })) end def add_post diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 76f8beb3b..8449dac63 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -12,7 +12,7 @@ class FavoritesController < ApplicationController user = User.find(params[:user_id]) redirect_to posts_path(tags: "ordfav:#{user.name}", format: request.format.symbol) elsif !CurrentUser.is_anonymous? - redirect_to posts_path(tags: "ordfav:#{CurrentUser.name}", format: request.format.symbol) + redirect_to posts_path(tags: "ordfav:#{CurrentUser.user.name}", format: request.format.symbol) else redirect_to posts_path(format: request.format.symbol) end diff --git a/app/controllers/user_name_change_requests_controller.rb b/app/controllers/user_name_change_requests_controller.rb index 2dfc96b2e..72b00c2b2 100644 --- a/app/controllers/user_name_change_requests_controller.rb +++ b/app/controllers/user_name_change_requests_controller.rb @@ -7,7 +7,7 @@ class UserNameChangeRequestsController < ApplicationController end def create - @change_request = authorize UserNameChangeRequest.new(user: CurrentUser.user, original_name: CurrentUser.name) + @change_request = authorize UserNameChangeRequest.new(user: CurrentUser.user, original_name: CurrentUser.user.name) @change_request.update(permitted_attributes(@change_request)) flash[:notice] = "Your name has been changed" if @change_request.valid? respond_with(@change_request, location: profile_path) diff --git a/app/logical/current_user.rb b/app/logical/current_user.rb index 5145ec7a7..f66439dbb 100644 --- a/app/logical/current_user.rb +++ b/app/logical/current_user.rb @@ -1,16 +1,13 @@ -class CurrentUser - def self.scoped(user, ip_addr = "127.0.0.1") - old_user = self.user - old_ip_addr = self.ip_addr +class CurrentUser < ActiveSupport::CurrentAttributes + attribute :user, :ip_addr, :country, :root_url, :safe_mode - self.user = user - self.ip_addr = ip_addr + alias_method :safe_mode?, :safe_mode + delegate :id, to: :user, allow_nil: true + delegate_missing_to :user - begin - yield - ensure - self.user = old_user - self.ip_addr = old_ip_addr + def self.scoped(user, ip_addr = "127.0.0.1", &block) + set(user: user, ip_addr: ip_addr) do + yield user end end @@ -24,59 +21,7 @@ class CurrentUser scoped(user, &block) end - def self.user - RequestStore[:current_user] - end - - def self.user=(user) - RequestStore[:current_user] = user - end - - def self.ip_addr - RequestStore[:current_ip_addr] - end - - def self.ip_addr=(ip_addr) - RequestStore[:current_ip_addr] = ip_addr - end - - def self.country - RequestStore[:country] - end - - def self.country=(country) - RequestStore[:country] = country - end - def self.root_url - RequestStore[:current_root_url] || "https://#{Danbooru.config.hostname}" - end - - def self.root_url=(root_url) - RequestStore[:current_root_url] = root_url - end - - def self.id - if user.nil? - nil - else - user.id - end - end - - def self.name - user.name - end - - def self.safe_mode? - RequestStore[:safe_mode] - end - - def self.safe_mode=(safe_mode) - RequestStore[:safe_mode] = safe_mode - end - - def self.method_missing(method, *params, &block) - user.__send__(method, *params, &block) + attributes[:root_url] || "https://#{Danbooru.config.hostname}" end end diff --git a/app/models/comment.rb b/app/models/comment.rb index c56271f56..e18f27276 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,11 +10,11 @@ class Comment < ApplicationRecord before_create :autoreport_spam after_create :update_last_commented_at_on_create after_update(:if => ->(rec) {(!rec.is_deleted? || !rec.saved_change_to_is_deleted?) && CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log("comment ##{rec.id} updated by #{CurrentUser.name}", :comment_update) + ModAction.log("comment ##{rec.id} updated by #{CurrentUser.user.name}", :comment_update) end after_save :update_last_commented_at_on_destroy, :if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted?} after_save(:if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted? && CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log("comment ##{rec.id} deleted by #{CurrentUser.name}", :comment_delete) + ModAction.log("comment ##{rec.id} deleted by #{CurrentUser.user.name}", :comment_delete) end deletable diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 8cab6029b..16941f3c9 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -18,10 +18,10 @@ class ForumPost < ApplicationRecord validates_presence_of :body after_save :delete_topic_if_original_post after_update(:if => ->(rec) {rec.updater_id != rec.creator_id}) do |rec| - ModAction.log("#{CurrentUser.name} updated forum ##{rec.id}", :forum_post_update) + ModAction.log("#{CurrentUser.user.name} updated forum ##{rec.id}", :forum_post_update) end after_destroy(:if => ->(rec) {rec.updater_id != rec.creator_id}) do |rec| - ModAction.log("#{CurrentUser.name} deleted forum ##{rec.id}", :forum_post_delete) + ModAction.log("#{CurrentUser.user.name} deleted forum ##{rec.id}", :forum_post_delete) end deletable diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index 75a0f7dc3..58b89ff18 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -8,10 +8,10 @@ class UserFeedback < ApplicationRecord validates_inclusion_of :category, :in => %w(positive negative neutral) after_create :create_dmail, unless: :disable_dmail_notification after_update(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_update) + ModAction.log(%{#{CurrentUser.user.name} updated user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_update) end after_destroy(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_delete) + ModAction.log(%{#{CurrentUser.user.name} deleted user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_delete) end deletable diff --git a/app/views/posts/partials/common/_secondary_links.html.erb b/app/views/posts/partials/common/_secondary_links.html.erb index 6d9613491..c248cd285 100644 --- a/app/views/posts/partials/common/_secondary_links.html.erb +++ b/app/views/posts/partials/common/_secondary_links.html.erb @@ -3,13 +3,13 @@ <%= subnav_link_to "Upload", new_upload_path %> <%= subnav_link_to "Hot", posts_path(:tags => "order:rank", :d => "1") %> <% if RecommenderService.available_for_user?(CurrentUser.user) %> - <%= subnav_link_to "Recommended", recommended_posts_path(search: { user_name: CurrentUser.name }) %> + <%= subnav_link_to "Recommended", recommended_posts_path(search: { user_name: CurrentUser.user.name }) %> <% end %> <% if policy(Favorite).create? %> <%= subnav_link_to "Favorites", posts_path(tags: "ordfav:#{CurrentUser.user.name}") %> <% end %> <% if policy(FavoriteGroup).create? %> - <%= subnav_link_to "Fav groups", favorite_groups_path(search: { creator_name: CurrentUser.name }) %> + <%= subnav_link_to "Fav groups", favorite_groups_path(search: { creator_name: CurrentUser.user.name }) %> <% end %> <% if policy(SavedSearch).create? %> <%= subnav_link_to "Saved searches", posts_path(tags: "search:all") %> diff --git a/test/unit/post_query_builder_test.rb b/test/unit/post_query_builder_test.rb index b38ce79e8..fab1a2e36 100644 --- a/test/unit/post_query_builder_test.rb +++ b/test/unit/post_query_builder_test.rb @@ -209,10 +209,10 @@ class PostQueryBuilderTest < ActiveSupport::TestCase end should "return posts for the ordfav: metatag" do - post1 = create(:post, tag_string: "fav:#{CurrentUser.name}") - post2 = create(:post, tag_string: "fav:#{CurrentUser.name}") + post1 = create(:post, tag_string: "fav:#{CurrentUser.user.name}") + post2 = create(:post, tag_string: "fav:#{CurrentUser.user.name}") - assert_tag_match([post2, post1], "ordfav:#{CurrentUser.name}") + assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name}") assert_tag_match([], "ordfav:does_not_exist") end @@ -855,13 +855,13 @@ class PostQueryBuilderTest < ActiveSupport::TestCase upvoted = create(:post, tag_string: "upvote:self") downvoted = create(:post, tag_string: "downvote:self") - assert_tag_match([upvoted], "upvote:#{CurrentUser.name}") - assert_tag_match([downvoted], "downvote:#{CurrentUser.name}") - assert_tag_match([], "upvote:nobody upvote:#{CurrentUser.name}") - assert_tag_match([], "downvote:nobody downvote:#{CurrentUser.name}") + assert_tag_match([upvoted], "upvote:#{CurrentUser.user.name}") + assert_tag_match([downvoted], "downvote:#{CurrentUser.user.name}") + assert_tag_match([], "upvote:nobody upvote:#{CurrentUser.user.name}") + assert_tag_match([], "downvote:nobody downvote:#{CurrentUser.user.name}") - assert_tag_match([downvoted], "-upvote:#{CurrentUser.name}") - assert_tag_match([upvoted], "-downvote:#{CurrentUser.name}") + assert_tag_match([downvoted], "-upvote:#{CurrentUser.user.name}") + assert_tag_match([upvoted], "-downvote:#{CurrentUser.user.name}") end end @@ -871,14 +871,14 @@ class PostQueryBuilderTest < ActiveSupport::TestCase disapproved = create(:post, is_pending: true) disapproval = create(:post_disapproval, user: CurrentUser.user, post: disapproved, reason: "disinterest") - assert_tag_match([disapproved], "disapproved:#{CurrentUser.name}") - assert_tag_match([disapproved], "disapproved:#{CurrentUser.name.upcase}") + assert_tag_match([disapproved], "disapproved:#{CurrentUser.user.name}") + assert_tag_match([disapproved], "disapproved:#{CurrentUser.user.name.upcase}") assert_tag_match([disapproved], "disapproved:disinterest") assert_tag_match([disapproved], "disapproved:DISINTEREST") assert_tag_match([], "disapproved:breaks_rules") assert_tag_match([], "disapproved:breaks_rules disapproved:disinterest") - assert_tag_match([pending], "-disapproved:#{CurrentUser.name}") + assert_tag_match([pending], "-disapproved:#{CurrentUser.user.name}") assert_tag_match([pending], "-disapproved:disinterest") assert_tag_match([disapproved, pending], "-disapproved:breaks_rules") end