Fix #4946: Don't Add Comments to Posts When Doing Post Replacements.
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
class UploadService
|
||||
class Replacer
|
||||
extend Memoist
|
||||
class Error < StandardError; end
|
||||
|
||||
attr_reader :post, :replacement
|
||||
@@ -12,51 +11,6 @@ class UploadService
|
||||
@replacement = replacement
|
||||
end
|
||||
|
||||
def comment_replacement_message(post, replacement)
|
||||
%{"#{replacement.creator.name}":[#{Routes.user_path(replacement.creator)}] replaced this post with a new file:\n\n#{replacement_message(post, replacement)}}
|
||||
end
|
||||
|
||||
def replacement_message(post, replacement)
|
||||
linked_source = linked_source(replacement.replacement_url)
|
||||
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
|
||||
|
||||
def linked_source(source)
|
||||
return nil if source.nil?
|
||||
|
||||
# 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
|
||||
|
||||
def undo!
|
||||
undo_replacement = post.replacements.create(replacement_url: replacement.original_url)
|
||||
undoer = Replacer.new(post: post, replacement: undo_replacement)
|
||||
@@ -99,8 +53,6 @@ class UploadService
|
||||
|
||||
rescale_notes(post)
|
||||
|
||||
CurrentUser.scoped(User.system) { Comment.create!(post: post, creator: User.system, updater: User.system, body: comment_replacement_message(post, replacement), do_not_bump_post: true, creator_ip_addr: "127.0.0.1") }
|
||||
|
||||
replacement.save!
|
||||
post.save!
|
||||
|
||||
|
||||
@@ -224,12 +224,6 @@ class UploadServiceTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
context "#process!" do
|
||||
should "create a comment" do
|
||||
assert_difference(-> { @post.reload.comments.count }) do
|
||||
as(@user) { @post.reload.replace!(replacement_url: "", replacement_file: @new_file) }
|
||||
end
|
||||
end
|
||||
|
||||
should "not create a new post" do
|
||||
assert_difference(-> { Post.count }, 0) do
|
||||
as(@user) { @post.reload.replace!(replacement_url: "", replacement_file: @new_file) }
|
||||
@@ -400,12 +394,6 @@ class UploadServiceTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
context "#process!" do
|
||||
should "create a comment" do
|
||||
assert_difference(-> { @post.reload.comments.count }) do
|
||||
as(@user) { @post.reload.replace!(replacement_url: @new_url) }
|
||||
end
|
||||
end
|
||||
|
||||
should "not create a new post" do
|
||||
assert_difference(-> { Post.count }, 0) do
|
||||
as(@user) { @post.reload.replace!(replacement_url: @new_url) }
|
||||
@@ -431,14 +419,6 @@ class UploadServiceTest < ActiveSupport::TestCase
|
||||
@post.reload
|
||||
end
|
||||
end
|
||||
|
||||
should "leave a system comment" do
|
||||
as(@user) { @post.reload.replace!(replacement_url: @new_url) }
|
||||
comment = @post.comments.last
|
||||
assert_not_nil(comment)
|
||||
assert_equal(User.system.id, comment.creator_id)
|
||||
assert_match(/replaced this post/, comment.body)
|
||||
end
|
||||
end
|
||||
|
||||
context "a post with a pixiv html source" do
|
||||
|
||||
Reference in New Issue
Block a user