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 {MatrixClient} from "matrix-js-sdk/src/client";
|
|
|
|
import type {MatrixEvent} from "matrix-js-sdk/src/models/event";
|
|
|
|
import type {Room} from "matrix-js-sdk/src/models/room";
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
import SdkConfig from '../SdkConfig';
|
|
|
|
import Modal from '../Modal';
|
2020-07-17 15:38:08 +03:00
|
|
|
import {IntegrationManagerInstance, Kind} from "./IntegrationManagerInstance";
|
|
|
|
import IntegrationsImpossibleDialog from "../components/views/dialogs/IntegrationsImpossibleDialog";
|
|
|
|
import TabbedIntegrationManagerDialog from "../components/views/dialogs/TabbedIntegrationManagerDialog";
|
|
|
|
import IntegrationsDisabledDialog from "../components/views/dialogs/IntegrationsDisabledDialog";
|
2019-08-10 02:35:59 +03:00
|
|
|
import WidgetUtils from "../utils/WidgetUtils";
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "../MatrixClientPeg";
|
2019-11-21 06:40:39 +03:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2020-07-17 15:38:08 +03:00
|
|
|
import url from 'url';
|
2019-08-22 23:49:20 +03:00
|
|
|
|
|
|
|
const KIND_PREFERENCE = [
|
|
|
|
// Ordered: first is most preferred, last is least preferred.
|
2020-07-17 15:38:08 +03:00
|
|
|
Kind.Account,
|
|
|
|
Kind.Homeserver,
|
|
|
|
Kind.Config,
|
2019-08-22 23:49:20 +03:00
|
|
|
];
|
2019-08-10 01:05:05 +03:00
|
|
|
|
|
|
|
export class IntegrationManagers {
|
2020-07-17 15:38:08 +03:00
|
|
|
private static instance;
|
|
|
|
|
|
|
|
private managers: IntegrationManagerInstance[] = [];
|
|
|
|
private client: MatrixClient;
|
|
|
|
private primaryManager: IntegrationManagerInstance;
|
2019-08-10 01:05:05 +03:00
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
public static sharedInstance(): IntegrationManagers {
|
|
|
|
if (!IntegrationManagers.instance) {
|
|
|
|
IntegrationManagers.instance = new IntegrationManagers();
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
2020-07-17 15:38:08 +03:00
|
|
|
return IntegrationManagers.instance;
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
2020-07-17 15:38:08 +03:00
|
|
|
this.compileManagers();
|
2019-08-10 02:35:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
startWatching(): void {
|
|
|
|
this.stopWatching();
|
2020-07-17 15:38:08 +03:00
|
|
|
this.client = MatrixClientPeg.get();
|
|
|
|
this.client.on("accountData", this.onAccountData);
|
|
|
|
this.client.on("WellKnown.client", this.setupHomeserverManagers);
|
|
|
|
this.compileManagers();
|
2019-08-10 02:35:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stopWatching(): void {
|
2020-07-17 15:38:08 +03:00
|
|
|
if (!this.client) return;
|
|
|
|
this.client.removeListener("accountData", this.onAccountData);
|
|
|
|
this.client.removeListener("WellKnown.client", this.setupHomeserverManagers);
|
2019-08-10 02:35:59 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
private compileManagers() {
|
|
|
|
this.managers = [];
|
|
|
|
this.setupConfiguredManager();
|
|
|
|
this.setupAccountManagers();
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
private setupConfiguredManager() {
|
|
|
|
const apiUrl: string = SdkConfig.get()['integrations_rest_url'];
|
|
|
|
const uiUrl: string = SdkConfig.get()['integrations_ui_url'];
|
2019-08-10 01:05:05 +03:00
|
|
|
|
|
|
|
if (apiUrl && uiUrl) {
|
2020-07-17 15:38:08 +03:00
|
|
|
this.managers.push(new IntegrationManagerInstance(Kind.Config, apiUrl, uiUrl));
|
|
|
|
this.primaryManager = null; // reset primary
|
2019-08-22 23:49:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
private setupHomeserverManagers = async (discoveryResponse) => {
|
2020-06-02 00:22:01 +03:00
|
|
|
console.log("Updating homeserver-configured integration managers...");
|
|
|
|
if (discoveryResponse && discoveryResponse['m.integrations']) {
|
|
|
|
let managers = discoveryResponse['m.integrations']['managers'];
|
|
|
|
if (!Array.isArray(managers)) managers = []; // make it an array so we can wipe the HS managers
|
|
|
|
|
|
|
|
console.log(`Homeserver has ${managers.length} integration managers`);
|
|
|
|
|
|
|
|
// Clear out any known managers for the homeserver
|
|
|
|
// TODO: Log out of the scalar clients
|
2020-07-17 15:38:08 +03:00
|
|
|
this.managers = this.managers.filter(m => m.kind !== Kind.Homeserver);
|
2020-06-02 00:22:01 +03:00
|
|
|
|
|
|
|
// Now add all the managers the homeserver wants us to have
|
|
|
|
for (const hsManager of managers) {
|
|
|
|
if (!hsManager["api_url"]) continue;
|
2020-07-17 15:38:08 +03:00
|
|
|
this.managers.push(new IntegrationManagerInstance(
|
|
|
|
Kind.Homeserver,
|
2020-06-02 00:22:01 +03:00
|
|
|
hsManager["api_url"],
|
|
|
|
hsManager["ui_url"], // optional
|
|
|
|
));
|
2019-08-22 23:49:20 +03:00
|
|
|
}
|
2020-06-02 00:22:01 +03:00
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
this.primaryManager = null; // reset primary
|
2020-06-02 00:22:01 +03:00
|
|
|
} else {
|
|
|
|
console.log("Homeserver has no integration managers");
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
2020-07-17 15:25:28 +03:00
|
|
|
};
|
2019-08-10 01:05:05 +03:00
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
private setupAccountManagers() {
|
|
|
|
if (!this.client || !this.client.getUserId()) return; // not logged in
|
2019-08-10 02:35:59 +03:00
|
|
|
const widgets = WidgetUtils.getIntegrationManagerWidgets();
|
|
|
|
widgets.forEach(w => {
|
|
|
|
const data = w.content['data'];
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
const uiUrl = w.content['url'];
|
2020-10-19 23:13:31 +03:00
|
|
|
const apiUrl = data['api_url'] as string;
|
2019-08-10 02:35:59 +03:00
|
|
|
if (!apiUrl || !uiUrl) return;
|
|
|
|
|
2020-07-20 22:43:49 +03:00
|
|
|
const manager = new IntegrationManagerInstance(
|
|
|
|
Kind.Account,
|
|
|
|
apiUrl,
|
|
|
|
uiUrl,
|
|
|
|
w['id'] || w['state_key'] || '',
|
|
|
|
);
|
2020-07-17 15:38:08 +03:00
|
|
|
this.managers.push(manager);
|
2019-08-10 02:35:59 +03:00
|
|
|
});
|
2020-07-17 15:38:08 +03:00
|
|
|
this.primaryManager = null; // reset primary
|
2019-08-10 02:35:59 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
private onAccountData = (ev: MatrixEvent): void => {
|
2019-08-10 02:35:59 +03:00
|
|
|
if (ev.getType() === 'm.widgets') {
|
2020-07-17 15:38:08 +03:00
|
|
|
this.compileManagers();
|
2019-08-10 02:35:59 +03:00
|
|
|
}
|
2020-02-20 05:35:30 +03:00
|
|
|
};
|
2019-08-10 02:35:59 +03:00
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
hasManager(): boolean {
|
2020-07-17 15:38:08 +03:00
|
|
|
return this.managers.length > 0;
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 00:17:59 +03:00
|
|
|
getOrderedManagers(): IntegrationManagerInstance[] {
|
|
|
|
const ordered = [];
|
|
|
|
for (const kind of KIND_PREFERENCE) {
|
2020-07-17 15:38:08 +03:00
|
|
|
const managers = this.managers.filter(m => m.kind === kind);
|
2019-08-23 00:17:59 +03:00
|
|
|
if (!managers || !managers.length) continue;
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
if (kind === Kind.Account) {
|
2019-08-23 00:17:59 +03:00
|
|
|
// Order by state_keys (IDs)
|
|
|
|
managers.sort((a, b) => a.id.localeCompare(b.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered.push(...managers);
|
|
|
|
}
|
|
|
|
return ordered;
|
|
|
|
}
|
|
|
|
|
2019-08-10 01:05:05 +03:00
|
|
|
getPrimaryManager(): IntegrationManagerInstance {
|
|
|
|
if (this.hasManager()) {
|
2020-07-17 15:38:08 +03:00
|
|
|
if (this.primaryManager) return this.primaryManager;
|
2019-08-22 23:49:20 +03:00
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
this.primaryManager = this.getOrderedManagers()[0];
|
|
|
|
return this.primaryManager;
|
2019-08-10 01:05:05 +03:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openNoManagerDialog(): void {
|
2019-11-21 06:40:39 +03:00
|
|
|
Modal.createTrackedDialog('Integrations impossible', '', IntegrationsImpossibleDialog);
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
2019-08-13 00:35:39 +03:00
|
|
|
|
2019-08-23 18:12:40 +03:00
|
|
|
openAll(room: Room = null, screen: string = null, integrationId: string = null): void {
|
2019-11-21 06:40:39 +03:00
|
|
|
if (!SettingsStore.getValue("integrationProvisioning")) {
|
|
|
|
return this.showDisabledDialog();
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
if (this.managers.length === 0) {
|
2019-11-21 06:40:39 +03:00
|
|
|
return this.openNoManagerDialog();
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:12:40 +03:00
|
|
|
Modal.createTrackedDialog(
|
|
|
|
'Tabbed Integration Manager', '', TabbedIntegrationManagerDialog,
|
|
|
|
{room, screen, integrationId}, 'mx_TabbedIntegrationManagerDialog',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-21 06:40:39 +03:00
|
|
|
showDisabledDialog(): void {
|
|
|
|
Modal.createTrackedDialog('Integrations disabled', '', IntegrationsDisabledDialog);
|
|
|
|
}
|
|
|
|
|
2019-08-13 00:35:39 +03:00
|
|
|
async overwriteManagerOnAccount(manager: IntegrationManagerInstance) {
|
|
|
|
// TODO: TravisR - We should be logging out of scalar clients.
|
|
|
|
await WidgetUtils.removeIntegrationManagerWidgets();
|
|
|
|
|
|
|
|
// TODO: TravisR - We should actually be carrying over the discovery response verbatim.
|
2019-08-14 17:57:38 +03:00
|
|
|
await WidgetUtils.addIntegrationManagerWidget(manager.name, manager.uiUrl, manager.apiUrl);
|
2019-08-13 00:35:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-15 22:28:23 +03:00
|
|
|
* Attempts to discover an integration manager using only its name. This will not validate that
|
|
|
|
* the integration manager is functional - that is the caller's responsibility.
|
2019-08-13 00:35:39 +03:00
|
|
|
* @param {string} domainName The domain name to look up.
|
|
|
|
* @returns {Promise<IntegrationManagerInstance>} Resolves to an integration manager instance,
|
|
|
|
* or null if none was found.
|
|
|
|
*/
|
2020-07-17 15:38:08 +03:00
|
|
|
async tryDiscoverManager(domainName: string): Promise<IntegrationManagerInstance> {
|
2019-08-13 00:35:39 +03:00
|
|
|
console.log("Looking up integration manager via .well-known");
|
|
|
|
if (domainName.startsWith("http:") || domainName.startsWith("https:")) {
|
|
|
|
// trim off the scheme and just use the domain
|
2020-07-17 15:38:08 +03:00
|
|
|
domainName = url.parse(domainName).host;
|
2019-08-13 00:35:39 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:08 +03:00
|
|
|
let wkConfig: object;
|
2019-08-13 00:35:39 +03:00
|
|
|
try {
|
|
|
|
const result = await fetch(`https://${domainName}/.well-known/matrix/integrations`);
|
|
|
|
wkConfig = await result.json();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
console.warn("Failed to locate integration manager");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wkConfig || !wkConfig["m.integrations_widget"]) {
|
|
|
|
console.warn("Missing integrations widget on .well-known response");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const widget = wkConfig["m.integrations_widget"];
|
|
|
|
if (!widget["url"] || !widget["data"] || !widget["data"]["api_url"]) {
|
|
|
|
console.warn("Malformed .well-known response for integrations widget");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All discovered managers are per-user managers
|
2020-07-17 15:38:08 +03:00
|
|
|
const manager = new IntegrationManagerInstance(Kind.Account, widget["data"]["api_url"], widget["url"]);
|
2019-08-15 22:28:23 +03:00
|
|
|
console.log("Got an integration manager (untested)");
|
2019-08-13 00:35:39 +03:00
|
|
|
|
2019-08-15 22:28:23 +03:00
|
|
|
// We don't test the manager because the caller may need to do extra
|
|
|
|
// checks or similar with it. For instance, they may need to deal with
|
|
|
|
// terms of service or want to call something particular.
|
2019-08-13 00:35:39 +03:00
|
|
|
|
|
|
|
return manager;
|
|
|
|
}
|
2019-08-10 01:05:05 +03:00
|
|
|
}
|
2019-08-10 02:35:59 +03:00
|
|
|
|
|
|
|
// For debugging
|
2020-07-20 22:43:49 +03:00
|
|
|
window.mxIntegrationManagers = IntegrationManagers;
|