From caf2086585110de7e606e34118e42bda01f234f0 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 21 Jun 2018 10:08:41 +0100 Subject: [PATCH] Restructure limits to be set at RoomView, so they may change if the upload fails --- src/components/structures/RoomView.js | 23 ++++++++++++++++++- src/components/views/rooms/MessageComposer.js | 17 ++++++-------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/components/structures/RoomView.js b/src/components/structures/RoomView.js index 4beafb099c..cb6b94c2e6 100644 --- a/src/components/structures/RoomView.js +++ b/src/components/structures/RoomView.js @@ -49,6 +49,8 @@ import SettingsStore, {SettingLevel} from "../../settings/SettingsStore"; const DEBUG = false; let debuglog = function() {}; +let mediaLimitCache = null; + const BROWSER_SUPPORTS_SANDBOX = 'sandbox' in document.createElement('iframe'); if (DEBUG) { @@ -94,6 +96,9 @@ module.exports = React.createClass({ roomLoading: true, peekLoading: false, shouldPeek: true, + + // Media limits for uploading, this may change. + mediaLimits: {}, // The event to be scrolled to initially initialEventId: null, @@ -147,12 +152,26 @@ module.exports = React.createClass({ MatrixClientPeg.get().on("RoomState.members", this.onRoomStateMember); MatrixClientPeg.get().on("RoomMember.membership", this.onRoomMemberMembership); MatrixClientPeg.get().on("accountData", this.onAccountData); - + this._fetchMediaLimits(); // Start listening for RoomViewStore updates this._roomStoreToken = RoomViewStore.addListener(this._onRoomViewStoreUpdate); this._onRoomViewStoreUpdate(true); }, + _fetchMediaLimits: function(invalidateCache: boolean = false) { + let limits; + if(invalidateCache || mediaLimitCache == null) { + MatrixClientPeg.get().getMediaLimits().then((_limits) => { + limits = _limits; + }).catch(() => { + // Media repo can't or won't report limits, so provide an empty object (no limits). + limits = {}; + }); + mediaLimitCache = limits; + } + this.state.mediaLimits = limits; + }, + _onRoomViewStoreUpdate: function(initial) { if (this.unmounted) { return; @@ -481,6 +500,7 @@ module.exports = React.createClass({ break; case 'notifier_enabled': case 'upload_failed': + this._fetchMediaLimits(true); case 'upload_started': case 'upload_finished': this.forceUpdate(); @@ -1654,6 +1674,7 @@ module.exports = React.createClass({ callState={this.state.callState} disabled={this.props.disabled} showApps={this.state.showApps} + mediaLimits={this.state.mediaLimits} />; } diff --git a/src/components/views/rooms/MessageComposer.js b/src/components/views/rooms/MessageComposer.js index bb8f7d78d2..1a80a0be4f 100644 --- a/src/components/views/rooms/MessageComposer.js +++ b/src/components/views/rooms/MessageComposer.js @@ -65,13 +65,6 @@ export default class MessageComposer extends React.Component { // XXX: fragile as all hell - fixme somehow, perhaps with a dedicated Room.encryption event or something. MatrixClientPeg.get().on("event", this.onEvent); this._roomStoreToken = RoomViewStore.addListener(this._onRoomViewStoreUpdate); - - MatrixClientPeg.get().getMediaLimits().then((limits) => { - this._uploadLimits = limits; - }).catch(() => { - // HS can't or won't provide limits, so don't give any. - this._uploadLimits = {}; - }) } componentWillUnmount() { @@ -110,8 +103,10 @@ export default class MessageComposer extends React.Component { } isFileUploadAllowed(file) { - if (this._uploadLimits.upload_size !== undefined && file.size > this._uploadLimits.upload_size) { - return _t("File is too big. Maximum file size is %(fileSize)s", {fileSize: filesize(this._uploadLimits.upload_size)}); + if (this.props.mediaLimits !== undefined && + this.props.mediaLimits.upload_size !== undefined && + file.size > this.props.mediaLimits.upload_size) { + return _t("File is too big. Maximum file size is %(fileSize)s", {fileSize: filesize(this.mediaLimits.upload_size)}); } return true; } @@ -430,6 +425,8 @@ MessageComposer.propTypes = { // callback when a file to upload is chosen uploadFile: PropTypes.func.isRequired, + mediaLimits: PropTypes.object, + // string representing the current room app drawer state - showApps: PropTypes.bool, + showApps: PropTypes.bool };