@@ -32,6 +32,7 @@ import { throttle } from "lodash";
32
32
import { CryptoEvent } from "matrix-js-sdk/src/crypto" ;
33
33
import { RoomType } from "matrix-js-sdk/src/@types/event" ;
34
34
import { DecryptionError } from "matrix-js-sdk/src/crypto/algorithms" ;
35
+ import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup" ;
35
36
36
37
// focus-visible is a Polyfill for the :focus-visible CSS pseudo-attribute used by various components
37
38
import "focus-visible" ;
@@ -355,7 +356,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
355
356
}
356
357
357
358
private async postLoginSetup ( ) : Promise < void > {
358
- const cli = MatrixClientPeg . get ( ) ;
359
+ const cli = MatrixClientPeg . safeGet ( ) ;
359
360
const cryptoEnabled = cli . isCryptoEnabled ( ) ;
360
361
if ( ! cryptoEnabled ) {
361
362
this . onLoggedIn ( ) ;
@@ -574,11 +575,11 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
574
575
if ( payload . event_type === "m.identity_server" ) {
575
576
const fullUrl = payload . event_content ? payload . event_content [ "base_url" ] : null ;
576
577
if ( ! fullUrl ) {
577
- MatrixClientPeg . get ( ) . setIdentityServerUrl ( undefined ) ;
578
+ MatrixClientPeg . safeGet ( ) . setIdentityServerUrl ( undefined ) ;
578
579
localStorage . removeItem ( "mx_is_access_token" ) ;
579
580
localStorage . removeItem ( "mx_is_url" ) ;
580
581
} else {
581
- MatrixClientPeg . get ( ) . setIdentityServerUrl ( fullUrl ) ;
582
+ MatrixClientPeg . safeGet ( ) . setIdentityServerUrl ( fullUrl ) ;
582
583
localStorage . removeItem ( "mx_is_access_token" ) ; // clear token
583
584
localStorage . setItem ( "mx_is_url" , fullUrl ) ; // XXX: Do we still need this?
584
585
}
@@ -625,7 +626,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
625
626
this . notifyNewScreen ( "forgot_password" ) ;
626
627
break ;
627
628
case "start_chat" :
628
- createRoom ( MatrixClientPeg . get ( ) , {
629
+ createRoom ( MatrixClientPeg . safeGet ( ) , {
629
630
dmUserId : payload . user_id ,
630
631
} ) ;
631
632
break ;
@@ -647,7 +648,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
647
648
// FIXME: controller shouldn't be loading a view :(
648
649
const modal = Modal . createDialog ( Spinner , undefined , "mx_Dialog_spinner" ) ;
649
650
650
- MatrixClientPeg . get ( )
651
+ MatrixClientPeg . safeGet ( )
651
652
. leave ( payload . room_id )
652
653
. then (
653
654
( ) => {
@@ -755,7 +756,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
755
756
this . viewSomethingBehindModal ( ) ;
756
757
break ;
757
758
case "view_invite" : {
758
- const room = MatrixClientPeg . get ( ) . getRoom ( payload . roomId ) ;
759
+ const room = MatrixClientPeg . safeGet ( ) . getRoom ( payload . roomId ) ;
759
760
if ( room ?. isSpaceRoom ( ) ) {
760
761
showSpaceInvite ( room ) ;
761
762
} else {
@@ -794,7 +795,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
794
795
Modal . createDialog ( DialPadModal , { } , "mx_Dialog_dialPadWrapper" ) ;
795
796
break ;
796
797
case Action . OnLoggedIn :
797
- this . stores . client = MatrixClientPeg . get ( ) ;
798
+ this . stores . client = MatrixClientPeg . safeGet ( ) ;
798
799
if (
799
800
// Skip this handling for token login as that always calls onLoggedIn itself
800
801
! this . tokenLogin &&
@@ -935,7 +936,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
935
936
}
936
937
937
938
let presentedId = roomInfo . room_alias || roomInfo . room_id ! ;
938
- const room = MatrixClientPeg . get ( ) . getRoom ( roomInfo . room_id ) ;
939
+ const room = MatrixClientPeg . safeGet ( ) . getRoom ( roomInfo . room_id ) ;
939
940
if ( room ) {
940
941
// Not all timeline events are decrypted ahead of time anymore
941
942
// Only the critical ones for a typical UI are
@@ -1062,14 +1063,14 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1062
1063
1063
1064
const [ shouldCreate , opts ] = await modal . finished ;
1064
1065
if ( shouldCreate ) {
1065
- createRoom ( MatrixClientPeg . get ( ) , opts ! ) ;
1066
+ createRoom ( MatrixClientPeg . safeGet ( ) , opts ! ) ;
1066
1067
}
1067
1068
}
1068
1069
1069
1070
private chatCreateOrReuse ( userId : string ) : void {
1070
1071
const snakedConfig = new SnakedObject < IConfigOptions > ( this . props . config ) ;
1071
1072
// Use a deferred action to reshow the dialog once the user has registered
1072
- if ( MatrixClientPeg . get ( ) . isGuest ( ) ) {
1073
+ if ( MatrixClientPeg . safeGet ( ) . isGuest ( ) ) {
1073
1074
// No point in making 2 DMs with welcome bot. This assumes view_set_mxid will
1074
1075
// result in a new DM with the welcome user.
1075
1076
if ( userId !== snakedConfig . get ( "welcome_user_id" ) ) {
@@ -1098,7 +1099,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1098
1099
1099
1100
// TODO: Immutable DMs replaces this
1100
1101
1101
- const client = MatrixClientPeg . get ( ) ;
1102
+ const client = MatrixClientPeg . safeGet ( ) ;
1102
1103
const dmRoom = findDMForUser ( client , userId ) ;
1103
1104
1104
1105
if ( dmRoom ) {
@@ -1116,7 +1117,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1116
1117
}
1117
1118
1118
1119
private leaveRoomWarnings ( roomId : string ) : JSX . Element [ ] {
1119
- const roomToLeave = MatrixClientPeg . get ( ) . getRoom ( roomId ) ;
1120
+ const roomToLeave = MatrixClientPeg . safeGet ( ) . getRoom ( roomId ) ;
1120
1121
const isSpace = roomToLeave ?. isSpaceRoom ( ) ;
1121
1122
// Show a warning if there are additional complications.
1122
1123
const warnings : JSX . Element [ ] = [ ] ;
@@ -1154,7 +1155,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1154
1155
}
1155
1156
1156
1157
private leaveRoom ( roomId : string ) : void {
1157
- const cli = MatrixClientPeg . get ( ) ;
1158
+ const cli = MatrixClientPeg . safeGet ( ) ;
1158
1159
const roomToLeave = cli . getRoom ( roomId ) ;
1159
1160
const warnings = this . leaveRoomWarnings ( roomId ) ;
1160
1161
@@ -1188,8 +1189,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1188
1189
}
1189
1190
1190
1191
private forgetRoom ( roomId : string ) : void {
1191
- const room = MatrixClientPeg . get ( ) . getRoom ( roomId ) ;
1192
- MatrixClientPeg . get ( )
1192
+ const room = MatrixClientPeg . safeGet ( ) . getRoom ( roomId ) ;
1193
+ MatrixClientPeg . safeGet ( )
1193
1194
. forget ( roomId )
1194
1195
. then ( ( ) => {
1195
1196
// Switch to home page if we're currently viewing the forgotten room
@@ -1212,7 +1213,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1212
1213
}
1213
1214
1214
1215
private async copyRoom ( roomId : string ) : Promise < void > {
1215
- const roomLink = makeRoomPermalink ( MatrixClientPeg . get ( ) , roomId ) ;
1216
+ const roomLink = makeRoomPermalink ( MatrixClientPeg . safeGet ( ) , roomId ) ;
1216
1217
const success = await copyPlaintext ( roomLink ) ;
1217
1218
if ( ! success ) {
1218
1219
Modal . createDialog ( ErrorDialog , {
@@ -1246,7 +1247,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1246
1247
1247
1248
const welcomeUserRooms = DMRoomMap . shared ( ) . getDMRoomsForUserId ( welcomeUserId ) ;
1248
1249
if ( welcomeUserRooms . length === 0 ) {
1249
- const roomId = await createRoom ( MatrixClientPeg . get ( ) , {
1250
+ const roomId = await createRoom ( MatrixClientPeg . safeGet ( ) , {
1250
1251
dmUserId : snakedConfig . get ( "welcome_user_id" ) ,
1251
1252
// Only view the welcome user if we're NOT looking at a room
1252
1253
andView : ! this . state . currentRoomId ,
@@ -1262,11 +1263,11 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1262
1263
// the saved sync to be loaded).
1263
1264
const saveWelcomeUser = ( ev : MatrixEvent ) : void => {
1264
1265
if ( ev . getType ( ) === EventType . Direct && ev . getContent ( ) [ welcomeUserId ] ) {
1265
- MatrixClientPeg . get ( ) . store . save ( true ) ;
1266
- MatrixClientPeg . get ( ) . removeListener ( ClientEvent . AccountData , saveWelcomeUser ) ;
1266
+ MatrixClientPeg . safeGet ( ) . store . save ( true ) ;
1267
+ MatrixClientPeg . safeGet ( ) . removeListener ( ClientEvent . AccountData , saveWelcomeUser ) ;
1267
1268
}
1268
1269
} ;
1269
- MatrixClientPeg . get ( ) . on ( ClientEvent . AccountData , saveWelcomeUser ) ;
1270
+ MatrixClientPeg . safeGet ( ) . on ( ClientEvent . AccountData , saveWelcomeUser ) ;
1270
1271
1271
1272
return roomId ;
1272
1273
}
@@ -1413,7 +1414,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1413
1414
// Before defaulting to directory, show the last viewed room
1414
1415
this . viewLastRoom ( ) ;
1415
1416
} else {
1416
- if ( MatrixClientPeg . get ( ) . isGuest ( ) ) {
1417
+ if ( MatrixClientPeg . safeGet ( ) . isGuest ( ) ) {
1417
1418
dis . dispatch ( { action : "view_welcome_page" } ) ;
1418
1419
} else {
1419
1420
dis . dispatch ( { action : Action . ViewHomePage } ) ;
@@ -1468,7 +1469,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1468
1469
// to do the first sync
1469
1470
this . firstSyncComplete = false ;
1470
1471
this . firstSyncPromise = defer ( ) ;
1471
- const cli = MatrixClientPeg . get ( ) ;
1472
+ const cli = MatrixClientPeg . safeGet ( ) ;
1472
1473
1473
1474
// Allow the JS SDK to reap timeline events. This reduces the amount of
1474
1475
// memory consumed as the JS SDK stores multiple distinct copies of room
@@ -1588,7 +1589,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1588
1589
cli . on ( MatrixEventEvent . Decrypted , ( e , err ) => dft . eventDecrypted ( e , err as DecryptionError ) ) ;
1589
1590
1590
1591
cli . on ( ClientEvent . Room , ( room ) => {
1591
- if ( MatrixClientPeg . get ( ) . isCryptoEnabled ( ) ) {
1592
+ if ( cli . isCryptoEnabled ( ) ) {
1592
1593
const blacklistEnabled = SettingsStore . getValueAt (
1593
1594
SettingLevel . ROOM_DEVICE ,
1594
1595
"blacklistUnverifiedDevices" ,
@@ -1618,15 +1619,15 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1618
1619
}
1619
1620
} ) ;
1620
1621
cli . on ( CryptoEvent . KeyBackupFailed , async ( errcode ) : Promise < void > => {
1621
- let haveNewVersion ;
1622
- let newVersionInfo ;
1622
+ let haveNewVersion : boolean | undefined ;
1623
+ let newVersionInfo : IKeyBackupInfo | null = null ;
1623
1624
// if key backup is still enabled, there must be a new backup in place
1624
- if ( MatrixClientPeg . get ( ) . getKeyBackupEnabled ( ) ) {
1625
+ if ( cli . getKeyBackupEnabled ( ) ) {
1625
1626
haveNewVersion = true ;
1626
1627
} else {
1627
1628
// otherwise check the server to see if there's a new one
1628
1629
try {
1629
- newVersionInfo = await MatrixClientPeg . get ( ) . getKeyBackupVersion ( ) ;
1630
+ newVersionInfo = await cli . getKeyBackupVersion ( ) ;
1630
1631
if ( newVersionInfo !== null ) haveNewVersion = true ;
1631
1632
} catch ( e ) {
1632
1633
logger . error ( "Saw key backup error but failed to check backup version!" , e ) ;
@@ -1639,7 +1640,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1639
1640
import (
1640
1641
"../../async-components/views/dialogs/security/NewRecoveryMethodDialog"
1641
1642
) as unknown as Promise < typeof NewRecoveryMethodDialog > ,
1642
- { newVersionInfo } ,
1643
+ { newVersionInfo : newVersionInfo ! } ,
1643
1644
) ;
1644
1645
} else {
1645
1646
Modal . createDialogAsync (
@@ -1686,7 +1687,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1686
1687
* @private
1687
1688
*/
1688
1689
private onClientStarted ( ) : void {
1689
- const cli = MatrixClientPeg . get ( ) ;
1690
+ const cli = MatrixClientPeg . safeGet ( ) ;
1690
1691
1691
1692
if ( cli . isCryptoEnabled ( ) ) {
1692
1693
const blacklistEnabled = SettingsStore . getValueAt ( SettingLevel . DEVICE , "blacklistUnverifiedDevices" ) ;
@@ -1734,7 +1735,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
1734
1735
params : params ,
1735
1736
} ) ;
1736
1737
} else if ( screen === "soft_logout" ) {
1737
- if ( cli . getUserId ( ) && ! Lifecycle . isSoftLogout ( ) ) {
1738
+ if ( ! ! cli ? .getUserId ( ) && ! Lifecycle . isSoftLogout ( ) ) {
1738
1739
// Logged in - visit a room
1739
1740
this . viewLastRoom ( ) ;
1740
1741
} else {
@@ -2057,7 +2058,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
2057
2058
{ ...this . props }
2058
2059
{ ...this . state }
2059
2060
ref = { this . loggedInView }
2060
- matrixClient = { MatrixClientPeg . get ( ) }
2061
+ matrixClient = { MatrixClientPeg . safeGet ( ) }
2061
2062
onRegistered = { this . onRegistered }
2062
2063
currentRoomId = { this . state . currentRoomId }
2063
2064
/>
0 commit comments