post replacement: rescale notes.

This commit is contained in:
evazion
2017-05-17 00:14:20 -05:00
parent 302bc52bf8
commit 9f1096e67f
3 changed files with 39 additions and 5 deletions

View File

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

View File

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

View File

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