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

Commit ef33adc

Browse files
committed
Merge branch 'travis/integs/widgets' into travis/integs/account_set
2 parents 60778d8 + d2c7a5a commit ef33adc

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/Lifecycle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { sendLoginRequest } from "./Login";
3535
import * as StorageManager from './utils/StorageManager';
3636
import SettingsStore from "./settings/SettingsStore";
3737
import TypingStore from "./stores/TypingStore";
38+
import {IntegrationManagers} from "./integrations/IntegrationManagers";
3839

3940
/**
4041
* Called at startup, to attempt to build a logged-in Matrix session. It tries
@@ -580,6 +581,7 @@ async function startMatrixClient(startSyncing=true) {
580581
Presence.start();
581582
}
582583
DMRoomMap.makeShared().start();
584+
IntegrationManagers.sharedInstance().startWatching();
583585
ActiveWidgetStore.start();
584586

585587
if (startSyncing) {
@@ -638,6 +640,7 @@ export function stopMatrixClient(unsetClient=true) {
638640
TypingStore.sharedInstance().reset();
639641
Presence.stop();
640642
ActiveWidgetStore.stop();
643+
IntegrationManagers.sharedInstance().stopWatching();
641644
if (DMRoomMap.shared()) DMRoomMap.shared().stop();
642645
const cli = MatrixClientPeg.get();
643646
if (cli) {

src/integrations/IntegrationManagers.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import SdkConfig from '../SdkConfig';
1818
import sdk from "../index";
1919
import Modal from '../Modal';
2020
import {IntegrationManagerInstance} from "./IntegrationManagerInstance";
21+
import type {MatrixClient, MatrixEvent} from "matrix-js-sdk";
22+
import WidgetUtils from "../utils/WidgetUtils";
23+
import MatrixClientPeg from "../MatrixClientPeg";
2124

2225
export class IntegrationManagers {
2326
static _instance;
@@ -30,9 +33,28 @@ export class IntegrationManagers {
3033
}
3134

3235
_managers: IntegrationManagerInstance[] = [];
36+
_client: MatrixClient;
3337

3438
constructor() {
39+
this._compileManagers();
40+
}
41+
42+
startWatching(): void {
43+
this.stopWatching();
44+
this._client = MatrixClientPeg.get();
45+
this._client.on("accountData", this._onAccountData.bind(this));
46+
this._compileManagers();
47+
}
48+
49+
stopWatching(): void {
50+
if (!this._client) return;
51+
this._client.removeListener("accountData", this._onAccountData.bind(this));
52+
}
53+
54+
_compileManagers() {
55+
this._managers = [];
3556
this._setupConfiguredManager();
57+
this._setupAccountManagers();
3658
}
3759

3860
_setupConfiguredManager() {
@@ -44,14 +66,34 @@ export class IntegrationManagers {
4466
}
4567
}
4668

69+
_setupAccountManagers() {
70+
if (!this._client || !this._client.getUserId()) return; // not logged in
71+
const widgets = WidgetUtils.getIntegrationManagerWidgets();
72+
widgets.forEach(w => {
73+
const data = w.content['data'];
74+
if (!data) return;
75+
76+
const uiUrl = w.content['url'];
77+
const apiUrl = data['api_url'];
78+
if (!apiUrl || !uiUrl) return;
79+
80+
this._managers.push(new IntegrationManagerInstance(apiUrl, uiUrl));
81+
});
82+
}
83+
84+
_onAccountData(ev: MatrixEvent): void {
85+
if (ev.getType() === 'm.widgets') {
86+
this._compileManagers();
87+
}
88+
}
89+
4790
hasManager(): boolean {
4891
return this._managers.length > 0;
4992
}
5093

5194
getPrimaryManager(): IntegrationManagerInstance {
5295
if (this.hasManager()) {
53-
// TODO: TravisR - Handle custom integration managers (widgets)
54-
return this._managers[0];
96+
return this._managers[this._managers.length - 1];
5597
} else {
5698
return null;
5799
}
@@ -66,3 +108,6 @@ export class IntegrationManagers {
66108
);
67109
}
68110
}
111+
112+
// For debugging
113+
global.mxIntegrationManagers = IntegrationManagers;

src/utils/WidgetUtils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,17 @@ export default class WidgetUtils {
340340
return widgets.filter((widget) => widget.content && widget.content.type === "m.stickerpicker");
341341
}
342342

343+
/**
344+
* Get all integration manager widgets for this user.
345+
* @returns {Object[]} An array of integration manager user widgets.
346+
*/
347+
static getIntegrationManagerWidgets() {
348+
const widgets = WidgetUtils.getUserWidgetsArray();
349+
// We'll be using im.vector.integration_manager until MSC1957 or similar is accepted.
350+
const imTypes = ["m.integration_manager", "im.vector.integration_manager"];
351+
return widgets.filter(w => w.content && imTypes.includes(w.content.type));
352+
}
353+
343354
/**
344355
* Remove all stickerpicker widgets (stickerpickers are user widgets by nature)
345356
* @return {Promise} Resolves on account data updated

0 commit comments

Comments
 (0)