Skip to content

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

FIX: check if saved draft is an edited post (#43680)

David Battersbybc06b6d1340a
diff --git a/frontend/discourse/app/controllers/nested.js b/frontend/discourse/app/controllers/nested.js
index 3932e797444..190db64a2ba 100644
--- a/frontend/discourse/app/controllers/nested.js
+++ b/frontend/discourse/app/controllers/nested.js
@@ -405,8 +405,8 @@ export default class NestedController extends Controller {
}
@action
- editPost(post) {
- this.#topicController.editPost(post);
+ async editPost(post) {
+ await this.#topicController.editPost(post);
this.composer.set("skipJumpOnSave", true);
}
diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.js
index c414211a1cc..dcc736b63dd 100644
--- a/frontend/discourse/app/controllers/topic.js
+++ b/frontend/discourse/app/controllers/topic.js
@@ -918,6 +918,11 @@ export default class TopicController extends Controller {
} else {
opts.reply = data.reply;
}
+
+ if (Composer.isEditDraft(data)) {
+ opts.draft = { ...data, reply: opts.reply };
+ opts.topic = topic;
+ }
} else if (quotedText) {
opts.quote = quotedText;
}
@@ -2013,7 +2018,7 @@ export default class TopicController extends Controller {
this.set("editingTopic", true);
}
- _openComposerForEdit(topic, post) {
+ async _openComposerForEdit(topic, post) {
let editingSharedDraft = false;
let draftsCategoryId = this.get("site.shared_drafts_category_id");
if (draftsCategoryId && draftsCategoryId === topic.get("category.id")) {
@@ -2038,7 +2043,20 @@ export default class TopicController extends Controller {
opts.action === composerModel?.action &&
opts.draftKey === composerModel?.draftKey;
- return editingSamePost ? composer.unshrink() : composer.open(opts);
+ if (editingSamePost) {
+ return composer.unshrink();
+ }
+
+ const draftData = await Draft.get(opts.draftKey);
+ const data = draftData.draft && JSON.parse(draftData.draft);
+
+ if (Composer.isEditDraft(data) && data.postId === post.id) {
+ opts.draft = data;
+ opts.draftSequence = draftData.draft_sequence;
+ opts.topic = topic;
+ }
+
+ return composer.open(opts);
}
async _openComposerForEditTranslation(topic, post) {
diff --git a/frontend/discourse/app/models/composer.js b/frontend/discourse/app/models/composer.js
index 8bb7e1a8a2b..3d15284f68e 100644
--- a/frontend/discourse/app/models/composer.js
+++ b/frontend/discourse/app/models/composer.js
@@ -206,6 +206,10 @@ export default class Composer extends RestModel {
return Object.keys(_draft_serializer);
}
+ static isEditDraft(draft) {
+ return isEdit(draft?.action) && !!draft.postId;
+ }
+
@service dialog;
@service siteSettings;
@service currentUser;
diff --git a/spec/system/composer/drafts_spec.rb b/spec/system/composer/drafts_spec.rb
index 41e18c393c6..ee2fe35c7e5 100644
--- a/spec/system/composer/drafts_spec.rb
+++ b/spec/system/composer/drafts_spec.rb
@@ -289,6 +289,48 @@ describe "Composer - Drafts" do
end
end
+ context "when resuming a saved edit draft" do
+ fab!(:post_1) { Fabricate(:post, topic:, user: current_user, raw: "original post content") }
+ fab!(:post_2) { Fabricate(:post, topic:, user: current_user, raw: "another post content") }
+
+ before do
+ topic_page.visit_topic(topic)
+ topic_page.click_post_action_button(post_1, :edit)
+ composer.fill_content("edited post content")
+ composer.close
+
+ expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
+ end
+
+ it "lets the user finish their edit after clicking reply" do
+ topic_page.click_reply_button
+
+ expect(composer).to have_content("edited post content")
+ expect(composer.button_label).to have_text(I18n.t("js.composer.save_edit"))
+
+ composer.create
+
+ expect(composer).to be_closed
+ expect(topic_page).to have_post_content(
+ post_number: post_1.post_number,
+ content: "edited post content",
+ )
+ end
+
+ it "shows the user their saved edit when they edit the same post again" do
+ topic_page.click_post_action_button(post_1, :edit)
+
+ expect(composer).to have_content("edited post content")
+ expect(composer.button_label).to have_text(I18n.t("js.composer.save_edit"))
+ end
+
+ it "shows the user the original content when they edit a different post" do
+ topic_page.click_post_action_button(post_2, :edit)
+
+ expect(composer).to have_content("another post content")
+ end
+ end
+
context "when replying to a different topic with an active draft" do
fab!(:other_topic, :topic_with_op)