2017-05-18 01:21:02 +03:00
|
|
|
/*
|
2017-06-28 14:26:05 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2017-05-18 01:21:02 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-20 12:54:41 +03:00
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-20 12:54:41 +03:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import AppTile from '../elements/AppTile';
|
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import dis from '../../../dispatcher';
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import SdkConfig from '../../../SdkConfig';
|
|
|
|
import ScalarAuthClient from '../../../ScalarAuthClient';
|
2017-06-20 19:56:45 +03:00
|
|
|
import ScalarMessaging from '../../../ScalarMessaging';
|
2017-06-28 15:55:18 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2018-06-26 13:59:16 +03:00
|
|
|
import WidgetUtils from '../../../utils/WidgetUtils';
|
2017-12-17 06:11:00 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2017-06-28 15:55:18 +03:00
|
|
|
|
2017-08-11 01:53:43 +03:00
|
|
|
// The maximum number of widgets that can be added in a room
|
|
|
|
const MAX_WIDGETS = 2;
|
2017-05-30 15:47:17 +03:00
|
|
|
|
2017-05-18 01:21:02 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'AppsDrawer',
|
|
|
|
|
|
|
|
propTypes: {
|
2018-02-23 18:37:33 +03:00
|
|
|
userId: PropTypes.string.isRequired,
|
2017-12-26 04:03:18 +03:00
|
|
|
room: PropTypes.object.isRequired,
|
2018-02-23 18:37:33 +03:00
|
|
|
showApps: PropTypes.bool, // Should apps be rendered
|
|
|
|
hide: PropTypes.bool, // If rendered, should apps drawer be visible
|
2018-02-09 16:23:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
defaultProps: {
|
|
|
|
showApps: true,
|
|
|
|
hide: false,
|
2017-06-13 16:28:37 +03:00
|
|
|
},
|
|
|
|
|
2017-06-28 14:54:47 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
apps: this._getApps(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
componentWillMount: function() {
|
2017-06-20 19:56:45 +03:00
|
|
|
ScalarMessaging.startListening();
|
2018-02-09 16:23:34 +03:00
|
|
|
MatrixClientPeg.get().on('RoomState.events', this.onRoomStateEvents);
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
2017-06-20 12:54:41 +03:00
|
|
|
this.scalarClient = null;
|
|
|
|
if (SdkConfig.get().integrations_ui_url && SdkConfig.get().integrations_rest_url) {
|
|
|
|
this.scalarClient = new ScalarAuthClient();
|
2017-08-23 21:35:33 +03:00
|
|
|
this.scalarClient.connect().then(() => {
|
2017-06-20 12:54:41 +03:00
|
|
|
this.forceUpdate();
|
2017-08-23 21:35:33 +03:00
|
|
|
}).catch((e) => {
|
2018-02-09 16:23:34 +03:00
|
|
|
console.log('Failed to connect to integrations server');
|
2017-08-23 21:35:33 +03:00
|
|
|
// TODO -- Handle Scalar errors
|
|
|
|
// this.setState({
|
|
|
|
// scalar_error: err,
|
|
|
|
// });
|
2017-06-20 12:54:41 +03:00
|
|
|
});
|
2017-06-14 16:05:29 +03:00
|
|
|
}
|
2017-08-18 16:48:58 +03:00
|
|
|
|
2017-08-21 16:34:13 +03:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
componentWillUnmount: function() {
|
2017-06-27 13:28:38 +03:00
|
|
|
ScalarMessaging.stopListening();
|
2017-06-13 16:28:37 +03:00
|
|
|
if (MatrixClientPeg.get()) {
|
2018-02-09 16:23:34 +03:00
|
|
|
MatrixClientPeg.get().removeListener('RoomState.events', this.onRoomStateEvents);
|
2017-06-13 16:28:37 +03:00
|
|
|
}
|
2017-08-18 16:48:58 +03:00
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
// Room has changed probably, update apps
|
|
|
|
this._updateApps();
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(action) {
|
2018-02-09 16:23:34 +03:00
|
|
|
const hideWidgetKey = this.props.room.roomId + '_hide_widget_drawer';
|
2017-08-18 16:48:58 +03:00
|
|
|
switch (action.action) {
|
|
|
|
case 'appsDrawer':
|
|
|
|
if (action.show) {
|
2017-10-26 21:17:13 +03:00
|
|
|
localStorage.removeItem(hideWidgetKey);
|
2017-10-25 02:21:46 +03:00
|
|
|
} else {
|
|
|
|
// Store hidden state of widget
|
|
|
|
// Don't show if previously hidden
|
2017-10-26 21:17:13 +03:00
|
|
|
localStorage.setItem(hideWidgetKey, true);
|
2017-08-18 16:48:58 +03:00
|
|
|
}
|
2017-10-25 02:21:46 +03:00
|
|
|
|
2017-08-18 16:48:58 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-06-05 20:21:31 +03:00
|
|
|
},
|
|
|
|
|
2017-06-27 14:26:13 +03:00
|
|
|
/**
|
|
|
|
* Encodes a URI according to a set of template variables. Variables will be
|
|
|
|
* passed through encodeURIComponent.
|
|
|
|
* @param {string} pathTemplate The path with template variables e.g. '/foo/$bar'.
|
|
|
|
* @param {Object} variables The key/value pairs to replace the template
|
2018-02-09 16:23:34 +03:00
|
|
|
* variables with. E.g. { '$bar': 'baz' }.
|
2017-06-27 14:26:13 +03:00
|
|
|
* @return {string} The result of replacing all template variables e.g. '/foo/baz'.
|
|
|
|
*/
|
|
|
|
encodeUri: function(pathTemplate, variables) {
|
|
|
|
for (const key in variables) {
|
|
|
|
if (!variables.hasOwnProperty(key)) {
|
|
|
|
continue;
|
2017-05-30 15:47:17 +03:00
|
|
|
}
|
2017-06-27 14:26:13 +03:00
|
|
|
pathTemplate = pathTemplate.replace(
|
|
|
|
key, encodeURIComponent(variables[key]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return pathTemplate;
|
|
|
|
},
|
|
|
|
|
2017-08-18 13:41:27 +03:00
|
|
|
_initAppConfig: function(appId, app, sender) {
|
2017-06-27 14:26:13 +03:00
|
|
|
const user = MatrixClientPeg.get().getUser(this.props.userId);
|
|
|
|
const params = {
|
|
|
|
'$matrix_user_id': this.props.userId,
|
|
|
|
'$matrix_room_id': this.props.room.roomId,
|
|
|
|
'$matrix_display_name': user ? user.displayName : this.props.userId,
|
|
|
|
'$matrix_avatar_url': user ? MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl) : '',
|
2017-12-17 06:11:00 +03:00
|
|
|
|
2017-12-18 19:19:54 +03:00
|
|
|
// TODO: Namespace themes through some standard
|
|
|
|
'$theme': SettingsStore.getValue("theme"),
|
2017-06-27 14:26:13 +03:00
|
|
|
};
|
|
|
|
|
2017-12-06 19:44:41 +03:00
|
|
|
app.id = appId;
|
|
|
|
app.name = app.name || app.type;
|
|
|
|
|
2017-11-16 16:19:36 +03:00
|
|
|
if (app.data) {
|
2017-06-27 14:26:13 +03:00
|
|
|
Object.keys(app.data).forEach((key) => {
|
|
|
|
params['$' + key] = app.data[key];
|
|
|
|
});
|
2017-12-06 19:44:41 +03:00
|
|
|
|
|
|
|
app.waitForIframeLoad = (app.data.waitForIframeLoad === 'false' ? false : true);
|
2017-05-30 15:47:17 +03:00
|
|
|
}
|
2017-06-13 16:28:37 +03:00
|
|
|
|
2017-06-27 14:26:13 +03:00
|
|
|
app.url = this.encodeUri(app.url, params);
|
2017-08-18 13:41:27 +03:00
|
|
|
app.creatorUserId = (sender && sender.userId) ? sender.userId : null;
|
2017-06-27 14:26:13 +03:00
|
|
|
|
2017-06-13 16:28:37 +03:00
|
|
|
return app;
|
|
|
|
},
|
|
|
|
|
|
|
|
onRoomStateEvents: function(ev, state) {
|
|
|
|
if (ev.getRoomId() !== this.props.room.roomId || ev.getType() !== 'im.vector.modular.widgets') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._updateApps();
|
|
|
|
},
|
|
|
|
|
|
|
|
_getApps: function() {
|
2017-07-11 14:15:27 +03:00
|
|
|
const appsStateEvents = this.props.room.currentState.getStateEvents('im.vector.modular.widgets');
|
2017-06-13 16:28:37 +03:00
|
|
|
if (!appsStateEvents) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:15:27 +03:00
|
|
|
return appsStateEvents.filter((ev) => {
|
|
|
|
return ev.getContent().type && ev.getContent().url;
|
|
|
|
}).map((ev) => {
|
2017-08-18 13:41:27 +03:00
|
|
|
return this._initAppConfig(ev.getStateKey(), ev.getContent(), ev.sender);
|
2017-06-13 16:28:37 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateApps: function() {
|
|
|
|
const apps = this._getApps();
|
|
|
|
this.setState({
|
2017-06-28 17:53:18 +03:00
|
|
|
apps: apps,
|
2017-06-13 16:28:37 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-28 01:38:02 +03:00
|
|
|
_canUserModify: function() {
|
|
|
|
try {
|
|
|
|
return WidgetUtils.canUserModifyWidgets(this.props.room.roomId);
|
2017-11-16 16:19:36 +03:00
|
|
|
} catch (err) {
|
2017-07-28 01:38:02 +03:00
|
|
|
console.error(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-18 16:48:58 +03:00
|
|
|
_launchManageIntegrations: function() {
|
2018-02-09 16:23:34 +03:00
|
|
|
const IntegrationsManager = sdk.getComponent('views.settings.IntegrationsManager');
|
2017-06-20 12:54:41 +03:00
|
|
|
const src = (this.scalarClient !== null && this.scalarClient.hasCredentials()) ?
|
2018-02-09 14:44:27 +03:00
|
|
|
this.scalarClient.getScalarInterfaceUrlForRoom(this.props.room, 'add_integ') :
|
2017-06-20 12:54:41 +03:00
|
|
|
null;
|
2017-07-27 19:19:18 +03:00
|
|
|
Modal.createTrackedDialog('Integrations Manager', '', IntegrationsManager, {
|
2017-06-20 12:54:41 +03:00
|
|
|
src: src,
|
2018-02-09 16:23:34 +03:00
|
|
|
}, 'mx_IntegrationsManager');
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
2017-06-13 16:31:37 +03:00
|
|
|
|
2017-08-18 16:48:58 +03:00
|
|
|
onClickAddWidget: function(e) {
|
|
|
|
e.preventDefault();
|
2017-08-11 01:53:43 +03:00
|
|
|
// Display a warning dialog if the max number of widgets have already been added to the room
|
2017-08-18 17:35:01 +03:00
|
|
|
const apps = this._getApps();
|
|
|
|
if (apps && apps.length >= MAX_WIDGETS) {
|
2018-02-09 16:23:34 +03:00
|
|
|
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
|
2017-08-11 01:53:43 +03:00
|
|
|
const errorMsg = `The maximum number of ${MAX_WIDGETS} widgets have already been added to this room.`;
|
|
|
|
console.error(errorMsg);
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2018-02-09 16:23:34 +03:00
|
|
|
title: _t('Cannot add any more widgets'),
|
|
|
|
description: _t('The maximum permitted number of widgets have already been added to this room.'),
|
2017-08-11 01:53:43 +03:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-08-18 16:48:58 +03:00
|
|
|
this._launchManageIntegrations();
|
2017-05-18 01:21:02 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2018-05-12 23:29:37 +03:00
|
|
|
const enableScreenshots = SettingsStore.getValue("enableWidgetScreenshots", this.props.room.room_id);
|
|
|
|
|
2017-05-22 14:34:27 +03:00
|
|
|
const apps = this.state.apps.map(
|
2017-06-14 14:26:43 +03:00
|
|
|
(app, index, arr) => {
|
2017-07-27 19:47:04 +03:00
|
|
|
return (<AppTile
|
2017-07-06 17:59:59 +03:00
|
|
|
key={app.id}
|
2017-06-14 14:26:43 +03:00
|
|
|
id={app.id}
|
2017-06-27 14:26:13 +03:00
|
|
|
url={app.url}
|
2017-06-14 14:26:43 +03:00
|
|
|
name={app.name}
|
2017-07-14 13:17:59 +03:00
|
|
|
type={app.type}
|
2017-06-14 14:26:43 +03:00
|
|
|
fullWidth={arr.length<2 ? true : false}
|
|
|
|
room={this.props.room}
|
|
|
|
userId={this.props.userId}
|
2017-08-16 20:19:12 +03:00
|
|
|
show={this.props.showApps}
|
2017-08-18 13:41:27 +03:00
|
|
|
creatorUserId={app.creatorUserId}
|
2017-12-08 18:27:14 +03:00
|
|
|
widgetPageTitle={(app.data && app.data.title) ? app.data.title : ''}
|
2017-12-06 19:44:41 +03:00
|
|
|
waitForIframeLoad={app.waitForIframeLoad}
|
2018-05-12 23:29:37 +03:00
|
|
|
whitelistCapabilities={enableScreenshots ? ["m.capability.screenshot"] : []}
|
2017-07-27 19:47:04 +03:00
|
|
|
/>);
|
2017-06-14 14:26:43 +03:00
|
|
|
});
|
2017-05-22 14:34:27 +03:00
|
|
|
|
2017-08-16 20:19:12 +03:00
|
|
|
let addWidget;
|
|
|
|
if (this.props.showApps &&
|
|
|
|
this._canUserModify()
|
|
|
|
) {
|
|
|
|
addWidget = <div
|
|
|
|
onClick={this.onClickAddWidget}
|
2018-02-09 16:23:34 +03:00
|
|
|
role='button'
|
|
|
|
tabIndex='0'
|
2017-08-21 13:29:42 +03:00
|
|
|
className={this.state.apps.length<2 ?
|
2018-02-09 16:23:34 +03:00
|
|
|
'mx_AddWidget_button mx_AddWidget_button_full_width' :
|
|
|
|
'mx_AddWidget_button'
|
2017-08-21 13:29:42 +03:00
|
|
|
}
|
2017-08-11 01:53:43 +03:00
|
|
|
title={_t('Add a widget')}>
|
2017-09-28 13:21:06 +03:00
|
|
|
[+] { _t('Add a widget') }
|
2017-08-16 20:19:12 +03:00
|
|
|
</div>;
|
|
|
|
}
|
2017-06-06 16:50:43 +03:00
|
|
|
|
2017-05-18 01:21:02 +03:00
|
|
|
return (
|
2018-02-09 16:23:34 +03:00
|
|
|
<div className={'mx_AppsDrawer' + (this.props.hide ? ' mx_AppsDrawer_hidden' : '')}>
|
|
|
|
<div id='apps' className='mx_AppsContainer'>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ apps }
|
2017-05-22 14:34:27 +03:00
|
|
|
</div>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ this._canUserModify() && addWidget }
|
2017-05-18 01:21:02 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|