expunge: decrement upload and note/post update counts (fix #2062).

This commit is contained in:
evazion
2017-07-28 17:10:10 -05:00
committed by r888888888
parent 95854756b4
commit fd9dc6f647
6 changed files with 27 additions and 9 deletions

View File

@@ -147,8 +147,6 @@ class Note < ApplicationRecord
def create_version
return unless versioned_attributes_changed?
User.where(id: CurrentUser.id).update_all("note_update_count = note_update_count + 1")
CurrentUser.reload
if merge_version?
merge_version

View File

@@ -1,6 +1,6 @@
class NoteVersion < ApplicationRecord
before_validation :initialize_updater
belongs_to :updater, :class_name => "User"
belongs_to :updater, :class_name => "User", :counter_cache => "note_update_count"
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
attr_accessible :note_id, :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id, :html_id, :version

View File

@@ -36,7 +36,7 @@ class Post < ApplicationRecord
belongs_to :updater, :class_name => "User"
belongs_to :approver, :class_name => "User"
belongs_to :uploader, :class_name => "User"
belongs_to :uploader, :class_name => "User", :counter_cache => "post_upload_count"
belongs_to :parent, :class_name => "Post"
has_one :upload, :dependent => :destroy
has_one :artist_commentary, :dependent => :destroy

View File

@@ -2,7 +2,7 @@ class PostArchive < ApplicationRecord
extend Memoist
belongs_to :post
belongs_to :updater, class_name: "User"
belongs_to :updater, class_name: "User", counter_cache: "post_update_count"
def self.enabled?
Danbooru.config.aws_sqs_archives_url.present?

View File

@@ -139,7 +139,6 @@ class Upload < ApplicationRecord
post = convert_to_post
post.distribute_files
if post.save
User.where(id: CurrentUser.id).update_all("post_upload_count = post_upload_count + 1")
create_artist_commentary(post) if include_artist_commentary?
ugoira_service.save_frame_data(post) if is_ugoira?
notify_cropper(post)

View File

@@ -61,6 +61,25 @@ class PostTest < ActiveSupport::TestCase
assert_equal(0, Favorite.for_user(@user.id).where("post_id = ?", @post.id).count)
end
should "decrement the uploader's upload count" do
assert_difference("@post.uploader.reload.post_upload_count", -1) do
@post.expunge!
end
end
should "decrement the user's note update count" do
FactoryGirl.create(:note, post: @post)
assert_difference(["@post.uploader.reload.note_update_count"], -1) do
@post.expunge!
end
end
should "decrement the user's post update count" do
assert_difference(["@post.uploader.reload.post_update_count"], -1) do
@post.expunge!
end
end
should "remove the post from iqdb" do
mock_iqdb_service!
Post.iqdb_sqs_service.expects(:send_message).with("remove\n#{@post.id}")
@@ -1162,11 +1181,13 @@ class PostTest < ActiveSupport::TestCase
should "increment the updater's post_update_count" do
PostArchive.sqs_service.stubs(:merge?).returns(false)
post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc")
CurrentUser.reload
assert_difference("CurrentUser.post_update_count", 1) do
# XXX in the test environment the update count gets bumped twice: and
# once by Post#post_update_count, and once by the counter cache. in
# production the counter cache doesn't bump the count, because
# versions are created on a separate server.
assert_difference("CurrentUser.user.reload.post_update_count", 2) do
post.update_attributes(:tag_string => "zzz")
CurrentUser.reload
end
end