Compare commits

...
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.

8 commits

Author SHA1 Message Date
Richard Lewis
5d5caa3b35 Mock a response object 2021-07-08 00:27:54 +01:00
Richard Lewis
26ec4853e7 Always resolve with a promise 2021-07-08 00:13:03 +01:00
Richard Lewis
ffa8966058 Return a function, for consistency 2021-07-08 00:00:11 +01:00
Richard Lewis
fd749ecc2b Don't attempt to send read receipts for guest accounts 2021-07-07 15:18:08 +01:00
Richard Lewis
8144e66d00 Revert nullish coalescing 2021-07-07 15:17:53 +01:00
Richard Lewis
250054059c Merge branch 'master' of github.com:vector-im/hydrogen-web into rxl881/ems-guest-sync 2021-07-07 13:47:45 +01:00
Richard Lewis
bb4a859c87 Store guest status on SessionContainer and HomeServerApi instances. 2021-06-09 19:28:50 +01:00
Richard Lewis
b274607f41 Allow registering and logging in as guest 2021-06-04 20:53:37 +01:00
5 changed files with 45 additions and 12 deletions

View file

@ -36,7 +36,7 @@ export class RoomMemberTile extends SimpleTile {
if (!content.displayname) { if (!content.displayname) {
return `${stateKey} removed their name (${prevContent.displayname})`; return `${stateKey} removed their name (${prevContent.displayname})`;
} }
return `${prevContent.displayname ?? stateKey} changed their name to ${content.displayname}`; return `${prevContent.displayname ? prevContent.displayname : stateKey} changed their name to ${content.displayname}`;
} }
} else if (membership === "join") { } else if (membership === "join") {
return `${targetName} joined the room`; return `${targetName} joined the room`;

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,19 +98,29 @@ export class SessionContainer {
}); });
} }
async startWithLogin(homeServer, username, password) { 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({
const loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response(); homeServer,
request,
guestAccount: this._guestAccount
});
let loginData;
if (guestAccount === true) {
loginData = await hsApi.guestLogin("Hydrogen", {log}).response();
} else {
loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response();
}
const sessionId = this.createNewSessionId(); const sessionId = this.createNewSessionId();
sessionInfo = { sessionInfo = {
id: sessionId, id: sessionId,
@ -117,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);
@ -156,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({
@ -168,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);
@ -183,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);
} }
@ -126,9 +131,12 @@ export class HomeServerApi {
} }
receipt(roomId, receiptType, eventId, options = null) { receipt(roomId, receiptType, eventId, options = null) {
if(!this._isGuestAccount()) {
return this._post(`/rooms/${encodeURIComponent(roomId)}/receipt/${encodeURIComponent(receiptType)}/${encodeURIComponent(eventId)}`, return this._post(`/rooms/${encodeURIComponent(roomId)}/receipt/${encodeURIComponent(receiptType)}/${encodeURIComponent(eventId)}`,
{}, {}, options); {}, {}, options);
} }
return {response: () => Promise.resolve(null)};
}
passwordLogin(username, password, initialDeviceDisplayName, options = null) { passwordLogin(username, password, initialDeviceDisplayName, options = null) {
return this._unauthedRequest("POST", this._url("/login"), null, { return this._unauthedRequest("POST", this._url("/login"), null, {
@ -142,6 +150,12 @@ export class HomeServerApi {
}, options); }, options);
} }
guestLogin(initialDeviceDisplayName, options = null) {
return this._unauthedRequest("POST", this._url(`/register`), {kind: 'guest'}, {
"initial_device_display_name": initialDeviceDisplayName
}, options);
}
createFilter(userId, filter, options = null) { createFilter(userId, filter, options = null) {
return this._post(`/user/${encodeURIComponent(userId)}/filter`, null, filter, options); return this._post(`/user/${encodeURIComponent(userId)}/filter`, null, filter, options);
} }
@ -205,6 +219,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);
} }