2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 01:11:10 +05:30
|
|
|
import {Room} from "./room/Room.js";
|
2019-02-27 01:19:45 +05:30
|
|
|
import { ObservableMap } from "../observable/index.js";
|
2019-07-27 02:03:33 +05:30
|
|
|
import { SendScheduler, RateLimitingBackoff } from "./SendScheduler.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {User} from "./User.js";
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class Session {
|
2019-03-09 00:33:18 +05:30
|
|
|
// sessionInfo contains deviceId, userId and homeServer
|
2019-05-12 23:56:46 +05:30
|
|
|
constructor({storage, hsApi, sessionInfo}) {
|
|
|
|
this._storage = storage;
|
2019-03-09 00:33:18 +05:30
|
|
|
this._hsApi = hsApi;
|
2019-05-12 23:56:46 +05:30
|
|
|
this._session = null;
|
2019-03-09 00:30:37 +05:30
|
|
|
this._sessionInfo = sessionInfo;
|
2019-05-12 23:56:46 +05:30
|
|
|
this._rooms = new ObservableMap();
|
2019-07-27 02:03:33 +05:30
|
|
|
this._sendScheduler = new SendScheduler({hsApi, backoff: new RateLimitingBackoff()});
|
2019-02-24 23:55:06 +05:30
|
|
|
this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params);
|
2019-07-29 13:53:15 +05:30
|
|
|
this._user = new User(sessionInfo.userId);
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2019-05-12 23:56:46 +05:30
|
|
|
async load() {
|
|
|
|
const txn = await this._storage.readTxn([
|
|
|
|
this._storage.storeNames.session,
|
|
|
|
this._storage.storeNames.roomSummary,
|
|
|
|
this._storage.storeNames.roomState,
|
|
|
|
this._storage.storeNames.timelineEvents,
|
2019-05-20 00:19:46 +05:30
|
|
|
this._storage.storeNames.timelineFragments,
|
2019-07-27 02:03:33 +05:30
|
|
|
this._storage.storeNames.pendingEvents,
|
2019-05-12 23:56:46 +05:30
|
|
|
]);
|
|
|
|
// restore session object
|
|
|
|
this._session = await txn.session.get();
|
|
|
|
if (!this._session) {
|
2019-03-09 00:30:37 +05:30
|
|
|
this._session = {};
|
2019-05-12 23:56:46 +05:30
|
|
|
return;
|
|
|
|
}
|
2019-07-27 02:03:33 +05:30
|
|
|
const pendingEventsByRoomId = await this._getPendingEventsByRoom(txn);
|
2019-05-12 23:56:46 +05:30
|
|
|
// load rooms
|
|
|
|
const rooms = await txn.roomSummary.getAll();
|
|
|
|
await Promise.all(rooms.map(summary => {
|
2019-07-27 14:10:56 +05:30
|
|
|
const room = this.createRoom(summary.roomId, pendingEventsByRoomId.get(summary.roomId));
|
2019-05-12 23:56:46 +05:30
|
|
|
return room.load(summary, txn);
|
|
|
|
}));
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-04-20 23:18:21 +05:30
|
|
|
get isStarted() {
|
|
|
|
return this._sendScheduler.isStarted;
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:46:16 +05:30
|
|
|
stop() {
|
|
|
|
this._sendScheduler.stop();
|
|
|
|
}
|
|
|
|
|
2020-04-20 23:18:21 +05:30
|
|
|
async start(lastVersionResponse) {
|
|
|
|
if (lastVersionResponse) {
|
|
|
|
// store /versions response
|
|
|
|
const txn = await this._storage.readWriteTxn([
|
|
|
|
this._storage.storeNames.session
|
|
|
|
]);
|
|
|
|
const newSessionData = Object.assign({}, this._session, {serverVersions: lastVersionResponse});
|
|
|
|
txn.session.set(newSessionData);
|
|
|
|
// TODO: what can we do if this throws?
|
|
|
|
await txn.complete();
|
|
|
|
this._session = newSessionData;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._sendScheduler.start();
|
2019-07-27 14:10:56 +05:30
|
|
|
for (const [, room] of this._rooms) {
|
2019-07-27 02:10:39 +05:30
|
|
|
room.resumeSending();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 02:03:33 +05:30
|
|
|
async _getPendingEventsByRoom(txn) {
|
|
|
|
const pendingEvents = await txn.pendingEvents.getAll();
|
|
|
|
return pendingEvents.reduce((groups, pe) => {
|
|
|
|
const group = groups.get(pe.roomId);
|
|
|
|
if (group) {
|
|
|
|
group.push(pe);
|
|
|
|
} else {
|
|
|
|
groups.set(pe.roomId, [pe]);
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}, new Map());
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:18:16 +05:30
|
|
|
get rooms() {
|
|
|
|
return this._rooms;
|
|
|
|
}
|
|
|
|
|
2019-07-27 02:03:33 +05:30
|
|
|
createRoom(roomId, pendingEvents) {
|
2019-05-12 23:56:46 +05:30
|
|
|
const room = new Room({
|
2019-03-09 00:33:18 +05:30
|
|
|
roomId,
|
|
|
|
storage: this._storage,
|
|
|
|
emitCollectionChange: this._roomUpdateCallback,
|
|
|
|
hsApi: this._hsApi,
|
2019-07-27 02:03:33 +05:30
|
|
|
sendScheduler: this._sendScheduler,
|
|
|
|
pendingEvents,
|
2019-07-29 13:53:15 +05:30
|
|
|
user: this._user,
|
2019-03-09 00:33:18 +05:30
|
|
|
});
|
2019-05-12 23:56:46 +05:30
|
|
|
this._rooms.add(roomId, room);
|
|
|
|
return room;
|
|
|
|
}
|
2018-12-21 19:05:24 +05:30
|
|
|
|
2020-03-15 01:15:36 +05:30
|
|
|
writeSync(syncToken, syncFilterId, accountData, txn) {
|
2019-05-12 23:56:46 +05:30
|
|
|
if (syncToken !== this._session.syncToken) {
|
2020-03-15 01:15:36 +05:30
|
|
|
// don't modify this._session because transaction might still fail
|
|
|
|
const newSessionData = Object.assign({}, this._session, {syncToken, syncFilterId});
|
|
|
|
txn.session.set(newSessionData);
|
|
|
|
return newSessionData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
afterSync(newSessionData) {
|
|
|
|
if (newSessionData) {
|
|
|
|
// sync transaction succeeded, modify object state now
|
|
|
|
this._session = newSessionData;
|
2019-05-12 23:56:46 +05:30
|
|
|
}
|
|
|
|
}
|
2019-02-07 05:50:27 +05:30
|
|
|
|
2019-05-12 23:56:46 +05:30
|
|
|
get syncToken() {
|
|
|
|
return this._session.syncToken;
|
|
|
|
}
|
2019-06-16 14:23:23 +05:30
|
|
|
|
2019-10-12 23:54:09 +05:30
|
|
|
get syncFilterId() {
|
|
|
|
return this._session.syncFilterId;
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:53:15 +05:30
|
|
|
get user() {
|
|
|
|
return this._user;
|
2019-06-16 14:23:23 +05:30
|
|
|
}
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
2020-03-15 01:15:36 +05:30
|
|
|
|
|
|
|
export function tests() {
|
2020-03-15 02:08:37 +05:30
|
|
|
function createStorageMock(session, pendingEvents = []) {
|
2020-03-15 01:15:36 +05:30
|
|
|
return {
|
|
|
|
readTxn() {
|
|
|
|
return Promise.resolve({
|
|
|
|
session: {
|
|
|
|
get() {
|
|
|
|
return Promise.resolve(Object.assign({}, session));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pendingEvents: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve(pendingEvents);
|
|
|
|
}
|
2020-03-15 02:08:37 +05:30
|
|
|
},
|
|
|
|
roomSummary: {
|
|
|
|
getAll() {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
});
|
2020-03-15 02:08:37 +05:30
|
|
|
},
|
|
|
|
storeNames: {}
|
2020-03-15 01:15:36 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"session data is not modified until after sync": async (assert) => {
|
|
|
|
const session = new Session({storage: createStorageMock({
|
|
|
|
syncToken: "a",
|
|
|
|
syncFilterId: 5,
|
2020-03-15 02:08:37 +05:30
|
|
|
}), sessionInfo: {userId: ""}});
|
2020-03-15 01:15:36 +05:30
|
|
|
await session.load();
|
|
|
|
let txnSetCalled = false;
|
|
|
|
const syncTxn = {
|
|
|
|
session: {
|
|
|
|
set({syncToken, syncFilterId}) {
|
|
|
|
txnSetCalled = true;
|
2020-03-15 02:08:37 +05:30
|
|
|
assert.equal(syncToken, "b");
|
|
|
|
assert.equal(syncFilterId, 6);
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const newSessionData = session.writeSync("b", 6, {}, syncTxn);
|
|
|
|
assert(txnSetCalled);
|
2020-03-15 02:08:37 +05:30
|
|
|
assert.equal(session.syncToken, "a");
|
|
|
|
assert.equal(session.syncFilterId, 5);
|
2020-03-15 01:15:36 +05:30
|
|
|
session.afterSync(newSessionData);
|
2020-03-15 02:08:37 +05:30
|
|
|
assert.equal(session.syncToken, "b");
|
|
|
|
assert.equal(session.syncFilterId, 6);
|
2020-03-15 01:15:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|