Skip to content

Commit

Permalink
show unknown room when room is loaded in a single user mode, and crea…
Browse files Browse the repository at this point in the history
…tes a guest session if necessary
  • Loading branch information
ashfame committed Mar 1, 2023
1 parent 01065ad commit 476875f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions frontend/iframe/platform/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum Section {
SessionLoading = "loading",
Session = "session",
Error = "error",
UnknownRoom = "unknown-room",
Redirecting = "redirecting",
}

Expand Down
75 changes: 75 additions & 0 deletions frontend/iframe/viewmodels/RootViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LogoutViewModel } from "hydrogen-web/src/domain/LogoutViewModel";
import { SegmentType } from "hydrogen-web/src/domain/navigation";
import { SessionLoadViewModel } from "hydrogen-web/src/domain/SessionLoadViewModel";
import { SessionPickerViewModel } from "hydrogen-web/src/domain/SessionPickerViewModel";
import { UnknownRoomViewModel } from "hydrogen-web/src/domain/session/room/UnknownRoomViewModel";
import { Options as BaseOptions, ViewModel } from "hydrogen-web/src/domain/ViewModel";
import { Client } from "hydrogen-web/src/matrix/Client.js";
import { HomeServerApi } from "hydrogen-web/src/matrix/net/HomeServerApi";
Expand All @@ -22,6 +23,7 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
private _sessionPickerViewModel: SessionPickerViewModel | undefined;
private _sessionLoadViewModel: SessionLoadViewModel | undefined;
private _sessionViewModel: SessionViewModel | undefined;
private _unknownRoomViewModel: UnknownRoomViewModel | undefined;
private _pendingClient: Client;
private readonly _singleRoomIdOrAlias: string | undefined;
private _resolvedSingleRoomId: string | undefined;
Expand All @@ -46,6 +48,8 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
return Section.SessionLoading;
} else if (this._sessionViewModel) {
return Section.Session;
} else if (this._unknownRoomViewModel) {
return Section.UnknownRoom;
} else {
return Section.Redirecting;
}
Expand All @@ -71,6 +75,10 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
return this._sessionLoadViewModel;
}

public get unknownRoomViewModel(): UnknownRoomViewModel | undefined {
return this._unknownRoomViewModel;
}

public get sessionViewModel(): SessionViewModel | undefined {
return this._sessionViewModel;
}
Expand Down Expand Up @@ -157,6 +165,22 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
this._showLogin(loginToken);
}
} else {
if (this._singleRoomIdOrAlias) {
// No active session but we're in single-room mode.
if (!this._resolvedSingleRoomId) {
try {
this._resolvedSingleRoomId = await this.resolveRoomAlias(this._singleRoomIdOrAlias);
} catch (error) {
console.warn(error);
}
}

if (this._resolvedSingleRoomId) {
await this._showUnknownRoom(this._resolvedSingleRoomId);
return;
}
}

try {
if (!(shouldRestoreLastUrl && this.urlRouter.tryRestoreLastUrl())) {
const sessionInfos = await this.platform.sessionInfoStorage.getAll();
Expand Down Expand Up @@ -204,6 +228,29 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
return response.room_id;
}

private async isWorldReadableRoom(roomId: string, sessionId: string): Promise<boolean> {
const sessionInfo = await this.platform.sessionInfoStorage.get(sessionId);
if (!sessionInfo) {
console.error(`Could not find session for id ${sessionId}`);
return false;
}

const homeserver = await lookupHomeserver(roomId.split(':')[1], this.platform.request);
const homeserverApi = new HomeServerApi({
homeserver: homeserver,
request: this.platform.request,
accessToken: sessionInfo.accessToken,
reconnector: this.platform.reconnector,
});

return homeserverApi.state(roomId, 'm.room.history_visibility', '').response().then(
response => response.history_visibility === 'world_readable'
).catch(err => {
console.error(err);
return false;
});
}

private _showLogin(loginToken: string | undefined) {
this._setSection(() => {
const options = this.childOptions({
Expand Down Expand Up @@ -271,6 +318,34 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
});
}

private async _showUnknownRoom(roomId: string) {
const client = new Client(this.platform);
let chosenSession;

let sessionInfos = await this.platform.sessionInfoStorage.getAll();
if (sessionInfos.length === 0) {
const homeserver = await lookupHomeserver(roomId.split(':')[1], this.platform.request);
await client.doGuestLogin(homeserver);
sessionInfos = await this.platform.sessionInfoStorage.getAll();
chosenSession = sessionInfos[0];
} else {
chosenSession = sessionInfos[0];
await client.startWithExistingSession(chosenSession.id);
}
console.info('chosenSession',chosenSession);
// @TODO need to stop/prevent sync for guest account (when creating a new one or reusing an existing one)

this._setSection(() => {
this._unknownRoomViewModel = new UnknownRoomViewModel(this.childOptions({
roomIdOrAlias: roomId,
session: chosenSession,
isWorldReadablePromise: this.isWorldReadableRoom(roomId, chosenSession.id),
}));
});

this.navigation.push("session", chosenSession.id);
}

private _setSection(setter: Function) {
// Clear all members the activeSection depends on.
this._error = undefined;
Expand Down
3 changes: 3 additions & 0 deletions frontend/iframe/views/RootView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TemplateView } from "hydrogen-web/src/platform/web/ui/general/TemplateV
import { LoginView } from "hydrogen-web/src/platform/web/ui/login/LoginView";
import { SessionLoadView } from "hydrogen-web/src/platform/web/ui/login/SessionLoadView";
import { SessionPickerView } from "hydrogen-web/src/platform/web/ui/login/SessionPickerView";
import { UnknownRoomView } from "hydrogen-web/src/platform/web/ui/session/room/UnknownRoomView";
import { LogoutView } from "hydrogen-web/src/platform/web/ui/LogoutView";
import { Section } from "../platform/Navigation";
import { RootViewModel } from "../viewmodels/RootViewModel";
Expand Down Expand Up @@ -36,6 +37,8 @@ export class RootView extends TemplateView<RootViewModel> {
return new StaticView(t => t.p("Redirecting..."));
case Section.SessionLoading:
return new SessionLoadView(vm.sessionLoadViewModel);
case Section.UnknownRoom:
return new UnknownRoomView(vm.unknownRoomViewModel);
case Section.Error:
return new StaticView(t => {
return t.div({ className: "StatusView" }, [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"@wordpress/compose": "^5.17.0",
"bs58": "^5.0.0",
"hydrogen-web": "Automattic/hydrogen-web#peeking_unknown_rooms",
"hydrogen-web": "Automattic/hydrogen-web#peeking_with_guest_login",
"node-html-parser": "^4.0.0"
},
"resolutions": {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5715,9 +5715,9 @@ human-signals@^2.1.0:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==

hydrogen-web@Automattic/hydrogen-web#peeking_unknown_rooms:
hydrogen-web@Automattic/hydrogen-web#peeking_with_guest_login:
version "0.3.8"
resolved "https://codeload.github.com/Automattic/hydrogen-web/tar.gz/07cce3857a163c17ee68aa86b304bd4430de9bfd"
resolved "https://codeload.github.com/Automattic/hydrogen-web/tar.gz/8b71ac900e683da110077d9ae87e6902dfb73399"
dependencies:
"@matrix-org/olm" "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz"
another-json "^0.2.0"
Expand Down

0 comments on commit 476875f

Please sign in to comment.