@@ -15,6 +15,8 @@ limitations under the License.
1515*/
1616
1717import { MatrixEvent } from "matrix-js-sdk/src" ;
18+ import { sleep } from "matrix-js-sdk/src/utils" ;
19+ import { ISyncStateData , SyncState } from "matrix-js-sdk/src/sync" ;
1820
1921import SdkConfig from '../SdkConfig' ;
2022import sendBugReport from '../rageshake/submit-rageshake' ;
@@ -23,14 +25,17 @@ import { AsyncStoreWithClient } from './AsyncStoreWithClient';
2325import { ActionPayload } from '../dispatcher/payloads' ;
2426import SettingsStore from "../settings/SettingsStore" ;
2527
26- // Minimum interval of 5 minutes between reports, especially important when we're doing an initial sync with a lot of decryption errors
27- const RAGESHAKE_INTERVAL = 5 * 60 * 1000 ;
28+ // Minimum interval of 1 minute between reports
29+ const RAGESHAKE_INTERVAL = 60000 ;
30+ // Before rageshaking, wait 5 seconds and see if the message has successfully decrypted
31+ const GRACE_PERIOD = 5000 ;
2832// Event type for to-device messages requesting sender auto-rageshakes
2933const AUTO_RS_REQUEST = "im.vector.auto_rs_request" ;
3034
3135interface IState {
3236 reportedSessionIds : Set < string > ;
3337 lastRageshakeTime : number ;
38+ initialSyncCompleted : boolean ;
3439}
3540
3641/**
@@ -45,9 +50,11 @@ export default class AutoRageshakeStore extends AsyncStoreWithClient<IState> {
4550 super ( defaultDispatcher , {
4651 reportedSessionIds : new Set < string > ( ) ,
4752 lastRageshakeTime : 0 ,
53+ initialSyncCompleted : false ,
4854 } ) ;
4955 this . onDecryptionAttempt = this . onDecryptionAttempt . bind ( this ) ;
5056 this . onDeviceMessage = this . onDeviceMessage . bind ( this ) ;
57+ this . onSyncStateChange = this . onSyncStateChange . bind ( this ) ;
5158 }
5259
5360 public static get instance ( ) : AutoRageshakeStore {
@@ -64,6 +71,7 @@ export default class AutoRageshakeStore extends AsyncStoreWithClient<IState> {
6471 if ( this . matrixClient ) {
6572 this . matrixClient . on ( 'Event.decrypted' , this . onDecryptionAttempt ) ;
6673 this . matrixClient . on ( 'toDeviceEvent' , this . onDeviceMessage ) ;
74+ this . matrixClient . on ( 'sync' , this . onSyncStateChange ) ;
6775 }
6876 }
6977
@@ -75,9 +83,14 @@ export default class AutoRageshakeStore extends AsyncStoreWithClient<IState> {
7583 }
7684
7785 private async onDecryptionAttempt ( ev : MatrixEvent ) : Promise < void > {
86+ if ( ! this . state . initialSyncCompleted ) { return ; }
87+
7888 const wireContent = ev . getWireContent ( ) ;
7989 const sessionId = wireContent . session_id ;
8090 if ( ev . isDecryptionFailure ( ) && ! this . state . reportedSessionIds . has ( sessionId ) ) {
91+ await sleep ( GRACE_PERIOD ) ;
92+ if ( ! ev . isDecryptionFailure ( ) ) { return ; }
93+
8194 const newReportedSessionIds = new Set ( this . state . reportedSessionIds ) ;
8295 await this . updateState ( { reportedSessionIds : newReportedSessionIds . add ( sessionId ) } ) ;
8396
@@ -114,6 +127,12 @@ export default class AutoRageshakeStore extends AsyncStoreWithClient<IState> {
114127 }
115128 }
116129
130+ private async onSyncStateChange ( _state : SyncState , _prevState : SyncState , data : ISyncStateData ) {
131+ if ( ! this . state . initialSyncCompleted ) {
132+ await this . updateState ( { initialSyncCompleted : ! ! data . nextSyncToken } ) ;
133+ }
134+ }
135+
117136 private async onDeviceMessage ( ev : MatrixEvent ) : Promise < void > {
118137 if ( ev . getType ( ) !== AUTO_RS_REQUEST ) return ;
119138 const messageContent = ev . getContent ( ) ;
0 commit comments