diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 8396daf73..133c63865 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -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 diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index 3fe223943..42d1fa824 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -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