This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/matrix/session.js

64 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-02-11 01:55:29 +05:30
import Room from "./room/room.js";
export default class Session {
2019-02-07 05:50:27 +05:30
constructor(storage) {
this._storage = storage;
2019-02-07 05:50:27 +05:30
this._session = null;
2019-02-11 02:32:42 +05:30
// use Map here?
2019-02-11 01:55:29 +05:30
this._rooms = {};
2019-02-07 05:50:27 +05:30
}
// should be called before load
2019-02-07 06:33:47 +05:30
// loginData has device_id, user_id, home_server, access_token
2019-02-07 05:50:27 +05:30
async setLoginData(loginData) {
2019-02-11 01:55:29 +05:30
console.log("session.setLoginData");
const txn = await this._storage.readWriteTxn([this._storage.storeNames.session]);
2019-02-07 05:50:27 +05:30
const session = {loginData};
txn.session.set(session);
await txn.complete();
2018-12-21 19:05:24 +05:30
}
2019-02-07 05:50:27 +05:30
async load() {
const txn = await this._storage.readTxn([
2019-02-07 06:21:48 +05:30
this._storage.storeNames.session,
this._storage.storeNames.roomSummary,
2019-02-11 01:55:29 +05:30
this._storage.storeNames.roomState,
this._storage.storeNames.roomTimeline,
2019-02-07 06:21:48 +05:30
]);
// restore session object
2019-02-07 05:50:27 +05:30
this._session = await txn.session.get();
if (!this._session) {
throw new Error("session store is empty");
}
// load rooms
2019-02-07 06:21:48 +05:30
const rooms = await txn.roomSummary.getAll();
2019-02-11 01:55:29 +05:30
await Promise.all(rooms.map(summary => {
const room = this.createRoom(summary.roomId);
return room.load(summary, txn);
2019-02-07 06:21:48 +05:30
}));
2018-12-21 19:05:24 +05:30
}
getRoom(roomId) {
return this._rooms[roomId];
}
createRoom(roomId) {
const room = new Room(roomId, this._storage);
this._rooms[roomId] = room;
return room;
}
2018-12-21 19:05:24 +05:30
applySync(syncToken, accountData, txn) {
2019-02-16 07:29:10 +05:30
if (syncToken !== this._session.syncToken) {
this._session.syncToken = syncToken;
txn.session.set(this._session);
}
2019-02-07 05:50:27 +05:30
}
get syncToken() {
return this._session.syncToken;
}
get accessToken() {
return this._session.loginData.access_token;
2018-12-21 19:05:24 +05:30
}
}