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.
This commit is contained in:
evazion
2022-02-01 00:59:42 -06:00
parent 60a13fd2d5
commit 7435f2e516
3 changed files with 6 additions and 4 deletions

View File

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

View File

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

View File

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