diff --git a/src/GuestAccess.js b/src/GuestAccess.js deleted file mode 100644 index ef48d23ded..0000000000 --- a/src/GuestAccess.js +++ /dev/null @@ -1,51 +0,0 @@ -/* -Copyright 2015 OpenMarket Ltd - -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. -*/ -const IS_GUEST_KEY = "matrix-is-guest"; - -class GuestAccess { - - constructor(localStorage) { - this.localStorage = localStorage; - try { - this._isGuest = localStorage.getItem(IS_GUEST_KEY) === "true"; - } - catch (e) {} // don't care - } - - setPeekedRoom(roomId) { - // we purposefully do not persist this to local storage as peeking is - // entirely transient. - this._peekedRoomId = roomId; - } - - getPeekedRoom() { - return this._peekedRoomId; - } - - isGuest() { - return this._isGuest; - } - - markAsGuest(isGuest) { - try { - this.localStorage.setItem(IS_GUEST_KEY, JSON.stringify(isGuest)); - } catch (e) {} // ignore. If they don't do LS, they'll just get a new account. - this._isGuest = isGuest; - this._peekedRoomId = null; - } -} - -module.exports = GuestAccess; diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 77ebbaa2ba..8d155143fc 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -17,7 +17,6 @@ limitations under the License. 'use strict'; import Matrix from 'matrix-js-sdk'; -import GuestAccess from './GuestAccess'; const localStorage = window.localStorage; @@ -32,12 +31,15 @@ function deviceId() { return id; } -// A thing that holds your Matrix Client -// Also magically works across sessions through the power of localstorage +/** + * Wrapper object for handling the js-sdk Matrix Client object in the react-sdk + * Handles the creation/initialisation of client objects. + * This module provides a singleton instance of this class so the 'current' + * Matrix Client object is available easily. + */ class MatrixClientPeg { - constructor(guestAccess) { + constructor() { this.matrixClient = null; - this.guestAccess = guestAccess; } get(): MatrixClient { @@ -48,15 +50,19 @@ class MatrixClientPeg { this.matrixClient = null; } + /** + * Replace this MatrixClientPeg's client with a client instance that has + * Home Server / Identity Server URLs but no credentials + */ replaceUsingUrls(hs_url, is_url) { - this.replaceClient(hs_url, is_url); + this.replaceUsingAccessToken(hs_url, is_url); } + /** + * Replace this MatrixClientPeg's client with a client instance that has + * Home Server / Identity Server URLs and active credentials + */ replaceUsingAccessToken(hs_url, is_url, user_id, access_token, isGuest) { - this.replaceClient(hs_url, is_url, user_id, access_token, isGuest); - } - - replaceClient(hs_url, is_url, user_id, access_token, isGuest) { if (localStorage) { try { localStorage.clear(); @@ -64,15 +70,20 @@ class MatrixClientPeg { console.warn("Error clearing local storage", e); } } - this.guestAccess.markAsGuest(Boolean(isGuest)); this._createClient(hs_url, is_url, user_id, access_token); + this.matrixClient.setGuest(Boolean(isGuest)); + if (localStorage) { try { localStorage.setItem("mx_hs_url", hs_url); localStorage.setItem("mx_is_url", is_url); - localStorage.setItem("mx_user_id", user_id); - localStorage.setItem("mx_access_token", access_token); - console.log("Session persisted for %s", user_id); + + if (user_id !== undefined && access_token !== undefined) { + localStorage.setItem("mx_user_id", user_id); + localStorage.setItem("mx_access_token", access_token); + localStorage.setItem("mx_is_guest", JSON.stringify(isGuest)); + console.log("Session persisted for %s", user_id); + } } catch (e) { console.warn("Error using local storage: can't persist session!", e); } @@ -87,7 +98,7 @@ class MatrixClientPeg { this.matrixClient.idBaseUrl, this.matrixClient.credentials.userId, this.matrixClient.getAccessToken(), - this.guestAccess.isGuest(), + this.matrixClient.isGuest(), ]; } @@ -97,10 +108,19 @@ class MatrixClientPeg { const is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org'; const access_token = localStorage.getItem("mx_access_token"); const user_id = localStorage.getItem("mx_user_id"); - const guestAccess = new GuestAccess(localStorage); + + let is_guest; + if (localStorage.getItem("mx_is_guest") !== null) { + is_guest = localStorage.getItem("mx_is_guest") === "true"; + } else { + // legacy key name + is_guest = localStorage.getItem("matrix-is-guest") === "true"; + } + if (access_token && user_id && hs_url) { console.log("Restoring session for %s", user_id); this._createClient(hs_url, is_url, user_id, access_token); + this.matrixClient.setGuest(is_guest); } else { console.log("Session not found."); } @@ -126,21 +146,11 @@ class MatrixClientPeg { // we're going to add eventlisteners for each matrix event tile, so the // potential number of event listeners is quite high. this.matrixClient.setMaxListeners(500); - - if (this.guestAccess) { - console.log("Guest: %s", this.guestAccess.isGuest()); - this.matrixClient.setGuest(this.guestAccess.isGuest()); - var peekedRoomId = this.guestAccess.getPeekedRoom(); - if (peekedRoomId) { - console.log("Peeking in room %s", peekedRoomId); - this.matrixClient.peekInRoom(peekedRoomId); - } - } } } if (!global.mxMatrixClientPeg) { - global.mxMatrixClientPeg = new MatrixClientPeg(new GuestAccess(localStorage)); + global.mxMatrixClientPeg = new MatrixClientPeg(); global.mxMatrixClientPeg.tryRestore(); } module.exports = global.mxMatrixClientPeg;