diff --git a/app/models/post.rb b/app/models/post.rb index deb29b043..7bb198d09 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1402,7 +1402,7 @@ class Post < ActiveRecord::Base ModAction.log("undeleted post ##{id}") end - def replace!(url) + def replace!(url, replacer = CurrentUser.user) # TODO for posts with notes we need to rescale the notes if the dimensions change. if notes.size > 0 raise NotImplementedError.new("Replacing images with notes not yet supported.") @@ -1436,11 +1436,8 @@ class Post < ActiveRecord::Base self.source = upload.source self.tag_string = upload.tag_string - ModAction.log(<<-EOS.strip_heredoc) - replaced post ##{id}: #{image_width_was}x#{image_height_was} (#{file_size_was.to_formatted_s(:human_size)} #{file_ext_was.upcase}) -> #{image_width}x#{image_height} (#{file_size.to_formatted_s(:human_size)} #{file_ext.upcase}) - source: #{source_was} -> #{source} - md5: "#{md5_was}":[/data/#{md5_was}.#{file_ext_was}] -> "#{md5}":[/data/#{md5}.#{file_ext}] - EOS + comments.create!({creator: User.system, body: presenter.comment_replacement_message(replacer), do_not_bump_post: true}, without_protection: true) + ModAction.log(presenter.modaction_replacement_message) save! end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index eaee5e70e..69d74bb33 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -279,4 +279,53 @@ class PostPresenter < Presenter pool_html << "" pool_html end + + def comment_replacement_message(replacer = CurrentUser.user) + "@#{replacer.name} replaced this post with a new image:\n\n#{replacement_message}" + end + + def modaction_replacement_message + "replaced post ##{@post.id}:\n\n#{replacement_message}" + end + + def replacement_message + linked_source = linked_source(@post.source) + linked_source_was = linked_source(@post.source_was) + + <<-EOS.strip_heredoc + [table] + [tbody] + [tr] + [th]Old[/th] + [td]#{linked_source_was}[/td] + [td]#{@post.md5_was}[/td] + [td]#{@post.file_ext_was}[/td] + [td]#{@post.image_width_was} x #{@post.image_height_was}[/td] + [td]#{@post.file_size_was.to_s(:human_size, precision: 4)}[/td] + [/tr] + [tr] + [th]New[/th] + [td]#{linked_source}[/td] + [td]#{@post.md5}[/td] + [td]#{@post.file_ext}[/td] + [td]#{@post.image_width} x #{@post.image_height}[/td] + [td]#{@post.file_size.to_s(:human_size, precision: 4)}[/td] + [/tr] + [/tbody] + [/table] + EOS + end + +protected + + def linked_source(source) + # truncate long sources in the middle: "www.pixiv.net...lust_id=23264933" + truncated_source = source.gsub(%r{\Ahttps?://}, "").truncate(64, omission: "...#{source.last(32)}") + + if source =~ %r{\Ahttps?://}i + %("#{truncated_source}":[#{source}]) + else + truncated_source + end + end end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 68f45764a..13939b714 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -1593,6 +1593,9 @@ class PostTest < ActiveSupport::TestCase Delayed::Worker.delay_jobs = true # don't delete the old images right away Danbooru.config.stubs(:use_s3_proxy?).returns(false) # don't fail on post ids < 10000 + @system = FactoryGirl.create(:user, created_at: 2.weeks.ago) + Danbooru.config.stubs(:system_user).returns(@system) + @uploader = FactoryGirl.create(:user, created_at: 2.weeks.ago, can_upload_free: true) @replacer = FactoryGirl.create(:user, created_at: 2.weeks.ago, can_approve_posts: true) CurrentUser.user = @replacer @@ -1638,6 +1641,14 @@ class PostTest < ActiveSupport::TestCase should "log a mod action" do assert_match(/replaced post ##{@post.id}/, @mod_action.description) end + + should "leave a system comment" do + comment = @post.comments.last + + assert_not_nil(comment) + assert_equal(User.system.id, comment.creator_id) + assert_match(/@#{@replacer.name} replaced this post/, comment.body) + end end context "replacing a post with a pixiv html source" do