2019-08-09 22:05:05 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import SdkConfig from '../SdkConfig';
|
|
|
|
import sdk from "../index";
|
|
|
|
import Modal from '../Modal';
|
2019-08-22 20:49:20 +00:00
|
|
|
import {IntegrationManagerInstance, KIND_ACCOUNT, KIND_CONFIG, KIND_HOMESERVER} from "./IntegrationManagerInstance";
|
2019-08-09 23:35:59 +00:00
|
|
|
import type {MatrixClient, MatrixEvent} from "matrix-js-sdk";
|
|
|
|
import WidgetUtils from "../utils/WidgetUtils";
|
|
|
|
import MatrixClientPeg from "../MatrixClientPeg";
|
2019-08-22 20:49:20 +00:00
|
|
|
import {AutoDiscovery} from "../../../matrix-js-sdk";
|
|
|
|
|
|
|
|
const HS_MANAGERS_REFRESH_INTERVAL = 8 * 60 * 60 * 1000; // 8 hours
|
|
|
|
const KIND_PREFERENCE = [
|
|
|
|
// Ordered: first is most preferred, last is least preferred.
|
|
|
|
KIND_ACCOUNT,
|
|
|
|
KIND_HOMESERVER,
|
|
|
|
KIND_CONFIG,
|
|
|
|
];
|
2019-08-09 22:05:05 +00:00
|
|
|
|
|
|
|
export class IntegrationManagers {
|
|
|
|
static _instance;
|
|
|
|
|
|
|
|
static sharedInstance(): IntegrationManagers {
|
|
|
|
if (!IntegrationManagers._instance) {
|
|
|
|
IntegrationManagers._instance = new IntegrationManagers();
|
|
|
|
}
|
|
|
|
return IntegrationManagers._instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
_managers: IntegrationManagerInstance[] = [];
|
2019-08-09 23:35:59 +00:00
|
|
|
_client: MatrixClient;
|
2019-08-22 20:49:20 +00:00
|
|
|
_wellknownRefreshTimerId: number = null;
|
|
|
|
_primaryManager: IntegrationManagerInstance;
|
2019-08-09 22:05:05 +00:00
|
|
|
|
|
|
|
constructor() {
|
2019-08-09 23:35:59 +00:00
|
|
|
this._compileManagers();
|
|
|
|
}
|
|
|
|
|
|
|
|
startWatching(): void {
|
|
|
|
this.stopWatching();
|
|
|
|
this._client = MatrixClientPeg.get();
|
|
|
|
this._client.on("accountData", this._onAccountData.bind(this));
|
|
|
|
this._compileManagers();
|
2019-08-22 20:49:20 +00:00
|
|
|
setInterval(() => this._setupHomeserverManagers(), HS_MANAGERS_REFRESH_INTERVAL);
|
2019-08-09 23:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stopWatching(): void {
|
|
|
|
if (!this._client) return;
|
|
|
|
this._client.removeListener("accountData", this._onAccountData.bind(this));
|
2019-08-22 20:49:20 +00:00
|
|
|
if (this._wellknownRefreshTimerId !== null) clearInterval(this._wellknownRefreshTimerId);
|
2019-08-09 23:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_compileManagers() {
|
|
|
|
this._managers = [];
|
2019-08-09 22:05:05 +00:00
|
|
|
this._setupConfiguredManager();
|
2019-08-22 20:49:20 +00:00
|
|
|
this._setupHomeserverManagers();
|
2019-08-09 23:35:59 +00:00
|
|
|
this._setupAccountManagers();
|
2019-08-09 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_setupConfiguredManager() {
|
|
|
|
const apiUrl = SdkConfig.get()['integrations_rest_url'];
|
|
|
|
const uiUrl = SdkConfig.get()['integrations_ui_url'];
|
|
|
|
|
|
|
|
if (apiUrl && uiUrl) {
|
2019-08-12 21:35:39 +00:00
|
|
|
this._managers.push(new IntegrationManagerInstance(KIND_CONFIG, apiUrl, uiUrl));
|
2019-08-22 20:49:20 +00:00
|
|
|
this._primaryManager = null; // reset primary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _setupHomeserverManagers() {
|
|
|
|
try {
|
|
|
|
console.log("Updating homeserver-configured integration managers...");
|
|
|
|
const homeserverDomain = MatrixClientPeg.getHomeserverName();
|
|
|
|
const discoveryResponse = await AutoDiscovery.getRawClientConfig(homeserverDomain);
|
|
|
|
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
|
|
|
|
this._managers = this._managers.filter(m => m.kind !== KIND_HOMESERVER);
|
|
|
|
|
|
|
|
// Now add all the managers the homeserver wants us to have
|
|
|
|
for (const hsManager of managers) {
|
|
|
|
if (!hsManager["api_url"]) continue;
|
|
|
|
this._managers.push(new IntegrationManagerInstance(
|
|
|
|
KIND_HOMESERVER,
|
|
|
|
hsManager["api_url"],
|
|
|
|
hsManager["ui_url"], // optional
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._primaryManager = null; // reset primary
|
|
|
|
} else {
|
|
|
|
console.log("Homeserver has no integration managers");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
// Errors during discovery are non-fatal
|
2019-08-09 22:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 23:35:59 +00:00
|
|
|
_setupAccountManagers() {
|
2019-08-10 20:59:13 +00:00
|
|
|
if (!this._client || !this._client.getUserId()) return; // not logged in
|
2019-08-09 23:35:59 +00:00
|
|
|
const widgets = WidgetUtils.getIntegrationManagerWidgets();
|
|
|
|
widgets.forEach(w => {
|
|
|
|
const data = w.content['data'];
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
const uiUrl = w.content['url'];
|
|
|
|
const apiUrl = data['api_url'];
|
|
|
|
if (!apiUrl || !uiUrl) return;
|
|
|
|
|
2019-08-22 20:49:20 +00:00
|
|
|
const manager = new IntegrationManagerInstance(KIND_ACCOUNT, apiUrl, uiUrl);
|
|
|
|
manager.id = w['id'] || w['state_key'] || '';
|
|
|
|
this._managers.push(manager);
|
2019-08-09 23:35:59 +00:00
|
|
|
});
|
2019-08-22 20:49:20 +00:00
|
|
|
this._primaryManager = null; // reset primary
|
2019-08-09 23:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onAccountData(ev: MatrixEvent): void {
|
|
|
|
if (ev.getType() === 'm.widgets') {
|
|
|
|
this._compileManagers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 22:05:05 +00:00
|
|
|
hasManager(): boolean {
|
|
|
|
return this._managers.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrimaryManager(): IntegrationManagerInstance {
|
|
|
|
if (this.hasManager()) {
|
2019-08-22 20:49:20 +00:00
|
|
|
if (this._primaryManager) return this._primaryManager;
|
|
|
|
|
|
|
|
for (const kind of KIND_PREFERENCE) {
|
|
|
|
const managers = this._managers.filter(m => m.kind === kind);
|
|
|
|
if (!managers || !managers.length) continue;
|
|
|
|
|
|
|
|
if (kind === KIND_ACCOUNT) {
|
|
|
|
// Order by state_keys (IDs)
|
|
|
|
managers.sort((a, b) => a.id.localeCompare(b.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
this._primaryManager = managers[0];
|
|
|
|
return this._primaryManager;
|
|
|
|
}
|
2019-08-09 22:05:05 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openNoManagerDialog(): void {
|
|
|
|
// TODO: Is it Integrations (plural) or Integration (singular). Singular is easier spoken.
|
|
|
|
const IntegrationsManager = sdk.getComponent("views.settings.IntegrationsManager");
|
|
|
|
Modal.createTrackedDialog(
|
|
|
|
"Integration Manager", "None", IntegrationsManager,
|
|
|
|
{configured: false}, 'mx_IntegrationsManager',
|
|
|
|
);
|
|
|
|
}
|
2019-08-12 21:35:39 +00: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 14:57:38 +00:00
|
|
|
await WidgetUtils.addIntegrationManagerWidget(manager.name, manager.uiUrl, manager.apiUrl);
|
2019-08-12 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-15 19:28:23 +00: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-12 21:35:39 +00: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.
|
|
|
|
*/
|
|
|
|
async tryDiscoverManager(domainName: string): IntegrationManagerInstance {
|
|
|
|
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
|
|
|
|
const url = url.parse(domainName);
|
|
|
|
domainName = url.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
let wkConfig;
|
|
|
|
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
|
|
|
|
const manager = new IntegrationManagerInstance(KIND_ACCOUNT, widget["data"]["api_url"], widget["url"]);
|
2019-08-15 19:28:23 +00:00
|
|
|
console.log("Got an integration manager (untested)");
|
2019-08-12 21:35:39 +00:00
|
|
|
|
2019-08-15 19:28:23 +00: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-12 21:35:39 +00:00
|
|
|
|
|
|
|
return manager;
|
|
|
|
}
|
2019-08-09 22:05:05 +00:00
|
|
|
}
|
2019-08-09 23:35:59 +00:00
|
|
|
|
|
|
|
// For debugging
|
|
|
|
global.mxIntegrationManagers = IntegrationManagers;
|