mirror of https://github.com/vector-im/riot-web
Add extra arg isGuest to MatrixClientPeg functions. Add GuestAccess class
GuestAccess serves to monitor which rooms are being tracked for guest purposespull/21833/head
parent
aceb85130e
commit
91ee5f8a42
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
import {MatrixClientPeg} from "./MatrixClientPeg";
|
||||
const ROOM_ID_KEY = "matrix-guest-room-ids";
|
||||
|
||||
class GuestAccess {
|
||||
|
||||
constructor(localStorage) {
|
||||
var existingRoomIds;
|
||||
try {
|
||||
existingRoomIds = JSON.parse(
|
||||
localStorage.getItem(ROOM_ID_KEY) // an array
|
||||
);
|
||||
}
|
||||
catch (e) {} // don't care
|
||||
this.rooms = new Set(existingRoomIds);
|
||||
this.localStorage = localStorage;
|
||||
}
|
||||
|
||||
addRoom(roomId) {
|
||||
this.rooms.add(roomId);
|
||||
}
|
||||
|
||||
removeRoom(roomId) {
|
||||
this.rooms.delete(roomId);
|
||||
}
|
||||
|
||||
getRooms() {
|
||||
return this.rooms.entries();
|
||||
}
|
||||
|
||||
register() {
|
||||
// nuke the rooms being watched from previous guest accesses if any.
|
||||
localStorage.setItem(ROOM_ID_KEY, "[]");
|
||||
return MatrixClientPeg.get().registerGuest();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuestAccess;
|
|
@ -33,7 +33,7 @@ function deviceId() {
|
|||
return id;
|
||||
}
|
||||
|
||||
function createClient(hs_url, is_url, user_id, access_token) {
|
||||
function createClient(hs_url, is_url, user_id, access_token, isGuest) {
|
||||
var opts = {
|
||||
baseUrl: hs_url,
|
||||
idBaseUrl: is_url,
|
||||
|
@ -47,6 +47,9 @@ function createClient(hs_url, is_url, user_id, access_token) {
|
|||
}
|
||||
|
||||
matrixClient = Matrix.createClient(opts);
|
||||
if (isGuest) {
|
||||
matrixClient.setGuest(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (localStorage) {
|
||||
|
@ -54,8 +57,16 @@ if (localStorage) {
|
|||
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");
|
||||
var isGuest = localStorage.getItem("mx_is_guest") || false;
|
||||
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) {
|
||||
createClient(hs_url, is_url, user_id, access_token);
|
||||
createClient(hs_url, is_url, user_id, access_token, isGuest);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +108,7 @@ class MatrixClient {
|
|||
}
|
||||
}
|
||||
|
||||
replaceUsingAccessToken(hs_url, is_url, user_id, access_token) {
|
||||
replaceUsingAccessToken(hs_url, is_url, user_id, access_token, isGuest) {
|
||||
if (localStorage) {
|
||||
try {
|
||||
localStorage.clear();
|
||||
|
@ -105,13 +116,14 @@ class MatrixClient {
|
|||
console.warn("Error using local storage");
|
||||
}
|
||||
}
|
||||
createClient(hs_url, is_url, user_id, access_token);
|
||||
createClient(hs_url, is_url, user_id, access_token, 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);
|
||||
localStorage.setItem("mx_is_guest", isGuest);
|
||||
} catch (e) {
|
||||
console.warn("Error using local storage: can't persist session!");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue