Skip to content

Commit 2ba3019

Browse files
authored
Merge pull request #239 from Automattic/single-session
Single session
2 parents e7dbbef + 0954d74 commit 2ba3019

File tree

7 files changed

+47
-73
lines changed

7 files changed

+47
-73
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
/release/
88
.env.*.local
99
/svn/
10-
/.yarn/
10+
.yarn*

.yarnrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

.yarnrc.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

frontend/iframe/styles/theme/theme.css

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ body {
3434
320px 1fr;
3535
}
3636

37-
/* Always show the back button in single-room mode. */
38-
.RootView.single-room-mode .close-middle {
39-
display: block;
37+
/* Don't show the back button when in a Room, when in single-room mode. */
38+
.RootView.single-room-mode .RoomHeader .close-middle {
39+
display: none !important;
40+
}
41+
42+
/* Don't show the back button in room list. */
43+
.LeftPanel .close-session {
44+
display: none !important;
45+
}
46+
47+
/* Don't show the back button in the login screen. */
48+
.LoginView_back {
49+
display: none !important;
4050
}
4151

4252
/* Remove horizontal scrollbars in pre-session screen. */

frontend/iframe/viewmodels/RoomViewModel.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { SegmentType } from "hydrogen-web/src/domain/navigation";
2-
import { Segment } from "hydrogen-web/src/domain/navigation/Navigation";
31
import { RoomViewModel as BaseRoomViewModel } from "hydrogen-web/src/domain/session/room/RoomViewModel";
42
import { URLRouter } from "../platform/URLRouter";
53

@@ -18,16 +16,6 @@ export class RoomViewModel extends BaseRoomViewModel {
1816
return super.urlRouter;
1917
}
2018

21-
get closeUrl() {
22-
if (this.singleRoomMode) {
23-
const path = this.navigation.path.with(new Segment<SegmentType>("session"));
24-
25-
return this.urlRouter.urlForPath(path);
26-
}
27-
28-
return super.closeUrl;
29-
}
30-
3119
get singleRoomMode(): boolean {
3220
return this._singleRoomMode;
3321
}

frontend/iframe/viewmodels/RootViewModel.ts

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,16 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
106106
const isLogin = this.navigation.path.get("login");
107107
const logoutSessionId = this.navigation.path.get("logout")?.value;
108108
const isForcedLogout = this.navigation.path.get("forced")?.value;
109-
const sessionId = this.navigation.path.get("session")?.value;
110109
const loginToken = this.navigation.path.get("sso")?.value;
111110

111+
let sessionId = this.navigation.path.get("session")?.value;
112+
if (sessionId === true) {
113+
// When logging out, we end up here (sessionId = true).
114+
// Since user is now logged out and as there can only be a single session,
115+
// we want to show the login screen directly.
116+
sessionId = null;
117+
}
118+
112119
if (isLogin) {
113120
if (this.activeSection !== Section.Login) {
114121
this._showLogin(undefined);
@@ -121,10 +128,6 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
121128
if (this.activeSection !== Section.Logout) {
122129
this._showLogout(logoutSessionId);
123130
}
124-
} else if (sessionId === true) {
125-
if (this.activeSection !== Section.SessionPicker) {
126-
void this._showPicker();
127-
}
128131
} else if (sessionId) {
129132
const singleRoomId = await this.getSingleRoomId();
130133
if (singleRoomId) {
@@ -153,76 +156,55 @@ export class RootViewModel extends ViewModel<SegmentType, Options> {
153156
}
154157
} else {
155158
try {
156-
await this._showInitialScreen(shouldRestoreLastUrl);
159+
await this._showInitialScreen();
157160
} catch (err) {
158161
console.error(err);
159162
this._setSection(() => this._error = err);
160163
}
161164
}
162165
}
163166

164-
private async _showInitialScreen(shouldRestoreLastUrl: boolean) {
165-
const sessionInfos = await this.platform.sessionInfoStorage.getAll();
166-
const singleRoomId = await this.getSingleRoomId();
167+
private async _showInitialScreen() {
168+
let sessionInfos = await this.platform.sessionInfoStorage.getAll();
167169

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

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

183+
// Only restore last url if there is already a session.
184+
// This will result in the user being sent to login screen.
185+
let shouldRestoreLastUrl = sessionInfos.length > 1;
186+
187+
// We never want to show the session picker, even if it was the last url.
188+
const willShowSessionPicker = this.platform.history.getLastSessionUrl() === "#/session";
189+
if (willShowSessionPicker) {
190+
shouldRestoreLastUrl = false;
191+
}
192+
185193
if (shouldRestoreLastUrl && this.urlRouter.tryRestoreLastUrl()) {
186194
// Restored last URL.
187-
// By the time we get here, _applyNavigation() has run for the restored URL, so we have nothing else
188-
// to do.
195+
// By the time we get here, _applyNavigation() has run for the restored URL, so we have nothing else to do.
189196
return;
190197
}
191198

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

195-
// Go to Login or, when in single-room mode, try registering guest user.
196-
if (sessionInfos.length === 0) {
197-
if (!singleRoomId) {
198-
this.navigation.push(Section.Login);
199-
return;
200-
}
201-
202-
// Attempt to log in as guest. If it fails, go to Login.
203-
const homeserver = await lookupHomeserver(singleRoomId.split(':')[1], this.platform.request);
204-
const client = new Client(this.platform);
205-
206-
await client.doGuestLogin(homeserver);
207-
if (client.loadError) {
208-
console.warn("Failed to login as guest. Guest registration is probably disabled on the homeserver", client.loadError);
209-
this.navigation.push(Section.Login);
210-
return;
211-
}
212-
213-
this._pendingClient = client;
214-
this.navigation.push(Section.Session, client.sessionId);
215-
return;
216-
}
217-
218-
// Open session.
219202
if (sessionInfos.length === 1) {
220203
this.navigation.push(Section.Session, sessionInfos[0].id);
221204
return;
222205
}
223206

224-
// Open session picker.
225-
this.navigation.push(Section.Session);
207+
this.navigation.push(Section.Login);
226208
}
227209

228210
private async resolveRoomAlias(roomIdOrAlias: string): Promise<string> {

frontend/iframe/views/RootView.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { TemplateView } from "hydrogen-web/src/platform/web/ui/general/TemplateV
44
import { LoginView } from "hydrogen-web/src/platform/web/ui/login/LoginView";
55
import { SessionLoadView } from "hydrogen-web/src/platform/web/ui/login/SessionLoadView";
66
import { SessionPickerView } from "hydrogen-web/src/platform/web/ui/login/SessionPickerView";
7-
import { UnknownRoomView } from "hydrogen-web/src/platform/web/ui/session/room/UnknownRoomView";
87
import { LogoutView } from "hydrogen-web/src/platform/web/ui/LogoutView";
8+
import { UnknownRoomView } from "hydrogen-web/src/platform/web/ui/session/room/UnknownRoomView";
99
import { Section } from "../platform/Navigation";
1010
import { RootViewModel } from "../viewmodels/RootViewModel";
1111
import { SessionView } from "./SessionView";
@@ -34,7 +34,7 @@ export class RootView extends TemplateView<RootViewModel> {
3434
case Section.SessionPicker:
3535
return new SessionPickerView(vm.sessionPickerViewModel);
3636
case Section.Redirecting:
37-
return new StaticView(t => t.p("Redirecting..."));
37+
return new StaticView(t => t.p("Loading..."));
3838
case Section.SessionLoading:
3939
return new SessionLoadView(vm.sessionLoadViewModel);
4040
case Section.UnknownRoom:

0 commit comments

Comments
 (0)