From 715db8920496e58912a528862f7e7a5789b2961b Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 20 Jul 2015 13:19:47 +0100 Subject: [PATCH 1/3] Move all the calls to createClient inside the MatrixClientPeg --- src/MatrixClientPeg.js | 40 ++++++++++++++++++++------- src/controllers/templates/Login.js | 28 ++++--------------- src/controllers/templates/Register.js | 17 ++---------- 3 files changed, 39 insertions(+), 46 deletions(-) diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 0b6c26496..87e6af08c 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -22,18 +22,25 @@ var Matrix = require("matrix-js-sdk"); var matrixClient = null; var localStorage = window.localStorage; + +function createClient(hs_url, is_url, user_id, access_token) { + var opts = { + baseUrl: hs_url, + idBaseUrl: is_url, + accessToken: access_token, + userId: user_id + }; + + matrixClient = Matrix.createClient(opts); +} + if (localStorage) { var hs_url = localStorage.getItem("mx_hs_url"); var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org'; var access_token = localStorage.getItem("mx_access_token"); var user_id = localStorage.getItem("mx_user_id"); if (access_token && user_id && hs_url) { - matrixClient = Matrix.createClient({ - baseUrl: hs_url, - idBaseUrl: is_url, - accessToken: access_token, - userId: user_id - }); + matrixClient = createClient(hs_url, is_url, user_id, access_token); } } @@ -42,15 +49,28 @@ module.exports = { return matrixClient; }, - replace: function(cli) { - matrixClient = cli; - }, - replaceUsingUrls: function(hs_url, is_url) { matrixClient = Matrix.createClient({ baseUrl: hs_url, idBaseUrl: is_url }); } + + replaceUsingAccessToken: function(hs_url, is_url, user_id, access_token) { + matrixClient = createClient(hs_url, is_url, user_id, access_token); + if (localStorage) { + try { + localStorage.clear(); + 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); + } catch (e) { + console.warn("Error using local storage: can't persist session!"); + } + } else { + console.warn("No local storage available: can't persist session!"); + } + } }; diff --git a/src/controllers/templates/Login.js b/src/controllers/templates/Login.js index 276692719..135b64337 100644 --- a/src/controllers/templates/Login.js +++ b/src/controllers/templates/Login.js @@ -75,29 +75,13 @@ module.exports = { 'user': formVals.username, 'password': formVals.password }).done(function(data) { - // XXX: we assume this means we're logged in, but there could be a next stage - MatrixClientPeg.replace(Matrix.createClient({ - baseUrl: self.state.hs_url, - idBaseUrl: self.state.is_url, - userId: data.user_id, - accessToken: data.access_token + MatrixClientPeg.replaceUsingAccessToken( + this.state.hs_url, this.state.is_url, + data.user_id, data.access_token + ); })); - var localStorage = window.localStorage; - if (localStorage) { - try { - localStorage.clear(); - localStorage.setItem("mx_hs_url", self.state.hs_url); - localStorage.setItem("mx_is_url", self.state.is_url); - localStorage.setItem("mx_user_id", data.user_id); - localStorage.setItem("mx_access_token", data.access_token); - } catch (e) { - console.warn("Error using local storage: can't persist session!"); - } - } else { - console.warn("No local storage available: can't persist session!"); - } - if (self.props.onLoggedIn) { - self.props.onLoggedIn(); + if (that.props.onLoggedIn) { + that.props.onLoggedIn(); } }, function(error) { self.setStep("stage_m.login.password"); diff --git a/src/controllers/templates/Register.js b/src/controllers/templates/Register.js index 89a3872dc..faff4c66f 100644 --- a/src/controllers/templates/Register.js +++ b/src/controllers/templates/Register.js @@ -259,20 +259,9 @@ module.exports = { }, onRegistered: function(user_id, access_token) { - MatrixClientPeg.replace(Matrix.createClient({ - baseUrl: this.state.hs_url, - idBaseUrl: this.state.is_url, - userId: user_id, - accessToken: access_token - })); - var localStorage = window.localStorage; - if (localStorage) { - localStorage.setItem("mx_hs_url", this.state.hs_url); - localStorage.setItem("mx_user_id", user_id); - localStorage.setItem("mx_access_token", access_token); - } else { - console.warn("No local storage available: can't persist session!"); - } + MatrixClientPeg.replaceUsingAccessToken( + this.state.hs_url, this.state.is_url, user_id, access_token + ); if (this.props.onLoggedIn) { this.props.onLoggedIn(); } From d8494ff89b2abd791fc360909239a7db8cce54b7 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 20 Jul 2015 13:48:40 +0100 Subject: [PATCH 2/3] Fix syntax --- src/MatrixClientPeg.js | 6 +++--- src/controllers/templates/Login.js | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 87e6af08c..6b36e67e6 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -40,7 +40,7 @@ if (localStorage) { var access_token = localStorage.getItem("mx_access_token"); var user_id = localStorage.getItem("mx_user_id"); if (access_token && user_id && hs_url) { - matrixClient = createClient(hs_url, is_url, user_id, access_token); + createClient(hs_url, is_url, user_id, access_token); } } @@ -54,10 +54,10 @@ module.exports = { baseUrl: hs_url, idBaseUrl: is_url }); - } + }, replaceUsingAccessToken: function(hs_url, is_url, user_id, access_token) { - matrixClient = createClient(hs_url, is_url, user_id, access_token); + createClient(hs_url, is_url, user_id, access_token); if (localStorage) { try { localStorage.clear(); diff --git a/src/controllers/templates/Login.js b/src/controllers/templates/Login.js index 135b64337..1cfec18d0 100644 --- a/src/controllers/templates/Login.js +++ b/src/controllers/templates/Login.js @@ -76,10 +76,9 @@ module.exports = { 'password': formVals.password }).done(function(data) { MatrixClientPeg.replaceUsingAccessToken( - this.state.hs_url, this.state.is_url, + that.state.hs_url, that.state.is_url, data.user_id, data.access_token ); - })); if (that.props.onLoggedIn) { that.props.onLoggedIn(); } From 28022534f70ac9e9a4d0e7496447613d0fe0fe86 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 20 Jul 2015 14:19:13 +0100 Subject: [PATCH 3/3] Fix the client-peg clean up to match the changes vector has made to the sdk --- src/MatrixClientPeg.js | 4 ++++ src/controllers/pages/MatrixChat.js | 2 +- src/controllers/templates/Login.js | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 6b36e67e6..1acd2a701 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -49,6 +49,10 @@ module.exports = { return matrixClient; }, + unset: function() { + matrixClient = null; + }, + replaceUsingUrls: function(hs_url, is_url) { matrixClient = Matrix.createClient({ baseUrl: hs_url, diff --git a/src/controllers/pages/MatrixChat.js b/src/controllers/pages/MatrixChat.js index 48c55fe09..42aa3a522 100644 --- a/src/controllers/pages/MatrixChat.js +++ b/src/controllers/pages/MatrixChat.js @@ -90,7 +90,7 @@ module.exports = { } Notifier.stop(); MatrixClientPeg.get().removeAllListeners(); - MatrixClientPeg.replace(null); + MatrixClientPeg.unset(); break; case 'start_registration': if (this.state.logged_in) return; diff --git a/src/controllers/templates/Login.js b/src/controllers/templates/Login.js index 1cfec18d0..37a957850 100644 --- a/src/controllers/templates/Login.js +++ b/src/controllers/templates/Login.js @@ -76,11 +76,11 @@ module.exports = { 'password': formVals.password }).done(function(data) { MatrixClientPeg.replaceUsingAccessToken( - that.state.hs_url, that.state.is_url, + self.state.hs_url, self.state.is_url, data.user_id, data.access_token ); - if (that.props.onLoggedIn) { - that.props.onLoggedIn(); + if (self.props.onLoggedIn) { + self.props.onLoggedIn(); } }, function(error) { self.setStep("stage_m.login.password");