A platform for community discussion. Free, open, simple.
FIX: Stop successful uploads from reporting cancellation (#44153)
Alan Guo Xiang Tand388a1cdda6e
diff --git a/frontend/discourse/app/lib/uppy/composer-upload.js b/frontend/discourse/app/lib/uppy/composer-upload.jsindex fe8822cfb63..8d522220ec9 100644--- a/frontend/discourse/app/lib/uppy/composer-upload.js+++ b/frontend/discourse/app/lib/uppy/composer-upload.js@@ -263,28 +263,6 @@ export default class UppyComposerUpload {});});- this.uppyWrapper.uppyInstance.on("file-removed", (file, reason) => {- run(() => {- // we handle the cancel-all event specifically, so no need- // to do anything here. this event is also fired when some files- // are handled by an upload handler- if (reason === "cancel-all") {- return;- }- this.appEvents.trigger(- `${this.composerEventPrefix}:upload-cancelled`,- file.id- );- file.meta.cancelled = true;- this.#removeInProgressUpload(file.id);- this.#resetUpload(file);- if (this.#inProgressUploads.length === 0) {- this.#userCancelled = true;- this.uppyWrapper.uppyInstance.cancelAll();- }- });- });-this.uppyWrapper.uppyInstance.on("upload-progress", (file, progress) => {run(() => {if (this.isDestroying) {@@ -553,7 +531,23 @@ export default class UppyComposerUpload {_cancelUpload(data) {if (data) {// Single file- this.uppyWrapper.uppyInstance.removeFile(data.fileId);+ const file = this.uppyWrapper.uppyInstance.getFile(data.fileId);+ if (!file) {+ return;+ }++ file.meta.cancelled = true;+ this.uppyWrapper.uppyInstance.removeFile(file.id);+ this.appEvents.trigger(+ `${this.composerEventPrefix}:upload-cancelled`,+ file.id+ );+ this.#removeInProgressUpload(file.id);+ this.#resetUpload(file);+ if (this.#inProgressUploads.length === 0) {+ this.#userCancelled = true;+ this.uppyWrapper.uppyInstance.cancelAll();+ }} else {// All filesthis.#userCancelled = true;diff --git a/frontend/discourse/app/lib/uppy/uppy-upload.js b/frontend/discourse/app/lib/uppy/uppy-upload.jsindex abda3ce6d8d..9766edc7f7c 100644--- a/frontend/discourse/app/lib/uppy/uppy-upload.js+++ b/frontend/discourse/app/lib/uppy/uppy-upload.js@@ -316,21 +316,6 @@ export default class UppyUpload {});- this.uppyWrapper.uppyInstance.on("file-removed", (file, reason) => {- run(() => {- // we handle the cancel-all event specifically, so no need- // to do anything here. this event is also fired when some files- // are handled by an upload handler- if (reason === "cancel-all") {- return;- }- this.appEvents.trigger(- `upload-mixin:${this.config.id}:upload-cancelled`,- file.id- );- });- });-if (this.siteSettings.enable_upload_debug_mode) {this.uppyWrapper.debug.instrumentUploadTimings(this.uppyWrapper.uppyInstance@@ -366,10 +351,6 @@ export default class UppyUpload {}this.uppyWrapper.uppyInstance.on("cancel-all", () => {- this.appEvents.trigger(- `upload-mixin:${this.config.id}:uploads-cancelled`- );-if (this.inProgressUploads.length) {this.inProgressUploads.length = 0; // Clear array in-placethis.#triggerInProgressUploadsEvent();@@ -416,14 +397,25 @@ export default class UppyUpload {@bindcancelSingleUpload(data) {- this.uppyWrapper.uppyInstance.removeFile(data.fileId);+ if (this.uppyWrapper.uppyInstance.getFile(data.fileId)) {+ this.uppyWrapper.uppyInstance.removeFile(data.fileId);+ this.appEvents.trigger(+ `upload-mixin:${this.config.id}:upload-cancelled`,+ data.fileId+ );+ }this.#removeInProgressUpload(data.fileId);this.#finishBatch();}@bindcancelAllUploads() {- this.uppyWrapper.uppyInstance?.cancelAll();+ if (this.uppyWrapper.uppyInstance) {+ this.uppyWrapper.uppyInstance.cancelAll();+ this.appEvents.trigger(+ `upload-mixin:${this.config.id}:uploads-cancelled`+ );+ }this.inProgressUploads.length = 0;this.#triggerInProgressUploadsEvent();this.#finishBatch();diff --git a/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.js b/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.jsindex 5c844f39cd9..d71022c4a2a 100644--- a/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.js+++ b/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.js@@ -57,7 +57,10 @@ const extension = {const isUserAction = transactions.some((tr) => tr.docChanged && tr.getMeta("addToHistory") !== false);- if (!isUserAction) {+ const resolvesUpload = transactions.some((tr) =>+ tr.getMeta("uploadPlaceholderResolved")+ );+ if (!isUserAction || resolvesUpload) {return;}diff --git a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.ts b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.tsindex 5f84808b646..3d1d4b218d2 100644--- a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.ts+++ b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.ts@@ -883,7 +883,7 @@ class ProsemirrorPlaceholderHandler implements PlaceholderHandler {}}- this.view.dispatch(tr);+ this.view.dispatch(tr.setMeta("uploadPlaceholderResolved", true));}#revokeBlobUrl(node: Node): void {diff --git a/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js b/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.jsindex cf6aa233485..f11542d2511 100644--- a/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js+++ b/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js@@ -7,6 +7,7 @@ import {focus,settled,triggerEvent,+ triggerKeyEvent,visit,waitFor,} from "@ember/test-helpers";@@ -104,6 +105,14 @@ acceptance("Uppy Composer Attachment - Upload Placeholder", function (needs) {await fillIn(".d-editor-input", "The image:\n");const appEvents = getOwner(this).lookup("service:app-events");const done = assert.async();+ const cancellations = [];++ appEvents.on("composer:upload-cancelled", () =>+ cancellations.push("upload-cancelled")+ );+ appEvents.on("composer:uploads-cancelled", () =>+ cancellations.push("uploads-cancelled")+ );appEvents.on("composer:all-uploads-complete", async () => {await settled();@@ -112,6 +121,11 @@ acceptance("Uppy Composer Attachment - Upload Placeholder", function (needs) {.hasValue("The image:\n\n");+ assert.deepEqual(+ cancellations,+ [],+ "the successful upload is not reported as cancelled"+ );done();});@@ -577,6 +591,71 @@ acceptance("Uppy Composer Attachment - Auto Image Grid", function (needs) {});});+acceptance("Uppy Composer Attachment - Rich Editor", function (needs) {+ needs.user({ "user_option.composition_mode": 1 });+ needs.pretender(pretender);+ needs.settings({ allow_uncategorized_topics: true });+ needs.hooks.afterEach(() => {+ uploadNumber = 1;+ });++ test("does not cancel an upload once it succeeds", async function (assert) {+ await visit("/new-topic");++ const appEvents = getOwner(this).lookup("service:app-events");+ const cancellations = [];+ appEvents.on("composer:upload-cancelled", () =>+ cancellations.push("upload-cancelled")+ );+ appEvents.on("composer:uploads-cancelled", () =>+ cancellations.push("uploads-cancelled")+ );+ const uploadsComplete = new Promise((resolve) => {+ appEvents.one("composer:all-uploads-complete", resolve);+ });++ appEvents.trigger("composer:add-files", createFile("avatar.png"));+ await uploadsComplete;+ await settled();++ assert+ .dom(".composer-image-node img")+ .hasAttribute(+ "data-orig-src",+ "upload://yoj8pf9DdIeHRRULyw7i57GAYdz.jpeg",+ "the uploaded image replaces the placeholder"+ );+ assert.deepEqual(+ cancellations,+ [],+ "the successful upload is not reported as cancelled"+ );+ });++ test("cancels the upload when its placeholder is deleted", async function (assert) {+ await visit("/new-topic");++ const appEvents = getOwner(this).lookup("service:app-events");+ const uploadCancelled = new Promise((resolve) => {+ appEvents.one("composer:upload-cancelled", resolve);+ });++ appEvents.trigger("composer:add-files", createFile("avatar.png"));+ await waitFor(".upload-placeholder.--image img");+ await click(".upload-placeholder.--image img");+ await triggerKeyEvent(".ProseMirror", "keydown", "Backspace");+ await uploadCancelled;+ await settled();++ assert+ .dom(".upload-placeholder")+ .doesNotExist("the deleted placeholder stays removed");+ assert+ .dom(".composer-image-node img")+ .doesNotExist("the cancelled upload is not inserted");+ });+});+acceptance("Uppy Composer Attachment - Upload Error", function (needs) {needs.user();needs.pretender((server, helper) => {diff --git a/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.js b/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.jsindex e3b6e6a5f0e..7bbc20a2170 100644--- a/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.js+++ b/frontend/discourse/tests/unit/lib/uppy/uppy-upload-test.js@@ -100,6 +100,111 @@ module("Unit | Lib | uppy/uppy-upload", function (hooks) {upload.teardown();});+ test("a successful upload is not reported as cancelled", async function (assert) {+ const completed = [];+ const cancelled = [];++ pretender.post(UPLOAD_URL, (request) =>+ uploadResponse(request.requestBody.get("file").name)+ );++ const upload = new UppyUpload(getOwner(this), {+ id: "uppy-upload-success-test",+ type: "composer",+ uploadDone: ({ file_name }) => completed.push(file_name),+ });++ upload.setup(this.fileInput);+ getOwner(this)+ .lookup("service:app-events")+ .on("upload-mixin:uppy-upload-success-test:upload-cancelled", (fileId) =>+ cancelled.push(fileId)+ );+ getOwner(this)+ .lookup("service:app-events")+ .on("upload-mixin:uppy-upload-success-test:uploads-cancelled", () =>+ cancelled.push("uploads-cancelled")+ );++ await upload.addFiles([createFile(SIBLING_FILE)]);++ await waitUntil(() => !upload.uploading && completed.length > 0, {+ timeout: 5000,+ });++ assert.deepEqual(completed, [SIBLING_FILE], "the upload finishes");+ assert.deepEqual(+ cancelled,+ [],+ "the finished upload is not reported as cancelled"+ );++ upload.teardown();+ });++ test("explicit single-file cancellation is reported", async function (assert) {+ const cancellations = [];+ const upload = new UppyUpload(getOwner(this), {+ id: "uppy-upload-cancel-single-test",+ type: "composer",+ autoStartUploads: false,+ uploadDone: () => {},+ });++ upload.setup(this.fileInput);+ getOwner(this)+ .lookup("service:app-events")+ .on(+ "upload-mixin:uppy-upload-cancel-single-test:upload-cancelled",+ (fileId) => cancellations.push(fileId)+ );++ await upload.addFiles([createFile(SIBLING_FILE)]);+ const fileId = upload.uppyWrapper.uppyInstance.getFiles()[0].id;+ upload.cancelSingleUpload({ fileId });++ assert.deepEqual(+ cancellations,+ [fileId],+ "explicit cancellation reports the removed file once"+ );+ assert.deepEqual(+ upload.uppyWrapper.uppyInstance.getFiles(),+ [],+ "the cancelled file is removed"+ );++ upload.teardown();+ });++ test("explicit cancel all is reported as cancelled", async function (assert) {+ const cancellations = [];+ const upload = new UppyUpload(getOwner(this), {+ id: "uppy-upload-cancel-all-test",+ type: "composer",+ autoStartUploads: false,+ uploadDone: () => {},+ });++ upload.setup(this.fileInput);+ getOwner(this)+ .lookup("service:app-events")+ .on("upload-mixin:uppy-upload-cancel-all-test:uploads-cancelled", () =>+ cancellations.push("uploads-cancelled")+ );++ await upload.addFiles([createFile(SIBLING_FILE)]);+ upload.cancelAllUploads();++ assert.deepEqual(+ cancellations,+ ["uploads-cancelled"],+ "explicit cancel all reports one cancellation"+ );++ upload.teardown();+ });+test("a batch drained by a cancel still reports the failure", async function (assert) {const alert = sinon.stub(dialog, "alert");