No longer persist guest room IDs. Replace with transient peeked room ID.

pull/21833/head
Kegan Dougal 2016-01-06 17:33:00 +00:00
parent afbb451d4a
commit 29587ec347
2 changed files with 13 additions and 30 deletions

View File

@ -13,37 +13,26 @@ 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";
const IS_GUEST_KEY = "matrix-is-guest";
class GuestAccess {
constructor(localStorage) {
var existingRoomIds;
this.localStorage = localStorage;
try {
this._isGuest = localStorage.getItem(IS_GUEST_KEY) === "true";
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);
this._saveAndSetRooms();
setPeekedRoom(roomId) {
// we purposefully do not persist this to local storage as peeking is
// entirely transient.
this._peekedRoomId = roomId;
}
removeRoom(roomId) {
this.rooms.delete(roomId);
this._saveAndSetRooms();
}
getRooms() {
return Array.from(this.rooms.entries());
getPeekedRoom() {
return this._peekedRoomId;
}
isGuest() {
@ -53,19 +42,9 @@ class GuestAccess {
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) {}
this._peekedRoomId = null;
}
}

View File

@ -51,7 +51,11 @@ function createClient(hs_url, is_url, user_id, access_token, guestAccess) {
if (guestAccess) {
console.log("Guest: %s", guestAccess.isGuest());
matrixClient.setGuest(guestAccess.isGuest());
matrixClient.setGuestRooms(guestAccess.getRooms());
var peekedRoomId = guestAccess.getPeekedRoom();
if (peekedRoomId) {
console.log("Peeking in room %s", peekedRoomId);
matrixClient.peekInRoom(peekedRoomId);
}
}
}