From bb80cfb9a66fd26af9571b8be3c0979d3cb944a6 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 5 Mar 2021 13:20:50 -0700 Subject: [PATCH] Convert all of file uploads to the new dispatcher --- src/ContentMessages.tsx | 27 ++++++------- src/components/structures/RoomView.tsx | 6 +-- src/components/structures/UploadBar.tsx | 12 +++--- src/dispatcher/actions.ts | 25 ++++++++++++ src/dispatcher/payloads/UploadPayload.ts | 51 ++++++++++++++++++++++++ src/models/IUpload.ts | 24 +++++++++++ 6 files changed, 123 insertions(+), 22 deletions(-) create mode 100644 src/dispatcher/payloads/UploadPayload.ts create mode 100644 src/models/IUpload.ts diff --git a/src/ContentMessages.tsx b/src/ContentMessages.tsx index bec36d49f6..95b45cce4a 100644 --- a/src/ContentMessages.tsx +++ b/src/ContentMessages.tsx @@ -32,6 +32,14 @@ import Spinner from "./components/views/elements/Spinner"; import "blueimp-canvas-to-blob"; import { Action } from "./dispatcher/actions"; import CountlyAnalytics from "./CountlyAnalytics"; +import { + UploadCanceledPayload, + UploadErrorPayload, + UploadFinishedPayload, + UploadProgressPayload, + UploadStartedPayload, +} from "./dispatcher/payloads/UploadPayload"; +import {IUpload} from "./models/IUpload"; const MAX_WIDTH = 800; const MAX_HEIGHT = 600; @@ -44,15 +52,6 @@ export class UploadCanceledError extends Error {} type ThumbnailableElement = HTMLImageElement | HTMLVideoElement; -interface IUpload { - fileName: string; - roomId: string; - total: number; - loaded: number; - promise: Promise; - canceled?: boolean; -} - interface IMediaConfig { "m.upload.size"?: number; } @@ -478,7 +477,7 @@ export default class ContentMessages { if (upload) { upload.canceled = true; MatrixClientPeg.get().cancelUpload(upload.promise); - dis.dispatch({action: 'upload_canceled', upload}); + dis.dispatch({action: Action.UploadCanceled, upload}); } } @@ -539,7 +538,7 @@ export default class ContentMessages { promise: prom, }; this.inprogress.push(upload); - dis.dispatch({action: 'upload_started'}); + dis.dispatch({action: Action.UploadStarted, upload}); // Focus the composer view dis.fire(Action.FocusComposer); @@ -547,7 +546,7 @@ export default class ContentMessages { function onProgress(ev) { upload.total = ev.total; upload.loaded = ev.loaded; - dis.dispatch({action: 'upload_progress', upload: upload}); + dis.dispatch({action: Action.UploadProgress, upload}); } let error; @@ -601,9 +600,9 @@ export default class ContentMessages { if (error && error.http_status === 413) { this.mediaConfig = null; } - dis.dispatch({action: 'upload_failed', upload, error}); + dis.dispatch({action: Action.UploadFailed, upload, error}); } else { - dis.dispatch({action: 'upload_finished', upload}); + dis.dispatch({action: Action.UploadFinished, upload}); dis.dispatch({action: 'message_sent'}); } }); diff --git a/src/components/structures/RoomView.tsx b/src/components/structures/RoomView.tsx index 96808e651e..90f6daf6cb 100644 --- a/src/components/structures/RoomView.tsx +++ b/src/components/structures/RoomView.tsx @@ -711,9 +711,9 @@ export default class RoomView extends React.Component { [payload.file], this.state.room.roomId, this.context); break; case 'notifier_enabled': - case 'upload_started': - case 'upload_finished': - case 'upload_canceled': + case Action.UploadStarted: + case Action.UploadFinished: + case Action.UploadCanceled: this.forceUpdate(); break; case 'call_state': { diff --git a/src/components/structures/UploadBar.tsx b/src/components/structures/UploadBar.tsx index 27d6746698..f60e28b333 100644 --- a/src/components/structures/UploadBar.tsx +++ b/src/components/structures/UploadBar.tsx @@ -20,6 +20,8 @@ import dis from "../../dispatcher/dispatcher"; import filesize from "filesize"; import { _t } from '../../languageHandler'; import {Room} from "matrix-js-sdk/src/models/room"; +import {ActionPayload} from "../../dispatcher/payloads"; +import {Action} from "../../dispatcher/actions"; interface IProps { room: Room; @@ -42,12 +44,12 @@ export default class UploadBar extends React.Component { dis.unregister(this.dispatcherRef); } - private onAction = (payload) => { + private onAction = (payload: ActionPayload) => { switch (payload.action) { - case 'upload_progress': - case 'upload_finished': - case 'upload_canceled': - case 'upload_failed': + case Action.UploadProgress: + case Action.UploadFinished: + case Action.UploadCanceled: + case Action.UploadFailed: if (this.mounted) this.forceUpdate(); break; } diff --git a/src/dispatcher/actions.ts b/src/dispatcher/actions.ts index 12bf4c57a3..cd32c3743f 100644 --- a/src/dispatcher/actions.ts +++ b/src/dispatcher/actions.ts @@ -113,4 +113,29 @@ export enum Action { * XXX: Ditto */ VirtualRoomSupportUpdated = "virtual_room_support_updated", + + /** + * Fired when an upload has started. Should be used with UploadStartedPayload. + */ + UploadStarted = "upload_started", + + /** + * Fired when an upload makes progress. Should be used with UploadProgressPayload. + */ + UploadProgress = "upload_progress", + + /** + * Fired when an upload is completed. Should be used with UploadFinishedPayload. + */ + UploadFinished = "upload_finished", + + /** + * Fired when an upload fails. Should be used with UploadErrorPayload. + */ + UploadFailed = "upload_failed", + + /** + * Fired when an upload is cancelled by the user. Should be used with UploadCanceledPayload. + */ + UploadCanceled = "upload_canceled", } diff --git a/src/dispatcher/payloads/UploadPayload.ts b/src/dispatcher/payloads/UploadPayload.ts new file mode 100644 index 0000000000..40c2710dd6 --- /dev/null +++ b/src/dispatcher/payloads/UploadPayload.ts @@ -0,0 +1,51 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { ActionPayload } from "../payloads"; +import { Action } from "../actions"; +import {IUpload} from "../../models/IUpload"; + +interface UploadPayload extends ActionPayload { + /** + * The upload with fields representing the new upload state. + */ + upload: IUpload; +} + +export interface UploadStartedPayload extends UploadPayload { + action: Action.UploadStarted; +} + +export interface UploadProgressPayload extends UploadPayload { + action: Action.UploadProgress; +} + +export interface UploadErrorPayload extends UploadPayload { + action: Action.UploadFailed; + + /** + * An error to describe what went wrong with the upload. + */ + error: Error; +} + +export interface UploadFinishedPayload extends UploadPayload { + action: Action.UploadFinished; +} + +export interface UploadCanceledPayload extends UploadPayload { + action: Action.UploadCanceled; +} diff --git a/src/models/IUpload.ts b/src/models/IUpload.ts new file mode 100644 index 0000000000..5b376e9330 --- /dev/null +++ b/src/models/IUpload.ts @@ -0,0 +1,24 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +export interface IUpload { + fileName: string; + roomId: string; + total: number; + loaded: number; + promise: Promise; + canceled?: boolean; +}