From aab4c097e60c42010a6d4d8061806f7f4a2de75a Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 27 Jun 2017 12:26:13 +0100 Subject: [PATCH] Make query parameters generic. --- src/components/views/rooms/AppsDrawer.js | 86 ++++++++++++++++-------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/src/components/views/rooms/AppsDrawer.js b/src/components/views/rooms/AppsDrawer.js index ca56391cc3..8919fd6f74 100644 --- a/src/components/views/rooms/AppsDrawer.js +++ b/src/components/views/rooms/AppsDrawer.js @@ -63,32 +63,64 @@ module.exports = React.createClass({ } }, - _initAppConfig: function(appId, app) { - console.log("App props: ", this.props); - app.id = appId; - app.name = app.type; - - switch(app.type) { - case 'etherpad': - app.queryParams = '?userName=' + this.props.userId + - '&padId=' + this.props.room.roomId; - break; - case 'jitsi': { - const user = MatrixClientPeg.get().getUser(this.props.userId); - app.queryParams = '?confId=' + app.data.confId + - '&displayName=' + encodeURIComponent(user.displayName) + - '&avatarUrl=' + encodeURIComponent(MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl)) + - '&email=' + encodeURIComponent(this.props.userId) + - '&isAudioConf=' + app.data.isAudioConf; - - app.name += ' - ' + app.data.confId; - break; + /** + * 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 + * variables with. E.g. { "$bar": "baz" }. + * @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; } - case 'vrdemo': - app.name = 'Matrix VR Demo - ' + app.data.roomAlias; - app.queryParams = '?roomAlias=' + encodeURIComponent(app.data.roomAlias); - break; + pathTemplate = pathTemplate.replace( + key, encodeURIComponent(variables[key]), + ); } + return pathTemplate; + }, + + _initAppConfig: function(appId, app) { + 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) : '', + }; + + if(app.data) { + Object.keys(app.data).forEach((key) => { + params['$' + key] = app.data[key]; + }); + } + + app.id = appId; + app.name = app.name || app.type; + app.url = this.encodeUri(app.url, params); + + // switch(app.type) { + // case 'etherpad': + // app.queryParams = '?userName=' + this.props.userId + + // '&padId=' + this.props.room.roomId; + // break; + // case 'jitsi': { + // + // app.queryParams = '?confId=' + app.data.confId + + // '&displayName=' + encodeURIComponent(user.displayName) + + // '&avatarUrl=' + encodeURIComponent(MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl)) + + // '&email=' + encodeURIComponent(this.props.userId) + + // '&isAudioConf=' + app.data.isAudioConf; + // + // break; + // } + // case 'vrdemo': + // app.queryParams = '?roomAlias=' + encodeURIComponent(app.data.roomAlias); + // break; + // } return app; }, @@ -156,14 +188,10 @@ module.exports = React.createClass({ render: function() { const apps = this.state.apps.map( (app, index, arr) => { - let appUrl = app.url; - if (app.queryParams) { - appUrl += app.queryParams; - } return