post replacements: don't queue deletion when file is unchanged (#3235).
When replacing a post with the same file, don't queue a deletion and don't leave a comment or modaction. There's no need to do these things when we're restoring a missing or corrupted image with the original file.
This commit is contained in:
@@ -29,6 +29,7 @@ class PostReplacement < ApplicationRecord
|
||||
|
||||
upload.process_upload
|
||||
upload.update(status: "completed", post_id: post.id)
|
||||
md5_changed = (upload.md5 != post.md5)
|
||||
|
||||
if replacement_file.present?
|
||||
update(replacement_url: "file://#{replacement_file.original_filename}")
|
||||
@@ -39,7 +40,9 @@ class PostReplacement < ApplicationRecord
|
||||
# queue the deletion *before* updating the post so that we use the old
|
||||
# md5/file_ext to delete the old files. if saving the post fails,
|
||||
# this is rolled back so the job won't run.
|
||||
Post.delay(queue: "default", run_at: Time.now + DELETION_GRACE_PERIOD).delete_files(post.id, post.file_path, post.large_file_path, post.preview_file_path)
|
||||
if md5_changed
|
||||
Post.delay(queue: "default", run_at: Time.now + DELETION_GRACE_PERIOD).delete_files(post.id, post.file_path, post.large_file_path, post.preview_file_path)
|
||||
end
|
||||
|
||||
post.md5 = upload.md5
|
||||
post.file_ext = upload.file_ext
|
||||
@@ -51,8 +54,10 @@ class PostReplacement < ApplicationRecord
|
||||
rescale_notes
|
||||
update_ugoira_frame_data(upload)
|
||||
|
||||
post.comments.create!({creator: User.system, body: comment_replacement_message, do_not_bump_post: true}, without_protection: true)
|
||||
ModAction.log(modaction_replacement_message)
|
||||
if md5_changed
|
||||
post.comments.create!({creator: User.system, body: comment_replacement_message, do_not_bump_post: true}, without_protection: true)
|
||||
ModAction.log(modaction_replacement_message)
|
||||
end
|
||||
|
||||
post.save!
|
||||
end
|
||||
|
||||
@@ -261,6 +261,14 @@ class PostReplacementTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
should "not queue a deletion or log a comment" do
|
||||
upload_file("#{Rails.root}/test/files/test.jpg", "test.jpg") do |file|
|
||||
assert_no_difference(["@post.comments.count", "ModAction.count"]) do
|
||||
@post.replace!(replacement_file: file, replacement_url: "")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user