diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 6b36e67e6b..36ccd0a7dc 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -23,6 +23,16 @@ var matrixClient = null; var localStorage = window.localStorage; +function deviceId() { + var id = Math.floor(Math.random()*16777215).toString(16); + id = "W" + "000000".substring(id.length) + id; + if (localStorage) { + id = localStorage.getItem("mx_device_id") || id; + localStorage.setItem("mx_device_id", id); + } + return id; +} + function createClient(hs_url, is_url, user_id, access_token) { var opts = { baseUrl: hs_url, @@ -31,6 +41,11 @@ function createClient(hs_url, is_url, user_id, access_token) { userId: user_id }; + if (localStorage) { + opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage); + opts.deviceId = deviceId(); + } + matrixClient = Matrix.createClient(opts); } @@ -49,6 +64,10 @@ module.exports = { return matrixClient; }, + unset: function() { + matrixClient = null; + }, + replaceUsingUrls: function(hs_url, is_url) { matrixClient = Matrix.createClient({ baseUrl: hs_url, @@ -57,10 +76,16 @@ module.exports = { }, replaceUsingAccessToken: function(hs_url, is_url, user_id, access_token) { - createClient(hs_url, is_url, user_id, access_token); if (localStorage) { try { localStorage.clear(); + } catch (e) { + console.warn("Error using local storage"); + } + } + createClient(hs_url, is_url, user_id, access_token); + if (localStorage) { + try { localStorage.setItem("mx_hs_url", hs_url); localStorage.setItem("mx_is_url", is_url); localStorage.setItem("mx_user_id", user_id); diff --git a/src/RoomListSorter.js b/src/RoomListSorter.js index bc7a001670..730a0de18b 100644 --- a/src/RoomListSorter.js +++ b/src/RoomListSorter.js @@ -17,7 +17,12 @@ limitations under the License. 'use strict'; function tsOfNewestEvent(room) { - return room.timeline[room.timeline.length - 1].getTs(); + if (room.timeline.length) { + return room.timeline[room.timeline.length - 1].getTs(); + } + else { + return Number.MAX_SAFE_INTEGER; + } } function mostRecentActivityFirst(roomList) {