From 8408c0bf80eb1ad3faa561dccc5c574951e1328f Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 16 Dec 2018 13:45:11 -0600 Subject: [PATCH] Fix #3815: Adjust notes positions/sizes after post replacement Bug: Notes weren't rescaled when the 'final_source' field was given during replacement. The cause was that the notes were rescaled after the source was saved, but saving the source clobbered `image_{width,height}_before_last_save` inside `rescale_notes`. Regressed in b0c2ddba. --- app/logical/upload_service/replacer.rb | 14 ++++---------- test/models/upload_service_test.rb | 10 ++++++---- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/logical/upload_service/replacer.rb b/app/logical/upload_service/replacer.rb index 486eab3b2..b831ac796 100644 --- a/app/logical/upload_service/replacer.rb +++ b/app/logical/upload_service/replacer.rb @@ -113,9 +113,10 @@ class UploadService post.image_width = upload.image_width post.image_height = upload.image_height post.file_size = upload.file_size - post.source = replacement.replacement_url + post.source = replacement.final_source.presence || replacement.replacement_url post.tag_string = upload.tag_string + rescale_notes(post) update_ugoira_frame_data(post, upload) if md5_changed @@ -124,24 +125,17 @@ class UploadService end end - if replacement.final_source.present? - post.update(source: replacement.final_source) - end - replacement.save! post.save! post.update_iqdb_async - - rescale_notes(post) end def rescale_notes(post) - x_scale = post.image_width.to_f / post.image_width_before_last_save.to_f - y_scale = post.image_height.to_f / post.image_height_before_last_save.to_f + x_scale = post.image_width.to_f / post.image_width_was.to_f + y_scale = post.image_height.to_f / post.image_height_was.to_f post.notes.each do |note| - note.reload note.rescale!(x_scale, y_scale) end end diff --git a/test/models/upload_service_test.rb b/test/models/upload_service_test.rb index f7ae19355..06e462efe 100644 --- a/test/models/upload_service_test.rb +++ b/test/models/upload_service_test.rb @@ -956,14 +956,16 @@ class UploadServiceTest < ActiveSupport::TestCase assert_difference(-> { @note.versions.count }) do # replacement image is 80x82, so we're downscaling by 50% (160x164 -> 80x82). as_user do - @post.replace!(replacement_url: "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg") + @post.replace!( + replacement_url: "https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", + final_source: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350" + ) end @note.reload end - assert_equal([1024, 768, 1024, 768], [@note.x, @note.y, @note.width, @note.height]) - rescue Net::OpenTimeout - skip "Remote connection to Pixiv failed" + assert_equal([40, 41, 40, 41], [@note.x, @note.y, @note.width, @note.height]) + assert_equal("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350", @post.source) end end end