From bcd1cbc1b100ec5d7b6d64cf692bcd0a5317cfb3 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 7 Dec 2020 14:05:57 +0100 Subject: [PATCH] allow specifying client in url --- src/Link.js | 24 +++++++++++++++--------- src/open/OpenLinkView.js | 6 +----- src/open/OpenLinkViewModel.js | 17 ++++------------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/Link.js b/src/Link.js index 7d7b148..44d769e 100644 --- a/src/Link.js +++ b/src/Link.js @@ -70,14 +70,19 @@ export class Link { if (!fragment) { return null; } - let [linkStr, queryParams] = fragment.split("?"); + let [linkStr, queryParamsStr] = fragment.split("?"); let viaServers = []; - if (queryParams) { - viaServers = queryParams.split("&") - .map(pair => pair.split("=")) + let clientId = null; + if (queryParamsStr) { + const queryParams = queryParamsStr.split("&").map(pair => pair.split("=")); + viaServers = queryParams .filter(([key, value]) => key === "via") .map(([,value]) => value); + const clientParam = queryParams.find(([key]) => key === "client"); + if (clientParam) { + clientId = clientParam[1]; + } } if (linkStr.startsWith("#/")) { @@ -91,36 +96,37 @@ export class Link { if (matches) { const server = matches[2]; const localPart = matches[1]; - return new Link(viaServers, IdentifierKind.UserId, localPart, server); + return new Link(clientId, viaServers, IdentifierKind.UserId, localPart, server); } matches = ROOMALIAS_PATTERN.exec(identifier); if (matches) { const server = matches[2]; const localPart = matches[1]; - return new Link(viaServers, IdentifierKind.RoomAlias, localPart, server, eventId); + return new Link(clientId, viaServers, IdentifierKind.RoomAlias, localPart, server, eventId); } matches = ROOMID_PATTERN.exec(identifier); if (matches) { const server = matches[2]; const localPart = matches[1]; - return new Link(viaServers, IdentifierKind.RoomId, localPart, server, eventId); + return new Link(clientId, viaServers, IdentifierKind.RoomId, localPart, server, eventId); } matches = GROUPID_PATTERN.exec(identifier); if (matches) { const server = matches[2]; const localPart = matches[1]; - return new Link(viaServers, IdentifierKind.GroupId, localPart, server); + return new Link(clientId, viaServers, IdentifierKind.GroupId, localPart, server); } return null; } - constructor(viaServers, identifierKind, localPart, server, eventId) { + constructor(clientId, viaServers, identifierKind, localPart, server, eventId) { const servers = [server]; servers.push(...viaServers); this.servers = orderedUnique(servers); this.identifierKind = identifierKind; this.identifier = `${asPrefix(identifierKind)}${localPart}:${server}`; this.eventId = eventId; + this.clientId = clientId; } get kind() { diff --git a/src/open/OpenLinkView.js b/src/open/OpenLinkView.js index 5c82f57..badcd29 100644 --- a/src/open/OpenLinkView.js +++ b/src/open/OpenLinkView.js @@ -34,11 +34,7 @@ class ShowLinkView extends TemplateView { render(t, vm) { return t.div([ t.view(new PreviewView(vm.previewViewModel)), - t.p({className: {accept: true, hidden: vm => vm.clientsViewModel}}, t.button({ - className: "primary fullwidth", - onClick: () => vm.showClients() - }, vm => vm.showClientsLabel)), - t.mapView(vm => vm.clientsViewModel, childVM => childVM ? new ClientListView(childVM) : null), + t.view(new ClientListView(vm.clientsViewModel)), t.p({className: {previewSource: true, hidden: vm => !vm.previewDomain}}, [ vm => vm.previewFailed ? `${vm.previewDomain} has not returned a preview.` : `Preview provided by ${vm.previewDomain}.`, " ", diff --git a/src/open/OpenLinkViewModel.js b/src/open/OpenLinkViewModel.js index f49891c..bd72ff8 100644 --- a/src/open/OpenLinkViewModel.js +++ b/src/open/OpenLinkViewModel.js @@ -49,12 +49,13 @@ export class OpenLinkViewModel extends ViewModel { } async _showLink() { - const preferredClient = this.preferences.clientId ? this._clients.find(c => c.id === this.preferences.clientId) : null; - this.clientsViewModel = preferredClient ? new ClientListViewModel(this.childOptions({ + const clientId = this.preferences.clientId || this._link.clientId; + const preferredClient = clientId ? this._clients.find(c => c.id === clientId) : null; + this.clientsViewModel = new ClientListViewModel(this.childOptions({ clients: this._clients, link: this._link, client: preferredClient, - })) : null; + })); this.previewViewModel = new PreviewViewModel(this.childOptions({ link: this._link, consentedServers: this.preferences.homeservers @@ -84,14 +85,4 @@ export class OpenLinkViewModel extends ViewModel { this._showServerConsent(); this.emitChange(); } - - showClients() { - if (!this.clientsViewModel) { - this.clientsViewModel = new ClientListViewModel(this.childOptions({ - clients: this._clients, - link: this._link - })); - this.emitChange(); - } - } }