From 7435f2e5165bf7864bc84cfb94adef02bb739006 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 1 Feb 2022 00:59:42 -0600 Subject: [PATCH] Fix #4969: Tag changes made by replacements wipe out edits done at the same time. Lock the post during replacement to ensure we have the latest version of the tags and to ensure nobody else can modify the post until after the replacement is finished. --- app/logical/post_replacement_processor.rb | 3 ++- app/models/post_replacement.rb | 2 -- test/functional/post_replacements_controller_test.rb | 5 ++++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/logical/post_replacement_processor.rb b/app/logical/post_replacement_processor.rb index 14b0590c6..01f4cdfa2 100644 --- a/app/logical/post_replacement_processor.rb +++ b/app/logical/post_replacement_processor.rb @@ -34,13 +34,14 @@ class PostReplacementProcessor replacement.image_width = media_asset.image_width replacement.md5 = media_asset.md5 + post.lock! post.md5 = media_asset.md5 post.file_ext = media_asset.file_ext post.image_width = media_asset.image_width post.image_height = media_asset.image_height post.file_size = media_asset.file_size post.source = replacement.final_source.presence || replacement.replacement_url - post.tag_string = replacement.tags + post.tag_string = "#{post.tag_string} #{replacement.tags}" rescale_notes(post) post.save! diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 099a9c4fd..bbebbfffe 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -12,8 +12,6 @@ class PostReplacement < ApplicationRecord def initialize_fields self.original_url = post.source - self.tags = "#{post.tag_string} #{tags}" - self.old_file_ext = post.file_ext self.old_file_size = post.file_size self.old_image_width = post.image_width diff --git a/test/functional/post_replacements_controller_test.rb b/test/functional/post_replacements_controller_test.rb index 9380dfd6e..abfddbca8 100644 --- a/test/functional/post_replacements_controller_test.rb +++ b/test/functional/post_replacements_controller_test.rb @@ -6,13 +6,14 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest context "replacing a post from a source url" do should "replace the post" do assert_difference("PostReplacement.count") do - @post = create(:post) + @post = create(:post, tag_string: "image_sample") post_auth post_replacements_path, create(:moderator_user), params: { format: :json, post_id: @post.id, post_replacement: { replacement_url: "https://cdn.donmai.us/original/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg", + tags: "replaced -image_sample" } } @@ -31,6 +32,7 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest assert_equal(@post.file_size, @replacement.old_file_size) assert_equal(@post.file_ext, @replacement.old_file_ext) assert_equal(@post.md5, @replacement.old_md5) + assert_equal(@post.tag_string, "image_sample") @post.reload assert_equal("d34e4cf0a437a5d65f8e82b7bcd02606", @post.md5) @@ -40,6 +42,7 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest assert_equal(650, @post.image_height) assert_equal(127_238, @post.file_size) assert_equal("jpg", @post.file_ext) + assert_equal("replaced", @post.tag_string) end end