Skip to content

Commit 50a25dd

Browse files
committed
Add preference for custom web instances, use it for Element
1 parent 22ad0e9 commit 50a25dd

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

src/Preferences.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Preferences extends EventEmitter {
2525
// used to differentiate web from native if a client supports both
2626
this.platform = null;
2727
this.homeservers = null;
28+
this.preferredWebInstances = {};
2829

2930
const prefsStr = localStorage.getItem("preferred_client");
3031
if (prefsStr) {
@@ -36,6 +37,10 @@ export class Preferences extends EventEmitter {
3637
if (serversStr) {
3738
this.homeservers = JSON.parse(serversStr);
3839
}
40+
const preferredWebInstancesStr = localStorage.getItem("preferred_web_instances");
41+
if (preferredWebInstancesStr) {
42+
this.preferredWebInstances = JSON.parse(preferredWebInstancesStr);
43+
}
3944
}
4045

4146
setClient(id, platform) {
@@ -54,12 +59,24 @@ export class Preferences extends EventEmitter {
5459
}
5560
}
5661

62+
setPreferredWebInstance(client_id, instance_url) {
63+
this.preferredWebInstances[client_id] = instance_url;
64+
this._localStorage.setItem("preferred_web_instances", JSON.stringify(this.preferredWebInstances));
65+
this.emit("canClear");
66+
}
67+
68+
getPreferredWebInstance(client_id) {
69+
return this.preferredWebInstances[client_id];
70+
}
71+
5772
clear() {
5873
this._localStorage.removeItem("preferred_client");
5974
this._localStorage.removeItem("consented_servers");
75+
this._localStorage.removeItem("preferred_web_instances");
6076
this.clientId = null;
6177
this.platform = null;
6278
this.homeservers = null;
79+
this.preferredWebInstances = {};
6380
}
6481

6582
get canClear() {

src/open/ClientViewModel.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export class ClientViewModel extends ViewModel {
5959
if (this._proposedPlatform === this._nativePlatform) {
6060
deepLinkLabel = "Open in app";
6161
} else {
62-
deepLinkLabel = `Open on ${this._client.getPreferredWebInstance(this._link)}`;
62+
deepLinkLabel = `Open on ${this.preferredWebInstance}`;
6363
}
6464
}
6565
const actions = [];
66-
const proposedDeepLink = this._client.getDeepLink(this._proposedPlatform, this._link);
66+
const proposedDeepLink = this._client.getDeepLink(this._proposedPlatform, this._link, this.preferredWebInstance);
6767
if (proposedDeepLink) {
6868
actions.push({
6969
label: deepLinkLabel,
@@ -83,8 +83,8 @@ export class ClientViewModel extends ViewModel {
8383
// show only if there is a preferred instance, and if we don't already link to it in the first button
8484
if (hasPreferredWebInstance && this._webPlatform && this._proposedPlatform !== this._webPlatform) {
8585
actions.push({
86-
label: `Open on ${this._client.getPreferredWebInstance(this._link)}`,
87-
url: this._client.getDeepLink(this._webPlatform, this._link),
86+
label: `Open on ${this.preferredWebInstance}`,
87+
url: this._client.getDeepLink(this._webPlatform, this._link, this.preferredWebInstance),
8888
kind: "open-in-web",
8989
activated: () => {} // don't persist this choice as we don't persist the preferred web instance, it's in the url
9090
});
@@ -108,10 +108,10 @@ export class ClientViewModel extends ViewModel {
108108
actions.push(...nativeActions);
109109
}
110110
if (this._webPlatform) {
111-
const webDeepLink = this._client.getDeepLink(this._webPlatform, this._link);
111+
const webDeepLink = this._client.getDeepLink(this._webPlatform, this._link, this.preferredWebInstance);
112112
if (webDeepLink) {
113113
const webLabel = this.hasPreferredWebInstance ?
114-
`Open on ${this._client.getPreferredWebInstance(this._link)}` :
114+
`Open on ${this.preferredWebInstance}` :
115115
`Continue in your browser`;
116116
actions.push({
117117
label: webLabel,
@@ -128,14 +128,22 @@ export class ClientViewModel extends ViewModel {
128128
return actions;
129129
}
130130

131-
get hasPreferredWebInstance() {
131+
get preferredWebInstance() {
132132
// also check there is a web platform that matches the platforms the user is on (mobile or desktop web)
133-
return this._webPlatform && typeof this._client.getPreferredWebInstance(this._link) === "string";
133+
if (!this._webPlatform) return undefined;
134+
return (
135+
this.preferences.getPreferredWebInstance(this._client.id)
136+
|| this._client.getPreferredWebInstance(this._link)
137+
);
138+
}
139+
140+
get hasPreferredWebInstance() {
141+
return typeof this.preferredWebInstance === "string";
134142
}
135143

136144
get hostedByBannerLabel() {
137-
const preferredWebInstance = this._client.getPreferredWebInstance(this._link);
138-
if (this._webPlatform && preferredWebInstance) {
145+
if (this.hasPreferredWebInstance) {
146+
const preferredWebInstance = this.preferredWebInstance;
139147
let label = preferredWebInstance;
140148
const subDomainIdx = preferredWebInstance.lastIndexOf(".", preferredWebInstance.lastIndexOf(".") - 1);
141149
if (subDomainIdx !== -1) {
@@ -188,7 +196,7 @@ export class ClientViewModel extends ViewModel {
188196

189197
get showDeepLinkInInstall() {
190198
// we can assume this._nativePlatform as this._clientCanIntercept already checks it
191-
return this._clientCanIntercept && !!this._client.getDeepLink(this._nativePlatform, this._link);
199+
return this._clientCanIntercept && !!this._client.getDeepLink(this._nativePlatform, this._link, this.preferredWebInstance);
192200
}
193201

194202
get availableOnPlatformNames() {

src/open/clients/Element.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ export class Element {
5555
get homepage() { return "https://element.io"; }
5656
get author() { return "Element"; }
5757
getMaturity(platform) { return Maturity.Stable; }
58+
get supportsCustomInstances() { return true; }
5859

59-
getDeepLink(platform, link) {
60+
getDeepLink(platform, link, preferredWebInstance) {
6061
let fragmentPath;
6162
switch (link.kind) {
6263
case LinkKind.User:
@@ -82,8 +83,8 @@ export class Element {
8283
let instanceHost = trustedWebInstances[0];
8384
// we use app.element.io which iOS will intercept, but it likely won't intercept any other trusted instances
8485
// so only use a preferred web instance for true web links.
85-
if (isWebPlatform && trustedWebInstances.includes(link.webInstances[this.id])) {
86-
instanceHost = link.webInstances[this.id];
86+
if (isWebPlatform && preferredWebInstance) {
87+
instanceHost = preferredWebInstance;
8788
}
8889
return `https://${instanceHost}/#/${fragmentPath}`;
8990
} else if (platform === Platform.Linux || platform === Platform.Windows || platform === Platform.macOS) {

0 commit comments

Comments
 (0)