From d0ec84fe591734645a27bf5d0c687271a58a796b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 4 Dec 2015 11:34:50 +0000 Subject: [PATCH] Hook up auto-registration as a guest to MatrixChat. --- src/MatrixClientPeg.js | 2 +- src/components/structures/MatrixChat.js | 52 +++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 7a1ee781ab..76fa102562 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -117,7 +117,7 @@ class MatrixClient { console.warn("Error using local storage"); } } - this.guestAccess.markAsGuest(isGuest); + this.guestAccess.markAsGuest(Boolean(isGuest)); createClient(hs_url, is_url, user_id, access_token, this.guestAccess); if (localStorage) { try { diff --git a/src/components/structures/MatrixChat.js b/src/components/structures/MatrixChat.js index a6d6716222..6bb16dafd2 100644 --- a/src/components/structures/MatrixChat.js +++ b/src/components/structures/MatrixChat.js @@ -40,7 +40,8 @@ module.exports = React.createClass({ config: React.PropTypes.object.isRequired, ConferenceHandler: React.PropTypes.any, onNewScreen: React.PropTypes.func, - registrationUrl: React.PropTypes.string + registrationUrl: React.PropTypes.string, + enableGuest: React.PropTypes.bool }, PageTypes: { @@ -75,15 +76,31 @@ module.exports = React.createClass({ }, componentDidMount: function() { + this._autoRegisterAsGuest = false; + if (this.props.enableGuest) { + if (!this.props.config || !this.props.config.default_hs_url) { + console.error("Cannot enable guest access: No supplied config prop for HS/IS URLs"); + } + else { + this._autoRegisterAsGuest = true; + } + } + this.dispatcherRef = dis.register(this.onAction); if (this.state.logged_in) { + // Don't auto-register as a guest. This applies if you refresh the page on a + // logged in client THEN hit the Sign Out button. + this._autoRegisterAsGuest = false; this.startMatrixClient(); } this.focusComposer = false; document.addEventListener("keydown", this.onKeyDown); window.addEventListener("focus", this.onFocus); + if (this.state.logged_in) { this.notifyNewScreen(''); + } else if (this._autoRegisterAsGuest) { + this._registerAsGuest(); } else { this.notifyNewScreen('login'); } @@ -115,6 +132,26 @@ module.exports = React.createClass({ } }, + _registerAsGuest: function() { + var self = this; + var config = this.props.config; + console.log("Doing guest login on %s", config.default_hs_url); + MatrixClientPeg.replaceUsingUrls( + config.default_hs_url, config.default_is_url + ); + MatrixClientPeg.get().registerGuest().done(function(creds) { + console.log("Registered as guest: %s", JSON.stringify(creds)); + }, function(err) { + console.error(err.data); + self._setAutoRegisterAsGuest(false); + }); + }, + + _setAutoRegisterAsGuest: function(shouldAutoRegister) { + this._autoRegisterAsGuest = shouldAutoRegister; + this.forceUpdate(); + }, + onAction: function(payload) { var roomIndexDelta = 1; @@ -130,6 +167,7 @@ module.exports = React.createClass({ MatrixClientPeg.get().stopClient(); MatrixClientPeg.get().removeAllListeners(); MatrixClientPeg.unset(); + this.notifyNewScreen('login'); this.replaceState({ logged_in: false, @@ -634,12 +672,20 @@ module.exports = React.createClass({ ); } - } else if (this.state.logged_in) { + } else if (this.state.logged_in || (!this.state.logged_in && this._autoRegisterAsGuest)) { var Spinner = sdk.getComponent('elements.Spinner'); + var logoutLink; + if (this.state.logged_in) { + logoutLink = ( + + Logout + + ); + } return (
- Logout + {logoutLink}
); } else if (this.state.screen == 'register') {