Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Read integration managers from account data
Browse files Browse the repository at this point in the history
For element-hq/element-web#4913 / element-hq/element-web#10161

Relies on the structure defined by [MSC1957](matrix-org/matrix-spec-proposals#1957)

This is just the bit of code to parse the user's widgets (while watching for changes) and allow for it to be the default manager.
  • Loading branch information
turt2live committed Aug 9, 2019
1 parent 4c82d16 commit 74ce5c3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { sendLoginRequest } from "./Login";
import * as StorageManager from './utils/StorageManager';
import SettingsStore from "./settings/SettingsStore";
import TypingStore from "./stores/TypingStore";
import {IntegrationManagers} from "./integrations/IntegrationManagers";

/**
* Called at startup, to attempt to build a logged-in Matrix session. It tries
Expand Down Expand Up @@ -580,6 +581,7 @@ async function startMatrixClient(startSyncing=true) {
Presence.start();
}
DMRoomMap.makeShared().start();
IntegrationManagers.sharedInstance().startWatching();
ActiveWidgetStore.start();

if (startSyncing) {
Expand Down Expand Up @@ -638,6 +640,7 @@ export function stopMatrixClient(unsetClient=true) {
TypingStore.sharedInstance().reset();
Presence.stop();
ActiveWidgetStore.stop();
IntegrationManagers.sharedInstance().stopWatching();
if (DMRoomMap.shared()) DMRoomMap.shared().stop();
const cli = MatrixClientPeg.get();
if (cli) {
Expand Down
48 changes: 46 additions & 2 deletions src/integrations/IntegrationManagers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import SdkConfig from '../SdkConfig';
import sdk from "../index";
import Modal from '../Modal';
import {IntegrationManagerInstance} from "./IntegrationManagerInstance";
import type {MatrixClient, MatrixEvent} from "matrix-js-sdk";
import WidgetUtils from "../utils/WidgetUtils";
import MatrixClientPeg from "../MatrixClientPeg";

export class IntegrationManagers {
static _instance;
Expand All @@ -30,9 +33,28 @@ export class IntegrationManagers {
}

_managers: IntegrationManagerInstance[] = [];
_client: MatrixClient;

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._setupAccountManagers();
}

_setupConfiguredManager() {
Expand All @@ -44,14 +66,33 @@ export class IntegrationManagers {
}
}

_setupAccountManagers() {
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 {
return this._managers.length > 0;
}

getPrimaryManager(): IntegrationManagerInstance {
if (this.hasManager()) {
// TODO: TravisR - Handle custom integration managers (widgets)
return this._managers[0];
return this._managers[this._managers.length - 1];
} else {
return null;
}
Expand All @@ -66,3 +107,6 @@ export class IntegrationManagers {
);
}
}

// For debugging
global.mxIntegrationManagers = IntegrationManagers;
11 changes: 11 additions & 0 deletions src/utils/WidgetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,17 @@ export default class WidgetUtils {
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)
* @return {Promise} Resolves on account data updated
Expand Down

0 comments on commit 74ce5c3

Please sign in to comment.