Store guest status on SessionContainer and HomeServerApi instances.

This commit is contained in:
Richard Lewis 2021-06-09 19:28:50 +01:00
parent b274607f41
commit bb4a859c87
4 changed files with 28 additions and 9 deletions

View file

@ -51,7 +51,7 @@ function normalizeHomeserver(homeServer) {
} }
export class SessionContainer { export class SessionContainer {
constructor({platform, olmPromise, workerPromise}) { constructor({platform, olmPromise, workerPromise, guestAccount = false}) {
this._platform = platform; this._platform = platform;
this._sessionStartedByReconnector = false; this._sessionStartedByReconnector = false;
this._status = new ObservableValue(LoadStatus.NotLoading); this._status = new ObservableValue(LoadStatus.NotLoading);
@ -65,6 +65,7 @@ export class SessionContainer {
this._requestScheduler = null; this._requestScheduler = null;
this._olmPromise = olmPromise; this._olmPromise = olmPromise;
this._workerPromise = workerPromise; this._workerPromise = workerPromise;
this._guestAccount = guestAccount;
} }
createNewSessionId() { 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) { if (this._status.get() !== LoadStatus.NotLoading) {
return; return;
} }
await this._platform.logger.run("login", async log => { await this._platform.logger.run("login", async log => {
this._status.set(LoadStatus.Login); this._status.set(LoadStatus.Login);
this._guestAccount = guestAccount;
homeServer = normalizeHomeserver(homeServer); homeServer = normalizeHomeserver(homeServer);
const clock = this._platform.clock; const clock = this._platform.clock;
let sessionInfo; let sessionInfo;
try { try {
const request = this._platform.request; const request = this._platform.request;
const hsApi = new HomeServerApi({homeServer, request}); const hsApi = new HomeServerApi({
homeServer,
request,
guestAccount: this._guestAccount
});
let loginData; let loginData;
if (guest === true) { if (guestAccount === true) {
loginData = await hsApi.guestLogin("Hydrogen", {log}).response(); loginData = await hsApi.guestLogin("Hydrogen", {log}).response();
} else { } else {
loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response(); loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response();
@ -122,7 +128,8 @@ export class SessionContainer {
userId: loginData.user_id, userId: loginData.user_id,
homeServer: homeServer, homeServer: homeServer,
accessToken: loginData.access_token, accessToken: loginData.access_token,
lastUsed: clock.now() lastUsed: clock.now(),
guestAccount: this._guestAccount
}; };
log.set("id", sessionId); log.set("id", sessionId);
await this._platform.sessionInfoStorage.add(sessionInfo); await this._platform.sessionInfoStorage.add(sessionInfo);
@ -161,6 +168,7 @@ export class SessionContainer {
async _loadSessionInfo(sessionInfo, isNewLogin, log) { async _loadSessionInfo(sessionInfo, isNewLogin, log) {
log.set("appVersion", this._platform.version); log.set("appVersion", this._platform.version);
const clock = this._platform.clock; const clock = this._platform.clock;
this._guestAccount = !!sessionInfo.guestAccount;
this._sessionStartedByReconnector = false; this._sessionStartedByReconnector = false;
this._status.set(LoadStatus.Loading); this._status.set(LoadStatus.Loading);
this._reconnector = new Reconnector({ this._reconnector = new Reconnector({
@ -173,6 +181,7 @@ export class SessionContainer {
accessToken: sessionInfo.accessToken, accessToken: sessionInfo.accessToken,
request: this._platform.request, request: this._platform.request,
reconnector: this._reconnector, reconnector: this._reconnector,
guestAccount: this._guestAccount,
}); });
this._sessionId = sessionInfo.id; this._sessionId = sessionInfo.id;
this._storage = await this._platform.storageFactory.create(sessionInfo.id); this._storage = await this._platform.storageFactory.create(sessionInfo.id);
@ -188,7 +197,7 @@ export class SessionContainer {
if (this._workerPromise) { if (this._workerPromise) {
olmWorker = await this._workerPromise; olmWorker = await this._workerPromise;
} }
this._requestScheduler = new RequestScheduler({hsApi, clock}); this._requestScheduler = new RequestScheduler({hsApi, clock, guestAccount: this._guestAccount});
this._requestScheduler.start(); this._requestScheduler.start();
const mediaRepository = new MediaRepository({ const mediaRepository = new MediaRepository({
homeServer: sessionInfo.homeServer, homeServer: sessionInfo.homeServer,

View file

@ -181,7 +181,7 @@ export class Sync {
async _syncRequest(syncToken, timeout, log) { async _syncRequest(syncToken, timeout, log) {
let {syncFilterId} = this._session; 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}); this._currentRequest = this._hsApi.createFilter(this._session.user.id, {room: {state: {lazy_load_members: true}}}, {log});
syncFilterId = (await this._currentRequest.response()).filter_id; syncFilterId = (await this._currentRequest.response()).filter_id;
} }

View file

@ -19,13 +19,14 @@ import {encodeQueryParams, encodeBody} from "./common.js";
import {HomeServerRequest} from "./HomeServerRequest.js"; import {HomeServerRequest} from "./HomeServerRequest.js";
export class HomeServerApi { 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? // 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 // 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._homeserver = homeServer;
this._accessToken = accessToken; this._accessToken = accessToken;
this._requestFn = request; this._requestFn = request;
this._reconnector = reconnector; this._reconnector = reconnector;
this._guestAccount = guestAccount;
} }
_url(csPath) { _url(csPath) {
@ -104,6 +105,10 @@ export class HomeServerApi {
} }
sync(since, filter, timeout, options = null) { 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); return this._get("/sync", {since, timeout, filter}, null, options);
} }
@ -211,6 +216,10 @@ export class HomeServerApi {
forget(roomId, options = null) { forget(roomId, options = null) {
return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, null, null, options); return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, null, null, options);
} }
_isGuestAccount() {
return this._guestAccount;
}
} }
import {Request as MockRequest} from "../../mocks/Request.js"; import {Request as MockRequest} from "../../mocks/Request.js";

View file

@ -60,13 +60,14 @@ for (const methodName of Object.getOwnPropertyNames(HomeServerApi.prototype)) {
} }
export class RequestScheduler { export class RequestScheduler {
constructor({hsApi, clock}) { constructor({hsApi, clock, guestAccount = false}) {
this._hsApi = hsApi; this._hsApi = hsApi;
this._clock = clock; this._clock = clock;
this._requests = new Set(); this._requests = new Set();
this._isRateLimited = false; this._isRateLimited = false;
this._isDrainingRateLimit = false; this._isDrainingRateLimit = false;
this._stopped = true; this._stopped = true;
this._guestAccount = guestAccount;
this._wrapper = new HomeServerApiWrapper(this); this._wrapper = new HomeServerApiWrapper(this);
} }