Wire GuestAccess to MatrixClientPeg

This commit is contained in:
Kegan Dougal 2015-12-04 10:37:53 +00:00
parent 91ee5f8a42
commit d52b3149d4
2 changed files with 40 additions and 19 deletions

View File

@ -15,12 +15,14 @@ limitations under the License.
*/ */
import {MatrixClientPeg} from "./MatrixClientPeg"; import {MatrixClientPeg} from "./MatrixClientPeg";
const ROOM_ID_KEY = "matrix-guest-room-ids"; const ROOM_ID_KEY = "matrix-guest-room-ids";
const IS_GUEST_KEY = "matrix-is-guest";
class GuestAccess { class GuestAccess {
constructor(localStorage) { constructor(localStorage) {
var existingRoomIds; var existingRoomIds;
try { try {
this._isGuest = localStorage.getItem(IS_GUEST_KEY) === "true";
existingRoomIds = JSON.parse( existingRoomIds = JSON.parse(
localStorage.getItem(ROOM_ID_KEY) // an array localStorage.getItem(ROOM_ID_KEY) // an array
); );
@ -32,20 +34,38 @@ class GuestAccess {
addRoom(roomId) { addRoom(roomId) {
this.rooms.add(roomId); this.rooms.add(roomId);
this._saveAndSetRooms();
} }
removeRoom(roomId) { removeRoom(roomId) {
this.rooms.delete(roomId); this.rooms.delete(roomId);
this._saveAndSetRooms();
} }
getRooms() { getRooms() {
return this.rooms.entries(); return this.rooms.entries();
} }
register() { isGuest() {
// nuke the rooms being watched from previous guest accesses if any. return this._isGuest;
localStorage.setItem(ROOM_ID_KEY, "[]"); }
return MatrixClientPeg.get().registerGuest();
markAsGuest(isGuest) {
try {
this.localStorage.setItem(IS_GUEST_KEY, JSON.stringify(isGuest));
// nuke the rooms being watched from previous guest accesses if any.
this.localStorage.setItem(ROOM_ID_KEY, "[]");
} catch (e) {} // ignore. If they don't do LS, they'll just get a new account.
this._isGuest = isGuest;
this.rooms = new Set();
}
_saveAndSetRooms() {
let rooms = this.getRooms();
MatrixClientPeg.get().setGuestRooms(rooms);
try {
this.localStorage.setItem(ROOM_ID_KEY, rooms);
} catch (e) {}
} }
} }

View File

@ -18,6 +18,7 @@ limitations under the License.
// A thing that holds your Matrix Client // A thing that holds your Matrix Client
var Matrix = require("matrix-js-sdk"); var Matrix = require("matrix-js-sdk");
var GuestAccess = require("./GuestAccess");
var matrixClient = null; var matrixClient = null;
@ -33,7 +34,7 @@ function deviceId() {
return id; return id;
} }
function createClient(hs_url, is_url, user_id, access_token, isGuest) { function createClient(hs_url, is_url, user_id, access_token, guestAccess) {
var opts = { var opts = {
baseUrl: hs_url, baseUrl: hs_url,
idBaseUrl: is_url, idBaseUrl: is_url,
@ -47,8 +48,10 @@ function createClient(hs_url, is_url, user_id, access_token, isGuest) {
} }
matrixClient = Matrix.createClient(opts); matrixClient = Matrix.createClient(opts);
if (isGuest) { if (guestAccess) {
matrixClient.setGuest(true); console.log("Guest: %s", guestAccess.isGuest());
matrixClient.setGuest(guestAccess.isGuest());
matrixClient.setGuestRooms(guestAccess.getRooms());
} }
} }
@ -57,20 +60,18 @@ if (localStorage) {
var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org'; var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org';
var access_token = localStorage.getItem("mx_access_token"); var access_token = localStorage.getItem("mx_access_token");
var user_id = localStorage.getItem("mx_user_id"); var user_id = localStorage.getItem("mx_user_id");
var isGuest = localStorage.getItem("mx_is_guest") || false; var guestAccess = new GuestAccess(localStorage);
if (isGuest) {
try {
isGuest = JSON.parse(isGuest);
}
catch (e) {} // ignore
isGuest = Boolean(isGuest); // in case of null, make it false.
}
if (access_token && user_id && hs_url) { if (access_token && user_id && hs_url) {
createClient(hs_url, is_url, user_id, access_token, isGuest); createClient(hs_url, is_url, user_id, access_token, guestAccess);
} }
} }
class MatrixClient { class MatrixClient {
constructor(guestAccess) {
this.guestAccess = guestAccess;
}
get() { get() {
return matrixClient; return matrixClient;
} }
@ -116,14 +117,14 @@ class MatrixClient {
console.warn("Error using local storage"); console.warn("Error using local storage");
} }
} }
createClient(hs_url, is_url, user_id, access_token, isGuest); this.guestAccess.markAsGuest(isGuest);
createClient(hs_url, is_url, user_id, access_token, this.guestAccess);
if (localStorage) { if (localStorage) {
try { try {
localStorage.setItem("mx_hs_url", hs_url); localStorage.setItem("mx_hs_url", hs_url);
localStorage.setItem("mx_is_url", is_url); localStorage.setItem("mx_is_url", is_url);
localStorage.setItem("mx_user_id", user_id); localStorage.setItem("mx_user_id", user_id);
localStorage.setItem("mx_access_token", access_token); localStorage.setItem("mx_access_token", access_token);
localStorage.setItem("mx_is_guest", isGuest);
} catch (e) { } catch (e) {
console.warn("Error using local storage: can't persist session!"); console.warn("Error using local storage: can't persist session!");
} }
@ -134,6 +135,6 @@ class MatrixClient {
} }
if (!global.mxMatrixClient) { if (!global.mxMatrixClient) {
global.mxMatrixClient = new MatrixClient(); global.mxMatrixClient = new MatrixClient(new GuestAccess(localStorage));
} }
module.exports = global.mxMatrixClient; module.exports = global.mxMatrixClient;