Skip to content

A platform for community discussion. Free, open, simple.

FIX: Prevent failed permanent delete from recovering soft-deleted post (#38793)

Régis Hanol2ca2f44e7d49
diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.js
index 9e124aad854..05c63144ebb 100644
--- a/frontend/discourse/app/controllers/topic.js
+++ b/frontend/discourse/app/controllers/topic.js
@@ -876,7 +876,7 @@ export default class TopicController extends Controller {
if (replies.length === 0) {
return post.destroy(user, opts).catch((error) => {
popupAjaxError(error);
- post.undoDeleteState();
+ post.undoDeleteState(opts);
});
}
@@ -924,7 +924,7 @@ export default class TopicController extends Controller {
action: () => {
post.destroy(user, opts).catch((error) => {
popupAjaxError(error);
- post.undoDeleteState();
+ post.undoDeleteState(opts);
});
},
});
@@ -942,7 +942,7 @@ export default class TopicController extends Controller {
} else {
return post.destroy(user, opts).catch((error) => {
popupAjaxError(error);
- post.undoDeleteState();
+ post.undoDeleteState(opts);
});
}
}
diff --git a/frontend/discourse/app/models/post.js b/frontend/discourse/app/models/post.js
index 40338b82856..94568033978 100644
--- a/frontend/discourse/app/models/post.js
+++ b/frontend/discourse/app/models/post.js
@@ -588,24 +588,30 @@ export default class Post extends RestModel {
This can only be called after setDeletedState was called, but the delete
failed on the server.
**/
- undoDeleteState() {
- if (this.oldCooked) {
- this.setProperties({
- deleted_at: null,
- deleted_by: null,
- cooked: this.oldCooked,
- version: this.version - 1,
- can_recover: false,
- can_delete: true,
- user_deleted: false,
- can_edit: this.oldCanEdit ?? false,
- });
+ undoDeleteState({ force_destroy = false } = {}) {
+ if (force_destroy || !this.oldCooked) {
+ return;
}
+
+ this.setProperties({
+ deleted_at: null,
+ deleted_by: null,
+ cooked: this.oldCooked,
+ version: this.version - 1,
+ can_recover: false,
+ can_delete: true,
+ user_deleted: false,
+ can_edit: this.oldCanEdit ?? false,
+ });
}
destroy(deletedBy, opts) {
- return this.setDeletedState(deletedBy).then(() => {
- return ajax("/posts/" + this.id, {
+ const maybeSetDeletedState = opts?.force_destroy
+ ? Promise.resolve()
+ : this.setDeletedState(deletedBy);
+
+ return maybeSetDeletedState.then(() => {
+ return ajax(`/posts/${this.id}`, {
data: { context: window.location.pathname, ...opts },
type: "DELETE",
});
diff --git a/frontend/discourse/tests/unit/models/post-test.js b/frontend/discourse/tests/unit/models/post-test.js
index aedf57db57e..27ba5baad55 100644
--- a/frontend/discourse/tests/unit/models/post-test.js
+++ b/frontend/discourse/tests/unit/models/post-test.js
@@ -2,6 +2,7 @@ import { getOwner } from "@ember/owner";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import { withPluginApi } from "discourse/lib/plugin-api";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
module("Unit | Model | post", function (hooks) {
setupTest(hooks);
@@ -146,6 +147,45 @@ module("Unit | Model | post", function (hooks) {
assert.strictEqual(post.likeAction, null, "likeAction was reset to null");
});
+ test("undoDeleteState is a no-op when force_destroy", async function (assert) {
+ const user = this.store.createRecord("user", {
+ username: "admin",
+ admin: true,
+ moderator: true,
+ });
+ const post = this.store.createRecord("post", {
+ id: 1,
+ can_delete: true,
+ version: 1,
+ cooked: "original cooked",
+ });
+
+ await post.destroy(user);
+
+ assert.present(post.deleted_at, "post is soft-deleted");
+ const deletedAt = post.deleted_at;
+
+ pretender.delete("/posts/:post_id", () => {
+ return response(403, {
+ errors: ["You must wait 5 minutes"],
+ error_type: "invalid_access",
+ });
+ });
+
+ const opts = { force_destroy: true };
+ try {
+ await post.destroy(user, opts);
+ } catch {
+ post.undoDeleteState(opts);
+ }
+
+ assert.strictEqual(
+ post.deleted_at,
+ deletedAt,
+ "post remains in deleted state after failed permanent delete"
+ );
+ });
+
test("updateFromPost updates localization properties", function (assert) {
const post = this.store.createRecord("post", {
id: 1,