From 9f1096e67f6c0670603847bcfc4e7c8a153326ff Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 17 May 2017 00:14:20 -0500 Subject: [PATCH] post replacement: rescale notes. --- app/models/note.rb | 8 ++++++++ app/models/post_replacement.rb | 15 ++++++++++----- test/unit/post_replacement_test.rb | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/models/note.rb b/app/models/note.rb index 299498ab8..57418f6ea 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -127,6 +127,14 @@ class Note < ActiveRecord::Base User.id_to_name(creator_id) end + def rescale!(x_scale, y_scale) + self.x *= x_scale + self.y *= y_scale + self.width *= x_scale + self.height *= y_scale + save! + end + def update_post if self.changed? if Note.where(:is_active => true, :post_id => post_id).exists? diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 5ff7f9183..9cd52a02b 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -17,11 +17,6 @@ class PostReplacement < ActiveRecord::Base end def process! - # TODO for posts with notes we need to rescale the notes if the dimensions change. - if post.notes.any? - raise NotImplementedError.new("Replacing images with notes not yet supported.") - end - # TODO for ugoiras we need to replace the frame data. if post.is_ugoira? raise NotImplementedError.new("Replacing ugoira images not yet supported.") @@ -49,6 +44,7 @@ class PostReplacement < ActiveRecord::Base post.file_size = upload.file_size post.source = upload.source post.tag_string = upload.tag_string + rescale_notes post.comments.create!({creator: User.system, body: comment_replacement_message, do_not_bump_post: true}, without_protection: true) ModAction.log(modaction_replacement_message) @@ -62,6 +58,15 @@ class PostReplacement < ActiveRecord::Base post.update_iqdb_async end + def rescale_notes + 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.rescale!(x_scale, y_scale) + end + end + module SearchMethods def search(params = {}) q = all diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index 8b827b133..3bba669f6 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -102,6 +102,27 @@ class PostReplacementTest < ActiveSupport::TestCase end end + context "a post with notes" do + setup do + @post.update({image_width: 160, image_height: 164}, without_protection: true) + CurrentUser.scoped(@uploader, "127.0.0.1") do + @note = @post.notes.create(x: 80, y: 82, width: 80, height: 82, body: "test") + end + end + + should "rescale the notes" do + assert_equal([80, 82, 80, 82], [@note.x, @note.y, @note.width, @note.height]) + + assert_difference("@replacer.note_versions.count") do + # replacement image is 80x82, so we're downscaling by 50% (160x164 -> 80x82). + @post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350") + @note.reload + end + + assert_equal([40, 41, 40, 41], [@note.x, @note.y, @note.width, @note.height]) + end + end + context "a post with a pixiv html source" do should "replace with the full size image" do @post.replace!(replacement_url: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350")