@@ -15,9 +15,10 @@ import {
15
15
type SyncState ,
16
16
ClientStoppedError ,
17
17
} from "matrix-js-sdk/src/matrix" ;
18
- import { logger as baseLogger } from "matrix-js-sdk/src/logger" ;
18
+ import { logger as baseLogger , LogSpan } from "matrix-js-sdk/src/logger" ;
19
19
import { CryptoEvent , type KeyBackupInfo } from "matrix-js-sdk/src/crypto-api" ;
20
20
import { type CryptoSessionStateChange } from "@matrix-org/analytics-events/types/typescript/CryptoSessionStateChange" ;
21
+ import { secureRandomString } from "matrix-js-sdk/src/randomstring" ;
21
22
22
23
import { PosthogAnalytics } from "./PosthogAnalytics" ;
23
24
import dis from "./dispatcher/dispatcher" ;
@@ -96,6 +97,7 @@ export default class DeviceListener {
96
97
this . client . on ( ClientEvent . AccountData , this . onAccountData ) ;
97
98
this . client . on ( ClientEvent . Sync , this . onSync ) ;
98
99
this . client . on ( RoomStateEvent . Events , this . onRoomStateEvents ) ;
100
+ this . client . on ( ClientEvent . ToDeviceEvent , this . onToDeviceEvent ) ;
99
101
this . shouldRecordClientInformation = SettingsStore . getValue ( "deviceClientInformationOptIn" ) ;
100
102
// only configurable in config, so we don't need to watch the value
101
103
this . enableBulkUnverifiedSessionsReminder = SettingsStore . getValue ( UIFeature . BulkUnverifiedSessionsReminder ) ;
@@ -118,6 +120,7 @@ export default class DeviceListener {
118
120
this . client . removeListener ( ClientEvent . AccountData , this . onAccountData ) ;
119
121
this . client . removeListener ( ClientEvent . Sync , this . onSync ) ;
120
122
this . client . removeListener ( RoomStateEvent . Events , this . onRoomStateEvents ) ;
123
+ this . client . removeListener ( ClientEvent . ToDeviceEvent , this . onToDeviceEvent ) ;
121
124
}
122
125
SettingsStore . unwatchSetting ( this . deviceClientInformationSettingWatcherRef ) ;
123
126
dis . unregister ( this . dispatcherRef ) ;
@@ -225,6 +228,11 @@ export default class DeviceListener {
225
228
this . updateClientInformation ( ) ;
226
229
} ;
227
230
231
+ private onToDeviceEvent = ( event : MatrixEvent ) : void => {
232
+ // Receiving a 4S secret can mean we are in sync where we were not before.
233
+ if ( event . getType ( ) === EventType . SecretSend ) this . recheck ( ) ;
234
+ } ;
235
+
228
236
/**
229
237
* Fetch the key backup information from the server.
230
238
*
@@ -273,18 +281,29 @@ export default class DeviceListener {
273
281
274
282
private async doRecheck ( ) : Promise < void > {
275
283
if ( ! this . running || ! this . client ) return ; // we have been stopped
284
+ const logSpan = new LogSpan ( logger , "check_" + secureRandomString ( 4 ) ) ;
285
+
276
286
const cli = this . client ;
277
287
278
288
// cross-signing support was added to Matrix in MSC1756, which landed in spec v1.1
279
- if ( ! ( await cli . isVersionSupported ( "v1.1" ) ) ) return ;
289
+ if ( ! ( await cli . isVersionSupported ( "v1.1" ) ) ) {
290
+ logSpan . debug ( "cross-signing not supported" ) ;
291
+ return ;
292
+ }
280
293
281
294
const crypto = cli . getCrypto ( ) ;
282
- if ( ! crypto ) return ;
295
+ if ( ! crypto ) {
296
+ logSpan . debug ( "crypto not enabled" ) ;
297
+ return ;
298
+ }
283
299
284
300
// don't recheck until the initial sync is complete: lots of account data events will fire
285
301
// while the initial sync is processing and we don't need to recheck on each one of them
286
302
// (we add a listener on sync to do once check after the initial sync is done)
287
- if ( ! cli . isInitialSyncComplete ( ) ) return ;
303
+ if ( ! cli . isInitialSyncComplete ( ) ) {
304
+ logSpan . debug ( "initial sync not yet complete" ) ;
305
+ return ;
306
+ }
288
307
289
308
const crossSigningReady = await crypto . isCrossSigningReady ( ) ;
290
309
const secretStorageReady = await crypto . isSecretStorageReady ( ) ;
@@ -306,6 +325,7 @@ export default class DeviceListener {
306
325
await this . reportCryptoSessionStateToAnalytics ( cli ) ;
307
326
308
327
if ( this . dismissedThisDeviceToast || allSystemsReady ) {
328
+ logSpan . info ( "No toast needed" ) ;
309
329
hideSetupEncryptionToast ( ) ;
310
330
311
331
this . checkKeyBackupStatus ( ) ;
@@ -316,27 +336,33 @@ export default class DeviceListener {
316
336
if ( ! crossSigningReady ) {
317
337
// This account is legacy and doesn't have cross-signing set up at all.
318
338
// Prompt the user to set it up.
319
- logger . info ( "Cross-signing not ready: showing SET_UP_ENCRYPTION toast" ) ;
339
+ logSpan . info ( "Cross-signing not ready: showing SET_UP_ENCRYPTION toast" ) ;
320
340
showSetupEncryptionToast ( SetupKind . SET_UP_ENCRYPTION ) ;
321
341
} else if ( ! isCurrentDeviceTrusted ) {
322
342
// cross signing is ready but the current device is not trusted: prompt the user to verify
323
- logger . info ( "Current device not verified: showing VERIFY_THIS_SESSION toast" ) ;
343
+ logSpan . info ( "Current device not verified: showing VERIFY_THIS_SESSION toast" ) ;
324
344
showSetupEncryptionToast ( SetupKind . VERIFY_THIS_SESSION ) ;
325
345
} else if ( ! allCrossSigningSecretsCached ) {
326
346
// cross signing ready & device trusted, but we are missing secrets from our local cache.
327
347
// prompt the user to enter their recovery key.
328
- logger . info ( "Some secrets not cached: showing KEY_STORAGE_OUT_OF_SYNC toast" ) ;
348
+ logSpan . info (
349
+ "Some secrets not cached: showing KEY_STORAGE_OUT_OF_SYNC toast" ,
350
+ crossSigningStatus . privateKeysCachedLocally ,
351
+ ) ;
329
352
showSetupEncryptionToast ( SetupKind . KEY_STORAGE_OUT_OF_SYNC ) ;
330
353
} else if ( defaultKeyId === null ) {
331
354
// the user just hasn't set up 4S yet: prompt them to do so (unless they've explicitly said no to key storage)
332
355
const disabledEvent = cli . getAccountData ( BACKUP_DISABLED_ACCOUNT_DATA_KEY ) ;
333
356
if ( ! disabledEvent ?. getContent ( ) . disabled ) {
357
+ logSpan . info ( "No default 4S key: showing SET_UP_RECOVERY toast" ) ;
334
358
showSetupEncryptionToast ( SetupKind . SET_UP_RECOVERY ) ;
359
+ } else {
360
+ logSpan . info ( "No default 4S key but backup disabled: no toast needed" ) ;
335
361
}
336
362
} else {
337
363
// some other condition... yikes! Show the 'set up encryption' toast: this is what we previously did
338
364
// in 'other' situations. Possibly we should consider prompting for a full reset in this case?
339
- logger . warn ( "Couldn't match encryption state to a known case: showing 'setup encryption' prompt" , {
365
+ logSpan . warn ( "Couldn't match encryption state to a known case: showing 'setup encryption' prompt" , {
340
366
crossSigningReady,
341
367
secretStorageReady,
342
368
allCrossSigningSecretsCached,
@@ -345,6 +371,8 @@ export default class DeviceListener {
345
371
} ) ;
346
372
showSetupEncryptionToast ( SetupKind . SET_UP_ENCRYPTION ) ;
347
373
}
374
+ } else {
375
+ logSpan . info ( "Not yet ready, but shouldShowSetupEncryptionToast==false" ) ;
348
376
}
349
377
350
378
// This needs to be done after awaiting on getUserDeviceInfo() above, so
@@ -377,9 +405,9 @@ export default class DeviceListener {
377
405
}
378
406
}
379
407
380
- logger . debug ( "Old unverified sessions: " + Array . from ( oldUnverifiedDeviceIds ) . join ( "," ) ) ;
381
- logger . debug ( "New unverified sessions: " + Array . from ( newUnverifiedDeviceIds ) . join ( "," ) ) ;
382
- logger . debug ( "Currently showing toasts for: " + Array . from ( this . displayingToastsForDeviceIds ) . join ( "," ) ) ;
408
+ logSpan . debug ( "Old unverified sessions: " + Array . from ( oldUnverifiedDeviceIds ) . join ( "," ) ) ;
409
+ logSpan . debug ( "New unverified sessions: " + Array . from ( newUnverifiedDeviceIds ) . join ( "," ) ) ;
410
+ logSpan . debug ( "Currently showing toasts for: " + Array . from ( this . displayingToastsForDeviceIds ) . join ( "," ) ) ;
383
411
384
412
const isBulkUnverifiedSessionsReminderSnoozed = isBulkUnverifiedDeviceReminderSnoozed ( ) ;
385
413
@@ -404,7 +432,7 @@ export default class DeviceListener {
404
432
// ...and hide any we don't need any more
405
433
for ( const deviceId of this . displayingToastsForDeviceIds ) {
406
434
if ( ! newUnverifiedDeviceIds . has ( deviceId ) ) {
407
- logger . debug ( "Hiding unverified session toast for " + deviceId ) ;
435
+ logSpan . debug ( "Hiding unverified session toast for " + deviceId ) ;
408
436
hideUnverifiedSessionsToast ( deviceId ) ;
409
437
}
410
438
}
0 commit comments