A platform for community discussion. Free, open, simple.
FIX: Remove nameless uploads from deleted posts (#36484)
Gabriel Grubba6c9ef61520e4
diff --git a/plugins/automation/lib/discourse_automation/scripts/remove_upload_markup_from_deleted_posts.rb b/plugins/automation/lib/discourse_automation/scripts/remove_upload_markup_from_deleted_posts.rbindex ebab4736805..df07f1bc99a 100644--- a/plugins/automation/lib/discourse_automation/scripts/remove_upload_markup_from_deleted_posts.rb+++ b/plugins/automation/lib/discourse_automation/scripts/remove_upload_markup_from_deleted_posts.rb@@ -16,9 +16,12 @@ DiscourseAutomation::Scriptable.add(edit_reason =I18n.t("discourse_automation.scriptables.remove_upload_markup_from_deleted_posts.edit_reason")- # it matches both  and [small.pdf|attachment](upload://key.pdf) (Number Bytes)+ # it matches:+ # + # [small.pdf|attachment](upload://key.pdf) (Number Bytes)+ # upload_and_attachment_regex =- %r{!?\[([^\]|]+)(?:\|[^\]]*)?\]\(upload://([A-Za-z0-9_-]+)[^)]*\)(?:\s*\([^)]*\))?}+ %r{!?\[([^\]|]*)(?:\|[^\]]*)?\]\(upload://([A-Za-z0-9_-]+)[^)]*\)(?:\s*\([^)]*\))?}Post.with_deleteddiff --git a/plugins/automation/spec/scripts/remove_upload_markup_from_deleted_posts_spec.rb b/plugins/automation/spec/scripts/remove_upload_markup_from_deleted_posts_spec.rbindex 6c8fa261a5f..de74f5cb0e9 100644--- a/plugins/automation/spec/scripts/remove_upload_markup_from_deleted_posts_spec.rb+++ b/plugins/automation/spec/scripts/remove_upload_markup_from_deleted_posts_spec.rb@@ -3,6 +3,7 @@describe "RemoveUploadMarkupFromDeletedPosts" dofab!(:topic)fab!(:upload)+ fab!(:nameless_upload, :upload)fab!(:filename) { "small.pdf" }fab!(:file) { file_from_fixtures(filename, "pdf") }@@ -11,13 +12,25 @@ describe "RemoveUploadMarkupFromDeletedPosts" doDiscourse.system_user.id,)end+let!(:raw) do- "Hey it is a regular post with a link to [Discourse](https://www.discourse.org) and a #{upload.to_markdown} #{file_upload.to_markdown}"+ " Hey it is a regular post with a link to [Discourse](https://www.discourse.org) and a #{upload.to_markdown} #{file_upload.to_markdown}"+ end++ let!(:expected_raw) do+ " Hey it is a regular post with a link to [Discourse](https://www.discourse.org) and a"endlet!(:post) { Fabricate(:post, topic: topic, raw: raw) }let!(:deleted_post) { Fabricate(:post, topic: topic, raw: raw, deleted_at: 1.month.ago) }+ let!(:nameless_upload_reference) do+ Fabricate(:upload_reference, upload: nameless_upload, target: post)+ end+ let!(:deleted_nameless_upload_reference) do+ Fabricate(:upload_reference, upload: nameless_upload, target: deleted_post)+ end+let!(:upload_reference) { Fabricate(:upload_reference, upload: upload, target: post) }let!(:deleted_upload_reference) doFabricate(:upload_reference, upload: upload, target: deleted_post)@@ -43,9 +56,7 @@ describe "RemoveUploadMarkupFromDeletedPosts" doexpect {automation.trigger!deleted_post.reload- }.to change { deleted_post.raw }.from(raw).to(- "Hey it is a regular post with a link to [Discourse](https://www.discourse.org) and a",- )+ }.to change { deleted_post.raw }.from(raw).to(expected_raw)expect(post.raw).to eq(raw)end@@ -57,9 +68,7 @@ describe "RemoveUploadMarkupFromDeletedPosts" doexpect {automation.trigger!deleted_post.reload- }.to change { deleted_post.raw }.from(raw).to(- "Hey it is a regular post with a link to [Discourse](https://www.discourse.org) and a",- )+ }.to change { deleted_post.raw }.from(raw).to(expected_raw)endit "does not remove uploads from non-deleted posts" do@@ -155,9 +164,7 @@ describe "RemoveUploadMarkupFromDeletedPosts" doexpect {Jobs::DiscourseAutomation::Tracker.new.executedeleted_post.reload- }.to change { deleted_post.raw }.from(raw).to(- "Hey it is a regular post with a link to [Discourse](https://www.discourse.org) and a",- )+ }.to change { deleted_post.raw }.from(raw).to(expected_raw)expect(post.raw).to eq(raw)end