Fix #4670: Replace RequestStore with AS::CurrentAttributes.

This also requires replacing CurrentUser.name with CurrentUser.user.name
because the `name` method had a conflict with CurrentAttributes.
This commit is contained in:
evazion
2021-01-16 12:43:20 -06:00
parent 6f6ec6592d
commit 6ca007ee1f
11 changed files with 32 additions and 91 deletions

View File

@@ -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'

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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") %>

View File

@@ -209,10 +209,10 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
end
should "return posts for the ordfav:<name> 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