Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft PR for peeking with guest login #7

Open
wants to merge 4 commits into
base: peeking_unknown_rooms
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/domain/session/room/UnknownRoomViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,35 @@ import {ViewModel} from "../../ViewModel";
export class UnknownRoomViewModel extends ViewModel {
constructor(options) {
super(options);
const {roomIdOrAlias, session, isWorldReadablePromise} = options;
const {roomIdOrAlias, session, isWorldReadablePromise, guestJoinAllowed} = options;
this._session = session;
this.roomIdOrAlias = roomIdOrAlias;
this._error = null;
this._busy = false;
this._guestJoinAllowed = typeof guestJoinAllowed !== 'undefined' && guestJoinAllowed;

this.checkingPreviewCapability = true;
isWorldReadablePromise.then(() => {
this.checkingPreviewCapability = false;
this.emitChange('checkingPreviewCapability');
})

// join allowed for the current user/session?
this._joinAllowed = false;
this._session.isGuest().then((isGuest) => {
this._joinAllowed = isGuest ? this._guestJoinAllowed : true;
this.emitChange('joinAllowed');
});
}

get error() {
return this._error?.message;
}

get joinAllowed() {
return this._joinAllowed;
}

async join() {
this._busy = true;
this.emitChange("busy");
Expand Down
21 changes: 19 additions & 2 deletions src/domain/session/room/WorldReadableRoomViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ export class WorldReadableRoomViewModel extends RoomViewModel {
constructor(options) {
options.room.isWorldReadable = true;
super(options);
this._room = options.room;
this._session = options.session;
const {room, session, guestJoinAllowed} = options;
this._room = room;
this._session = session;
this._error = null;
this._busy = false;
this._guestJoinAllowed = typeof guestJoinAllowed !== 'undefined' && guestJoinAllowed;

// join allowed for the current user/session?
this._joinAllowed = false;
this._session.isGuest().then((isGuest) => {
this._joinAllowed = isGuest ? this._guestJoinAllowed : true;
this.emitChange('joinAllowed');
});
}

get kind() {
Expand All @@ -18,6 +27,10 @@ export class WorldReadableRoomViewModel extends RoomViewModel {
return this._busy;
}

get joinAllowed() {
return this._joinAllowed;
}

async join() {
this._busy = true;
this.emitChange("busy");
Expand All @@ -36,6 +49,10 @@ export class WorldReadableRoomViewModel extends RoomViewModel {
}
}

login() {
this.navigation.push("login");
}

dispose() {
super.dispose();

Expand Down
28 changes: 28 additions & 0 deletions src/matrix/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ export class Client {
return registration;
}

async doGuestLogin(homeserver) {
const currentStatus = this._status.get();
if (currentStatus !== LoadStatus.LoginFailed &&
currentStatus !== LoadStatus.NotLoading &&
currentStatus !== LoadStatus.Error) {
return;
}
this._resetStatus();

await this._platform.logger.run("login", async log => {
this._status.set(LoadStatus.Login);
try {
const request = this._platform.request;
const hsApi = new HomeServerApi({homeserver: homeserver, request: request});
const loginData = await hsApi.guestLogin().response();
await this.startWithAuthData({
accessToken: loginData.access_token,
deviceId: loginData.device_id,
userId: loginData.user_id,
homeserver: homeserver
});
} catch (err) {
this._error = err;
this._status.set(LoadStatus.Error);
}
});
}

/** Method to start client after registration or with given access token.
* To start the client after registering, use `startWithAuthData(registration.authData)`.
* `homeserver` won't be resolved or normalized using this method,
Expand Down
9 changes: 9 additions & 0 deletions src/matrix/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ export class Session {
return this._callHandler;
}

async isGuest() {
if (typeof this._guestUser !== 'undefined') {
return this._guestUser;
}
const whoami = await this._hsApi.whoami().response();
this._guestUser = whoami.is_guest;
return Boolean(whoami.is_guest);
}

_setupCallHandler() {
this._callHandler = new CallHandler({
clock: this._platform.clock,
Expand Down
3 changes: 2 additions & 1 deletion src/matrix/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ export class Sync {

async _syncRequest(syncToken, timeout, log) {
let {syncFilterId} = this._session;
if (typeof syncFilterId !== "string") {
let isGuest = await this._session.isGuest();
if (!isGuest && typeof syncFilterId !== "string") {
this._currentRequest = this._hsApi.createFilter(this._session.user.id, {room: {state: {lazy_load_members: true}}}, {log});
syncFilterId = (await this._currentRequest.response()).filter_id;
}
Expand Down
10 changes: 10 additions & 0 deletions src/matrix/net/HomeServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ export class HomeServerApi {
}, options);
}

guestLogin(initialDeviceDisplayName?: string, options?: BaseRequestOptions): IHomeServerRequest {
return this._unauthedRequest("POST", this._url("/register", CS_V3_PREFIX), { kind: "guest" }, {
initial_device_displayname: initialDeviceDisplayName ? initialDeviceDisplayName : 'Guest account on ' + this._homeserver,
});
}

whoami(): IHomeServerRequest {
return this._get( "/account/whoami", undefined, undefined, { prefix: CS_V3_PREFIX } );
}

createFilter(userId: string, filter: Record<string, any>, options?: BaseRequestOptions): IHomeServerRequest {
return this._post(`/user/${encodeURIComponent(userId)}/filter`, {}, filter, options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/web/ui/css/themes/element/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ button.link {
display: inline-block;
margin: 0;
}
.WorldReadableRoomComposerView .joinRoomButton {
.WorldReadableRoomComposerView .joinRoomButton, .WorldReadableRoomComposerView .loginButton{
float: right;
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform/web/ui/session/room/UnknownRoomView.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export class UnknownRoomView extends TemplateView {
t.br(),
vm.i18n`Want to join it?`
]),
t.button({
t.if(vm => vm.joinAllowed, t => t.button({
className: "button-action primary",
onClick: () => vm.join(),
disabled: vm => vm.busy,
}, vm.i18n`Join room`),
}, vm.i18n`Join room`)),
t.br(),
t.if(vm => vm.checkingPreviewCapability, t => t.div({className: "checkingPreviewCapability"}, [
spinner(t),
Expand Down
8 changes: 6 additions & 2 deletions src/platform/web/ui/session/room/WorldReadableRoomView.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ export class WorldReadableRoomView extends TemplateView {
}),
t.div({className: "WorldReadableRoomComposerView"}, [
t.h3(vm => vm.i18n`Join the room to participate`),
t.button({
t.if(vm => vm.joinAllowed, t => t.button({
className: "joinRoomButton",
onClick: () => vm.join(),
disabled: vm => vm.busy,
}, vm.i18n`Join Room`)
}, vm.i18n`Join Room`)),
t.if(vm => !vm.joinAllowed, t => t.button({
className: "loginButton",
onClick: () => vm.login(),
}, vm.i18n`Log In`))
])
])
]);
Expand Down