Skip to content

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.rb
index 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 ![alt|size](upload://key) and [small.pdf|attachment](upload://key.pdf) (Number Bytes)
+ # it matches:
+ # ![alt|size](upload://key)
+ # [small.pdf|attachment](upload://key.pdf) (Number Bytes)
+ # ![](upload://key.png)
upload_and_attachment_regex =
- %r{!?\[([^\]|]+)(?:\|[^\]]*)?\]\(upload://([A-Za-z0-9_-]+)[^)]*\)(?:\s*\([^)]*\))?}
+ %r{!?\[([^\]|]*)(?:\|[^\]]*)?\]\(upload://([A-Za-z0-9_-]+)[^)]*\)(?:\s*\([^)]*\))?}
Post
.with_deleted
diff --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.rb
index 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" do
fab!(:topic)
fab!(:upload)
+ fab!(:nameless_upload, :upload)
fab!(:filename) { "small.pdf" }
fab!(:file) { file_from_fixtures(filename, "pdf") }
@@ -11,13 +12,25 @@ describe "RemoveUploadMarkupFromDeletedPosts" do
Discourse.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}"
+ "![](#{nameless_upload.short_url}) 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"
end
let!(: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) do
Fabricate(:upload_reference, upload: upload, target: deleted_post)
@@ -43,9 +56,7 @@ describe "RemoveUploadMarkupFromDeletedPosts" do
expect {
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" do
expect {
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)
end
it "does not remove uploads from non-deleted posts" do
@@ -155,9 +164,7 @@ describe "RemoveUploadMarkupFromDeletedPosts" do
expect {
Jobs::DiscourseAutomation::Tracker.new.execute
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