@@ -18,6 +18,9 @@ import SdkConfig from '../SdkConfig';
1818import sdk from "../index" ;
1919import Modal from '../Modal' ;
2020import { IntegrationManagerInstance } from "./IntegrationManagerInstance" ;
21+ import type { MatrixClient , MatrixEvent } from "matrix-js-sdk" ;
22+ import WidgetUtils from "../utils/WidgetUtils" ;
23+ import MatrixClientPeg from "../MatrixClientPeg" ;
2124
2225export 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 ;
0 commit comments