From bb4a859c87bc998c3ccca0ff5332f26b9692526d Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Wed, 9 Jun 2021 19:28:50 +0100 Subject: [PATCH] Store guest status on SessionContainer and HomeServerApi instances. --- src/matrix/SessionContainer.js | 21 +++++++++++++++------ src/matrix/Sync.js | 2 +- src/matrix/net/HomeServerApi.js | 11 ++++++++++- src/matrix/net/RequestScheduler.js | 3 ++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 6a66178f..7a5d5fba 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -51,7 +51,7 @@ function normalizeHomeserver(homeServer) { } export class SessionContainer { - constructor({platform, olmPromise, workerPromise}) { + constructor({platform, olmPromise, workerPromise, guestAccount = false}) { this._platform = platform; this._sessionStartedByReconnector = false; this._status = new ObservableValue(LoadStatus.NotLoading); @@ -65,6 +65,7 @@ export class SessionContainer { this._requestScheduler = null; this._olmPromise = olmPromise; this._workerPromise = workerPromise; + this._guestAccount = guestAccount; } createNewSessionId() { @@ -97,20 +98,25 @@ export class SessionContainer { }); } - async startWithLogin(homeServer, username, password, guest = false) { + async startWithLogin(homeServer, username, password, guestAccount = false) { if (this._status.get() !== LoadStatus.NotLoading) { return; } await this._platform.logger.run("login", async log => { this._status.set(LoadStatus.Login); + this._guestAccount = guestAccount; homeServer = normalizeHomeserver(homeServer); const clock = this._platform.clock; let sessionInfo; try { const request = this._platform.request; - const hsApi = new HomeServerApi({homeServer, request}); + const hsApi = new HomeServerApi({ + homeServer, + request, + guestAccount: this._guestAccount + }); let loginData; - if (guest === true) { + if (guestAccount === true) { loginData = await hsApi.guestLogin("Hydrogen", {log}).response(); } else { loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response(); @@ -122,7 +128,8 @@ export class SessionContainer { userId: loginData.user_id, homeServer: homeServer, accessToken: loginData.access_token, - lastUsed: clock.now() + lastUsed: clock.now(), + guestAccount: this._guestAccount }; log.set("id", sessionId); await this._platform.sessionInfoStorage.add(sessionInfo); @@ -161,6 +168,7 @@ export class SessionContainer { async _loadSessionInfo(sessionInfo, isNewLogin, log) { log.set("appVersion", this._platform.version); const clock = this._platform.clock; + this._guestAccount = !!sessionInfo.guestAccount; this._sessionStartedByReconnector = false; this._status.set(LoadStatus.Loading); this._reconnector = new Reconnector({ @@ -173,6 +181,7 @@ export class SessionContainer { accessToken: sessionInfo.accessToken, request: this._platform.request, reconnector: this._reconnector, + guestAccount: this._guestAccount, }); this._sessionId = sessionInfo.id; this._storage = await this._platform.storageFactory.create(sessionInfo.id); @@ -188,7 +197,7 @@ export class SessionContainer { if (this._workerPromise) { olmWorker = await this._workerPromise; } - this._requestScheduler = new RequestScheduler({hsApi, clock}); + this._requestScheduler = new RequestScheduler({hsApi, clock, guestAccount: this._guestAccount}); this._requestScheduler.start(); const mediaRepository = new MediaRepository({ homeServer: sessionInfo.homeServer, diff --git a/src/matrix/Sync.js b/src/matrix/Sync.js index 62bb67bd..4b9f61cb 100644 --- a/src/matrix/Sync.js +++ b/src/matrix/Sync.js @@ -181,7 +181,7 @@ export class Sync { async _syncRequest(syncToken, timeout, log) { let {syncFilterId} = this._session; - if (typeof syncFilterId !== "string") { + if (typeof syncFilterId !== "string" && !this._hsApi._scheduler._guestAccount) { this._currentRequest = this._hsApi.createFilter(this._session.user.id, {room: {state: {lazy_load_members: true}}}, {log}); syncFilterId = (await this._currentRequest.response()).filter_id; } diff --git a/src/matrix/net/HomeServerApi.js b/src/matrix/net/HomeServerApi.js index 1bac4936..e06fb2d4 100644 --- a/src/matrix/net/HomeServerApi.js +++ b/src/matrix/net/HomeServerApi.js @@ -19,13 +19,14 @@ import {encodeQueryParams, encodeBody} from "./common.js"; import {HomeServerRequest} from "./HomeServerRequest.js"; export class HomeServerApi { - constructor({homeServer, accessToken, request, reconnector}) { + constructor({homeServer, accessToken, request, reconnector, guestAccount = false}) { // store these both in a closure somehow so it's harder to get at in case of XSS? // one could change the homeserver as well so the token gets sent there, so both must be protected from read/write this._homeserver = homeServer; this._accessToken = accessToken; this._requestFn = request; this._reconnector = reconnector; + this._guestAccount = guestAccount; } _url(csPath) { @@ -104,6 +105,10 @@ export class HomeServerApi { } sync(since, filter, timeout, options = null) { + const _filter = {room: {state: {lazy_load_members: true}}}; + if (this._isGuestAccount()) { + filter = _filter; + } return this._get("/sync", {since, timeout, filter}, null, options); } @@ -211,6 +216,10 @@ export class HomeServerApi { forget(roomId, options = null) { return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, null, null, options); } + + _isGuestAccount() { + return this._guestAccount; + } } import {Request as MockRequest} from "../../mocks/Request.js"; diff --git a/src/matrix/net/RequestScheduler.js b/src/matrix/net/RequestScheduler.js index 53ab50ac..25937b35 100644 --- a/src/matrix/net/RequestScheduler.js +++ b/src/matrix/net/RequestScheduler.js @@ -60,13 +60,14 @@ for (const methodName of Object.getOwnPropertyNames(HomeServerApi.prototype)) { } export class RequestScheduler { - constructor({hsApi, clock}) { + constructor({hsApi, clock, guestAccount = false}) { this._hsApi = hsApi; this._clock = clock; this._requests = new Set(); this._isRateLimited = false; this._isDrainingRateLimit = false; this._stopped = true; + this._guestAccount = guestAccount; this._wrapper = new HomeServerApiWrapper(this); }