Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 47 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,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 {
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 +108,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