mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-17 14:05:04 +08:00
Defer IM token until widget is shown and permission granted
This defers the work to request the IM token when displaying a widget until after we know that widget will actually be shown and the user has granted permission for this widget to be displayed. This is useful in general to avoid making unnecessary token checks, but it's particularly helpful with the new Terms API, so that we only show the Terms flow when widget are actually being used (as opposed to entering the room where widgets exist, but haven't been shown / permitted). Part of https://github.com/vector-im/riot-web/issues/10088
This commit is contained in:
parent
84e2333105
commit
09c2d899ac
@ -139,8 +139,11 @@ export default class AppTile extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
|
// Only fetch IM token on mount if we're showing and have permission to load
|
||||||
|
if (this.props.show && this.state.hasPermissionToLoad) {
|
||||||
this.setScalarToken();
|
this.setScalarToken();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
// Widget action listeners
|
// Widget action listeners
|
||||||
@ -164,8 +167,6 @@ export default class AppTile extends React.Component {
|
|||||||
* Component initialisation is only complete when this function has resolved
|
* Component initialisation is only complete when this function has resolved
|
||||||
*/
|
*/
|
||||||
setScalarToken() {
|
setScalarToken() {
|
||||||
this.setState({initialising: true});
|
|
||||||
|
|
||||||
if (!WidgetUtils.isScalarUrl(this.props.url)) {
|
if (!WidgetUtils.isScalarUrl(this.props.url)) {
|
||||||
console.warn('Non-scalar widget, not setting scalar token!', url);
|
console.warn('Non-scalar widget, not setting scalar token!', url);
|
||||||
this.setState({
|
this.setState({
|
||||||
@ -214,11 +215,20 @@ export default class AppTile extends React.Component {
|
|||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
if (nextProps.url !== this.props.url) {
|
if (nextProps.url !== this.props.url) {
|
||||||
this._getNewState(nextProps);
|
this._getNewState(nextProps);
|
||||||
|
// Fetch IM token for new URL if we're showing and have permission to load
|
||||||
|
if (this.props.show && this.state.hasPermissionToLoad) {
|
||||||
this.setScalarToken();
|
this.setScalarToken();
|
||||||
} else if (nextProps.show && !this.props.show && this.props.waitForIframeLoad) {
|
}
|
||||||
|
} else if (nextProps.show && !this.props.show) {
|
||||||
|
if (this.props.waitForIframeLoad) {
|
||||||
this.setState({
|
this.setState({
|
||||||
loading: true,
|
loading: true,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
// Fetch IM token now that we're showing if we already have permission to load
|
||||||
|
if (this.state.hasPermissionToLoad) {
|
||||||
|
this.setScalarToken();
|
||||||
|
}
|
||||||
} else if (nextProps.widgetPageTitle !== this.props.widgetPageTitle) {
|
} else if (nextProps.widgetPageTitle !== this.props.widgetPageTitle) {
|
||||||
this.setState({
|
this.setState({
|
||||||
widgetPageTitle: nextProps.widgetPageTitle,
|
widgetPageTitle: nextProps.widgetPageTitle,
|
||||||
@ -410,6 +420,8 @@ export default class AppTile extends React.Component {
|
|||||||
console.warn('Granting permission to load widget - ', this.state.widgetUrl);
|
console.warn('Granting permission to load widget - ', this.state.widgetUrl);
|
||||||
localStorage.setItem(this.state.widgetPermissionId, true);
|
localStorage.setItem(this.state.widgetPermissionId, true);
|
||||||
this.setState({hasPermissionToLoad: true});
|
this.setState({hasPermissionToLoad: true});
|
||||||
|
// Now that we have permission, fetch the IM token
|
||||||
|
this.setScalarToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
_revokeWidgetPermission() {
|
_revokeWidgetPermission() {
|
||||||
@ -525,13 +537,24 @@ export default class AppTile extends React.Component {
|
|||||||
<MessageSpinner msg='Loading...' />
|
<MessageSpinner msg='Loading...' />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
if (this.state.initialising) {
|
if (!this.state.hasPermissionToLoad) {
|
||||||
|
const isRoomEncrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.room.roomId);
|
||||||
|
appTileBody = (
|
||||||
|
<div className={appTileBodyClass}>
|
||||||
|
<AppPermission
|
||||||
|
isRoomEncrypted={isRoomEncrypted}
|
||||||
|
url={this.state.widgetUrl}
|
||||||
|
onPermissionGranted={this._grantWidgetPermission}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
} else if (this.state.initialising) {
|
||||||
appTileBody = (
|
appTileBody = (
|
||||||
<div className={appTileBodyClass + (this.state.loading ? 'mx_AppLoading' : '')}>
|
<div className={appTileBodyClass + (this.state.loading ? 'mx_AppLoading' : '')}>
|
||||||
{ loadingElement }
|
{ loadingElement }
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
} else if (this.state.hasPermissionToLoad == true) {
|
} else {
|
||||||
if (this.isMixedContent()) {
|
if (this.isMixedContent()) {
|
||||||
appTileBody = (
|
appTileBody = (
|
||||||
<div className={appTileBodyClass}>
|
<div className={appTileBodyClass}>
|
||||||
@ -571,17 +594,6 @@ export default class AppTile extends React.Component {
|
|||||||
</div>;
|
</div>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
const isRoomEncrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.room.roomId);
|
|
||||||
appTileBody = (
|
|
||||||
<div className={appTileBodyClass}>
|
|
||||||
<AppPermission
|
|
||||||
isRoomEncrypted={isRoomEncrypted}
|
|
||||||
url={this.state.widgetUrl}
|
|
||||||
onPermissionGranted={this._grantWidgetPermission}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user