2019-08-10 01:05:05 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
import type {Room} from "matrix-js-sdk/src/models/room";
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
import ScalarAuthClient from "../ScalarAuthClient";
|
|
|
|
import {dialogTermsInteractionCallback, TermsNotSignedError} from "../Terms";
|
|
|
|
import Modal from '../Modal';
|
2019-08-13 00:35:39 +03:00
|
|
|
import url from 'url';
|
2019-11-21 06:40:39 +03:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2020-07-17 15:38:08 +03:00
|
|
|
import IntegrationManager from "../components/views/settings/IntegrationManager";
|
2019-11-21 06:40:39 +03:00
|
|
|
import {IntegrationManagers} from "./IntegrationManagers";
|
2019-08-13 00:35:39 +03:00
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
export enum Kind {
|
|
|
|
Account = "account",
|
|
|
|
Config = "config",
|
|
|
|
Homeserver = "homeserver",
|
|
|
|
}
|
2019-08-10 01:05:05 +03:00
|
|
|
|
|
|
|
export class IntegrationManagerInstance {
|
2020-07-17 15:38:08 +03:00
|
|
|
public readonly apiUrl: string;
|
|
|
|
public readonly uiUrl: string;
|
|
|
|
public readonly kind: string;
|
|
|
|
public readonly id: string; // only applicable in some cases
|
2019-08-10 01:05:05 +03:00
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
// Per the spec: UI URL is optional.
|
|
|
|
constructor(kind: string, apiUrl: string, uiUrl: string = apiUrl, id?: string) {
|
2019-08-13 00:35:39 +03:00
|
|
|
this.kind = kind;
|
2019-08-10 01:05:05 +03:00
|
|
|
this.apiUrl = apiUrl;
|
|
|
|
this.uiUrl = uiUrl;
|
2020-07-17 15:38:08 +03:00
|
|
|
this.id = id;
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 00:35:39 +03:00
|
|
|
get name(): string {
|
|
|
|
const parsed = url.parse(this.uiUrl);
|
2019-08-15 22:28:23 +03:00
|
|
|
return parsed.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
get trimmedApiUrl(): string {
|
|
|
|
const parsed = url.parse(this.apiUrl);
|
|
|
|
parsed.pathname = '';
|
|
|
|
parsed.path = '';
|
2020-07-17 15:38:08 +03:00
|
|
|
return url.format(parsed);
|
2019-08-13 00:35:39 +03:00
|
|
|
}
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
getScalarClient(): ScalarAuthClient {
|
|
|
|
return new ScalarAuthClient(this.apiUrl, this.uiUrl);
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
async open(room: Room = null, screen: string = null, integrationId: string = null): Promise<void> {
|
2019-11-21 06:40:39 +03:00
|
|
|
if (!SettingsStore.getValue("integrationProvisioning")) {
|
|
|
|
return IntegrationManagers.sharedInstance().showDisabledDialog();
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
const dialog = Modal.createTrackedDialog(
|
2019-11-21 06:14:20 +03:00
|
|
|
'Integration Manager', '', IntegrationManager,
|
|
|
|
{loading: true}, 'mx_IntegrationManager',
|
2019-08-10 01:05:05 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const client = this.getScalarClient();
|
|
|
|
client.setTermsInteractionCallback((policyInfo, agreedUrls) => {
|
|
|
|
// To avoid visual glitching of two modals stacking briefly, we customise the
|
2019-11-21 06:14:20 +03:00
|
|
|
// terms dialog sizing when it will appear for the integration manager so that
|
2019-08-10 01:05:05 +03:00
|
|
|
// it gets the same basic size as the IM's own modal.
|
|
|
|
return dialogTermsInteractionCallback(
|
2019-11-21 06:14:20 +03:00
|
|
|
policyInfo, agreedUrls, 'mx_TermsDialog_forIntegrationManager',
|
2019-08-10 01:05:05 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-08-10 01:14:36 +03:00
|
|
|
const newProps = {};
|
2019-08-10 01:05:05 +03:00
|
|
|
try {
|
|
|
|
await client.connect();
|
|
|
|
if (!client.hasCredentials()) {
|
|
|
|
newProps["connected"] = false;
|
|
|
|
} else {
|
|
|
|
newProps["url"] = client.getScalarInterfaceUrlForRoom(room, screen, integrationId);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TermsNotSignedError) {
|
|
|
|
dialog.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error(e);
|
2019-08-10 01:14:36 +03:00
|
|
|
newProps["connected"] = false;
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the old dialog and open a new one
|
|
|
|
dialog.close();
|
|
|
|
Modal.createTrackedDialog(
|
2019-11-21 06:14:20 +03:00
|
|
|
'Integration Manager', '', IntegrationManager,
|
|
|
|
newProps, 'mx_IntegrationManager',
|
2019-08-10 01:05:05 +03:00
|
|
|
);
|
|
|
|
}
|
2019-08-10 01:14:36 +03:00
|
|
|
}
|