Skip to content

Commit

Permalink
Merge pull request #239 from Automattic/single-session
Browse files Browse the repository at this point in the history
Single session
  • Loading branch information
psrpinto authored Nov 30, 2023
2 parents e7dbbef + 0954d74 commit 2ba3019
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
/release/
.env.*.local
/svn/
/.yarn/
.yarn*
5 changes: 0 additions & 5 deletions .yarnrc

This file was deleted.

1 change: 0 additions & 1 deletion .yarnrc.yml

This file was deleted.

16 changes: 13 additions & 3 deletions frontend/iframe/styles/theme/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,19 @@ body {
320px 1fr;
}

/* Always show the back button in single-room mode. */
.RootView.single-room-mode .close-middle {
display: block;
/* Don't show the back button when in a Room, when in single-room mode. */
.RootView.single-room-mode .RoomHeader .close-middle {
display: none !important;
}

/* Don't show the back button in room list. */
.LeftPanel .close-session {
display: none !important;
}

/* Don't show the back button in the login screen. */
.LoginView_back {
display: none !important;
}

/* Remove horizontal scrollbars in pre-session screen. */
Expand Down
12 changes: 0 additions & 12 deletions frontend/iframe/viewmodels/RoomViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { SegmentType } from "hydrogen-web/src/domain/navigation";
import { Segment } from "hydrogen-web/src/domain/navigation/Navigation";
import { RoomViewModel as BaseRoomViewModel } from "hydrogen-web/src/domain/session/room/RoomViewModel";
import { URLRouter } from "../platform/URLRouter";

Expand All @@ -18,16 +16,6 @@ export class RoomViewModel extends BaseRoomViewModel {
return super.urlRouter;
}

get closeUrl() {
if (this.singleRoomMode) {
const path = this.navigation.path.with(new Segment<SegmentType>("session"));

return this.urlRouter.urlForPath(path);
}

return super.closeUrl;
}

get singleRoomMode(): boolean {
return this._singleRoomMode;
}
Expand Down
80 changes: 31 additions & 49 deletions frontend/iframe/viewmodels/RootViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,16 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
const isLogin = this.navigation.path.get("login");
const logoutSessionId = this.navigation.path.get("logout")?.value;
const isForcedLogout = this.navigation.path.get("forced")?.value;
const sessionId = this.navigation.path.get("session")?.value;
const loginToken = this.navigation.path.get("sso")?.value;

let sessionId = this.navigation.path.get("session")?.value;
if (sessionId === true) {
// When logging out, we end up here (sessionId = true).
// Since user is now logged out and as there can only be a single session,
// we want to show the login screen directly.
sessionId = null;
}

if (isLogin) {
if (this.activeSection !== Section.Login) {
this._showLogin(undefined);
Expand All @@ -121,10 +128,6 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
if (this.activeSection !== Section.Logout) {
this._showLogout(logoutSessionId);
}
} else if (sessionId === true) {
if (this.activeSection !== Section.SessionPicker) {
void this._showPicker();
}
} else if (sessionId) {
const singleRoomId = await this.getSingleRoomId();
if (singleRoomId) {
Expand Down Expand Up @@ -153,76 +156,55 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
}
} else {
try {
await this._showInitialScreen(shouldRestoreLastUrl);
await this._showInitialScreen();
} catch (err) {
console.error(err);
this._setSection(() => this._error = err);
}
}
}

private async _showInitialScreen(shouldRestoreLastUrl: boolean) {
const sessionInfos = await this.platform.sessionInfoStorage.getAll();
const singleRoomId = await this.getSingleRoomId();
private async _showInitialScreen() {
let sessionInfos = await this.platform.sessionInfoStorage.getAll();

if (shouldRestoreLastUrl && singleRoomId) {
// Do not restore last URL to Login if we're in single-room mode.
// We do this so that we can try guest login when appropriate.
const willShowLogin = this.platform.history.getLastSessionUrl() === "#/login";
if (willShowLogin) {
shouldRestoreLastUrl = false;
// In previous versions, it was possible to have multiple sessions open.
// When we have multiple sessions, we want to log out all of them except one.
if (sessionInfos.length > 1) {
for (let i = 0; i < sessionInfos.length - 1; i++) {
await this.platform.sessionInfoStorage.delete(sessionInfos[i].id);
}

// Do not restore last URL to session picker if we're in single-room mode and there are zero or one sessions.
// We do this so that we can try guest login when there are no sessions, or in the case where there is one
// session, use that session.
const willShowSessionPicker = this.platform.history.getLastSessionUrl() === "#/session";
if (shouldRestoreLastUrl && willShowSessionPicker && sessionInfos.length <= 1) {
shouldRestoreLastUrl = false;
sessionInfos = await this.platform.sessionInfoStorage.getAll();
if (sessionInfos.length > 1) {
console.error("Expected to have a single session, but multiple sessions were found.");
}
}

// Only restore last url if there is already a session.
// This will result in the user being sent to login screen.
let shouldRestoreLastUrl = sessionInfos.length > 1;

// We never want to show the session picker, even if it was the last url.
const willShowSessionPicker = this.platform.history.getLastSessionUrl() === "#/session";
if (willShowSessionPicker) {
shouldRestoreLastUrl = false;
}

if (shouldRestoreLastUrl && this.urlRouter.tryRestoreLastUrl()) {
// Restored last URL.
// By the time we get here, _applyNavigation() has run for the restored URL, so we have nothing else
// to do.
// By the time we get here, _applyNavigation() has run for the restored URL, so we have nothing else to do.
return;
}

// We were not able to restore the last URL.
// So we send the user to the screen that makes the most sense, according to how many sessions they have.

// Go to Login or, when in single-room mode, try registering guest user.
if (sessionInfos.length === 0) {
if (!singleRoomId) {
this.navigation.push(Section.Login);
return;
}

// Attempt to log in as guest. If it fails, go to Login.
const homeserver = await lookupHomeserver(singleRoomId.split(':')[1], this.platform.request);
const client = new Client(this.platform);

await client.doGuestLogin(homeserver);
if (client.loadError) {
console.warn("Failed to login as guest. Guest registration is probably disabled on the homeserver", client.loadError);
this.navigation.push(Section.Login);
return;
}

this._pendingClient = client;
this.navigation.push(Section.Session, client.sessionId);
return;
}

// Open session.
if (sessionInfos.length === 1) {
this.navigation.push(Section.Session, sessionInfos[0].id);
return;
}

// Open session picker.
this.navigation.push(Section.Session);
this.navigation.push(Section.Login);
}

private async resolveRoomAlias(roomIdOrAlias: string): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions frontend/iframe/views/RootView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 { UnknownRoomView } from "hydrogen-web/src/platform/web/ui/session/room/UnknownRoomView";
import { Section } from "../platform/Navigation";
import { RootViewModel } from "../viewmodels/RootViewModel";
import { SessionView } from "./SessionView";
Expand Down Expand Up @@ -34,7 +34,7 @@ export class RootView extends TemplateView<RootViewModel> {
case Section.SessionPicker:
return new SessionPickerView(vm.sessionPickerViewModel);
case Section.Redirecting:
return new StaticView(t => t.p("Redirecting..."));
return new StaticView(t => t.p("Loading..."));
case Section.SessionLoading:
return new SessionLoadView(vm.sessionLoadViewModel);
case Section.UnknownRoom:
Expand Down

0 comments on commit 2ba3019

Please sign in to comment.