Merge pull request #3302 from matrix-org/travis/integs/widgets

Read integration managers from account data (widgets)
pull/21833/head
Travis Ralston 2019-08-13 13:02:00 -06:00 committed by GitHub
commit a5cac02910
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 2 deletions

View File

@ -35,6 +35,7 @@ import { sendLoginRequest } from "./Login";
import * as StorageManager from './utils/StorageManager'; import * as StorageManager from './utils/StorageManager';
import SettingsStore from "./settings/SettingsStore"; import SettingsStore from "./settings/SettingsStore";
import TypingStore from "./stores/TypingStore"; import TypingStore from "./stores/TypingStore";
import {IntegrationManagers} from "./integrations/IntegrationManagers";
/** /**
* Called at startup, to attempt to build a logged-in Matrix session. It tries * Called at startup, to attempt to build a logged-in Matrix session. It tries
@ -580,6 +581,7 @@ async function startMatrixClient(startSyncing=true) {
Presence.start(); Presence.start();
} }
DMRoomMap.makeShared().start(); DMRoomMap.makeShared().start();
IntegrationManagers.sharedInstance().startWatching();
ActiveWidgetStore.start(); ActiveWidgetStore.start();
if (startSyncing) { if (startSyncing) {
@ -638,6 +640,7 @@ export function stopMatrixClient(unsetClient=true) {
TypingStore.sharedInstance().reset(); TypingStore.sharedInstance().reset();
Presence.stop(); Presence.stop();
ActiveWidgetStore.stop(); ActiveWidgetStore.stop();
IntegrationManagers.sharedInstance().stopWatching();
if (DMRoomMap.shared()) DMRoomMap.shared().stop(); if (DMRoomMap.shared()) DMRoomMap.shared().stop();
const cli = MatrixClientPeg.get(); const cli = MatrixClientPeg.get();
if (cli) { if (cli) {

View File

@ -18,6 +18,9 @@ import SdkConfig from '../SdkConfig';
import sdk from "../index"; import sdk from "../index";
import Modal from '../Modal'; import Modal from '../Modal';
import {IntegrationManagerInstance} from "./IntegrationManagerInstance"; import {IntegrationManagerInstance} from "./IntegrationManagerInstance";
import type {MatrixClient, MatrixEvent} from "matrix-js-sdk";
import WidgetUtils from "../utils/WidgetUtils";
import MatrixClientPeg from "../MatrixClientPeg";
export class IntegrationManagers { export class IntegrationManagers {
static _instance; static _instance;
@ -30,9 +33,28 @@ export class IntegrationManagers {
} }
_managers: IntegrationManagerInstance[] = []; _managers: IntegrationManagerInstance[] = [];
_client: MatrixClient;
constructor() { constructor() {
this._compileManagers();
}
startWatching(): void {
this.stopWatching();
this._client = MatrixClientPeg.get();
this._client.on("accountData", this._onAccountData.bind(this));
this._compileManagers();
}
stopWatching(): void {
if (!this._client) return;
this._client.removeListener("accountData", this._onAccountData.bind(this));
}
_compileManagers() {
this._managers = [];
this._setupConfiguredManager(); this._setupConfiguredManager();
this._setupAccountManagers();
} }
_setupConfiguredManager() { _setupConfiguredManager() {
@ -44,14 +66,34 @@ export class IntegrationManagers {
} }
} }
_setupAccountManagers() {
if (!this._client || !this._client.getUserId()) return; // not logged in
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;
this._managers.push(new IntegrationManagerInstance(apiUrl, uiUrl));
});
}
_onAccountData(ev: MatrixEvent): void {
if (ev.getType() === 'm.widgets') {
this._compileManagers();
}
}
hasManager(): boolean { hasManager(): boolean {
return this._managers.length > 0; return this._managers.length > 0;
} }
getPrimaryManager(): IntegrationManagerInstance { getPrimaryManager(): IntegrationManagerInstance {
if (this.hasManager()) { if (this.hasManager()) {
// TODO: TravisR - Handle custom integration managers (widgets) return this._managers[this._managers.length - 1];
return this._managers[0];
} else { } else {
return null; return null;
} }
@ -66,3 +108,6 @@ export class IntegrationManagers {
); );
} }
} }
// For debugging
global.mxIntegrationManagers = IntegrationManagers;

View File

@ -340,6 +340,17 @@ export default class WidgetUtils {
return widgets.filter((widget) => widget.content && widget.content.type === "m.stickerpicker"); return widgets.filter((widget) => widget.content && widget.content.type === "m.stickerpicker");
} }
/**
* Get all integration manager widgets for this user.
* @returns {Object[]} An array of integration manager user widgets.
*/
static getIntegrationManagerWidgets() {
const widgets = WidgetUtils.getUserWidgetsArray();
// We'll be using im.vector.integration_manager until MSC1957 or similar is accepted.
const imTypes = ["m.integration_manager", "im.vector.integration_manager"];
return widgets.filter(w => w.content && imTypes.includes(w.content.type));
}
/** /**
* Remove all stickerpicker widgets (stickerpickers are user widgets by nature) * Remove all stickerpicker widgets (stickerpickers are user widgets by nature)
* @return {Promise} Resolves on account data updated * @return {Promise} Resolves on account data updated