Convert all of file uploads to the new dispatcher

pull/21833/head
Travis Ralston 2021-03-05 13:20:50 -07:00
parent ae9618367e
commit bb80cfb9a6
6 changed files with 123 additions and 22 deletions

View File

@ -32,6 +32,14 @@ import Spinner from "./components/views/elements/Spinner";
import "blueimp-canvas-to-blob"; import "blueimp-canvas-to-blob";
import { Action } from "./dispatcher/actions"; import { Action } from "./dispatcher/actions";
import CountlyAnalytics from "./CountlyAnalytics"; 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_WIDTH = 800;
const MAX_HEIGHT = 600; const MAX_HEIGHT = 600;
@ -44,15 +52,6 @@ export class UploadCanceledError extends Error {}
type ThumbnailableElement = HTMLImageElement | HTMLVideoElement; type ThumbnailableElement = HTMLImageElement | HTMLVideoElement;
interface IUpload {
fileName: string;
roomId: string;
total: number;
loaded: number;
promise: Promise<any>;
canceled?: boolean;
}
interface IMediaConfig { interface IMediaConfig {
"m.upload.size"?: number; "m.upload.size"?: number;
} }
@ -478,7 +477,7 @@ export default class ContentMessages {
if (upload) { if (upload) {
upload.canceled = true; upload.canceled = true;
MatrixClientPeg.get().cancelUpload(upload.promise); MatrixClientPeg.get().cancelUpload(upload.promise);
dis.dispatch({action: 'upload_canceled', upload}); dis.dispatch<UploadCanceledPayload>({action: Action.UploadCanceled, upload});
} }
} }
@ -539,7 +538,7 @@ export default class ContentMessages {
promise: prom, promise: prom,
}; };
this.inprogress.push(upload); this.inprogress.push(upload);
dis.dispatch({action: 'upload_started'}); dis.dispatch<UploadStartedPayload>({action: Action.UploadStarted, upload});
// Focus the composer view // Focus the composer view
dis.fire(Action.FocusComposer); dis.fire(Action.FocusComposer);
@ -547,7 +546,7 @@ export default class ContentMessages {
function onProgress(ev) { function onProgress(ev) {
upload.total = ev.total; upload.total = ev.total;
upload.loaded = ev.loaded; upload.loaded = ev.loaded;
dis.dispatch({action: 'upload_progress', upload: upload}); dis.dispatch<UploadProgressPayload>({action: Action.UploadProgress, upload});
} }
let error; let error;
@ -601,9 +600,9 @@ export default class ContentMessages {
if (error && error.http_status === 413) { if (error && error.http_status === 413) {
this.mediaConfig = null; this.mediaConfig = null;
} }
dis.dispatch({action: 'upload_failed', upload, error}); dis.dispatch<UploadErrorPayload>({action: Action.UploadFailed, upload, error});
} else { } else {
dis.dispatch({action: 'upload_finished', upload}); dis.dispatch<UploadFinishedPayload>({action: Action.UploadFinished, upload});
dis.dispatch({action: 'message_sent'}); dis.dispatch({action: 'message_sent'});
} }
}); });

View File

@ -711,9 +711,9 @@ export default class RoomView extends React.Component<IProps, IState> {
[payload.file], this.state.room.roomId, this.context); [payload.file], this.state.room.roomId, this.context);
break; break;
case 'notifier_enabled': case 'notifier_enabled':
case 'upload_started': case Action.UploadStarted:
case 'upload_finished': case Action.UploadFinished:
case 'upload_canceled': case Action.UploadCanceled:
this.forceUpdate(); this.forceUpdate();
break; break;
case 'call_state': { case 'call_state': {

View File

@ -20,6 +20,8 @@ import dis from "../../dispatcher/dispatcher";
import filesize from "filesize"; import filesize from "filesize";
import { _t } from '../../languageHandler'; import { _t } from '../../languageHandler';
import {Room} from "matrix-js-sdk/src/models/room"; import {Room} from "matrix-js-sdk/src/models/room";
import {ActionPayload} from "../../dispatcher/payloads";
import {Action} from "../../dispatcher/actions";
interface IProps { interface IProps {
room: Room; room: Room;
@ -42,12 +44,12 @@ export default class UploadBar extends React.Component<IProps, IState> {
dis.unregister(this.dispatcherRef); dis.unregister(this.dispatcherRef);
} }
private onAction = (payload) => { private onAction = (payload: ActionPayload) => {
switch (payload.action) { switch (payload.action) {
case 'upload_progress': case Action.UploadProgress:
case 'upload_finished': case Action.UploadFinished:
case 'upload_canceled': case Action.UploadCanceled:
case 'upload_failed': case Action.UploadFailed:
if (this.mounted) this.forceUpdate(); if (this.mounted) this.forceUpdate();
break; break;
} }

View File

@ -113,4 +113,29 @@ export enum Action {
* XXX: Ditto * XXX: Ditto
*/ */
VirtualRoomSupportUpdated = "virtual_room_support_updated", 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",
} }

View File

@ -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;
}

24
src/models/IUpload.ts Normal file
View File

@ -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<any>;
canceled?: boolean;
}