@@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { DecryptionError } from "matrix-js-sdk/src/crypto/algorithms" ;
18
17
import { MatrixEvent } from "matrix-js-sdk/src/matrix" ;
19
18
import { Error as ErrorEvent } from "@matrix-org/analytics-events/types/typescript/Error" ;
19
+ import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api" ;
20
20
21
21
import { PosthogAnalytics } from "./PosthogAnalytics" ;
22
22
@@ -25,17 +25,15 @@ export class DecryptionFailure {
25
25
26
26
public constructor (
27
27
public readonly failedEventId : string ,
28
- public readonly errorCode : string ,
28
+ public readonly errorCode : DecryptionFailureCode ,
29
29
) {
30
30
this . ts = Date . now ( ) ;
31
31
}
32
32
}
33
33
34
- type ErrorCode = "OlmKeysNotSentError" | "OlmIndexError" | "UnknownError" | "OlmUnspecifiedError" ;
35
-
34
+ type ErrorCode = ErrorEvent [ "name" ] ;
36
35
type TrackingFn = ( count : number , trackedErrCode : ErrorCode , rawError : string ) => void ;
37
-
38
- export type ErrCodeMapFn = ( errcode : string ) => ErrorCode ;
36
+ export type ErrCodeMapFn = ( errcode : DecryptionFailureCode ) => ErrorCode ;
39
37
40
38
export class DecryptionFailureTracker {
41
39
private static internalInstance = new DecryptionFailureTracker (
@@ -52,12 +50,14 @@ export class DecryptionFailureTracker {
52
50
( errorCode ) => {
53
51
// Map JS-SDK error codes to tracker codes for aggregation
54
52
switch ( errorCode ) {
55
- case " MEGOLM_UNKNOWN_INBOUND_SESSION_ID" :
53
+ case DecryptionFailureCode . MEGOLM_UNKNOWN_INBOUND_SESSION_ID :
56
54
return "OlmKeysNotSentError" ;
57
- case " OLM_UNKNOWN_MESSAGE_INDEX" :
55
+ case DecryptionFailureCode . OLM_UNKNOWN_MESSAGE_INDEX :
58
56
return "OlmIndexError" ;
59
- case undefined :
60
- return "OlmUnspecifiedError" ;
57
+ case DecryptionFailureCode . HISTORICAL_MESSAGE_NO_KEY_BACKUP :
58
+ case DecryptionFailureCode . HISTORICAL_MESSAGE_BACKUP_UNCONFIGURED :
59
+ case DecryptionFailureCode . HISTORICAL_MESSAGE_WORKING_BACKUP :
60
+ return "HistoricalMessage" ;
61
61
default :
62
62
return "UnknownError" ;
63
63
}
@@ -76,11 +76,11 @@ export class DecryptionFailureTracker {
76
76
// accumulated in `failureCounts`.
77
77
public visibleFailures : Map < string , DecryptionFailure > = new Map ( ) ;
78
78
79
- // A histogram of the number of failures that will be tracked at the next tracking
80
- // interval, split by failure error code.
81
- public failureCounts : Record < string , number > = {
82
- // [errorCode]: 42
83
- } ;
79
+ /**
80
+ * A histogram of the number of failures that will be tracked at the next tracking
81
+ * interval, split by failure error code.
82
+ */
83
+ private failureCounts : Map < DecryptionFailureCode , number > = new Map ( ) ;
84
84
85
85
// Event IDs of failures that were tracked previously
86
86
public trackedEvents : Set < string > = new Set ( ) ;
@@ -108,10 +108,10 @@ export class DecryptionFailureTracker {
108
108
*
109
109
* @param {function } fn The tracking function, which will be called when failures
110
110
* are tracked. The function should have a signature `(count, trackedErrorCode) => {...}`,
111
- * where `count` is the number of failures and `errorCode` matches the `.code` of
112
- * provided DecryptionError errors (by default, unless `errorCodeMapFn` is specified.
113
- * @param {function? } errorCodeMapFn The function used to map error codes to the
114
- * trackedErrorCode. If not provided, the `.code` of errors will be used .
111
+ * where `count` is the number of failures and `errorCode` matches the output of `errorCodeMapFn`.
112
+ *
113
+ * @param {function } errorCodeMapFn The function used to map decryption failure reason codes to the
114
+ * ` trackedErrorCode` .
115
115
*/
116
116
private constructor (
117
117
private readonly fn : TrackingFn ,
@@ -138,13 +138,15 @@ export class DecryptionFailureTracker {
138
138
// localStorage.setItem('mx-decryption-failure-event-ids', JSON.stringify([...this.trackedEvents]));
139
139
// }
140
140
141
- public eventDecrypted ( e : MatrixEvent , err : DecryptionError ) : void {
142
- // for now we only track megolm decrytion failures
141
+ public eventDecrypted ( e : MatrixEvent ) : void {
142
+ // for now we only track megolm decryption failures
143
143
if ( e . getWireContent ( ) . algorithm != "m.megolm.v1.aes-sha2" ) {
144
144
return ;
145
145
}
146
- if ( err ) {
147
- this . addDecryptionFailure ( new DecryptionFailure ( e . getId ( ) ! , err . code ) ) ;
146
+
147
+ const errCode = e . decryptionFailureReason ;
148
+ if ( errCode !== null ) {
149
+ this . addDecryptionFailure ( new DecryptionFailure ( e . getId ( ) ! , errCode ) ) ;
148
150
} else {
149
151
// Could be an event in the failures, remove it
150
152
this . removeDecryptionFailuresForEvent ( e ) ;
@@ -205,7 +207,7 @@ export class DecryptionFailureTracker {
205
207
this . failures = new Map ( ) ;
206
208
this . visibleEvents = new Set ( ) ;
207
209
this . visibleFailures = new Map ( ) ;
208
- this . failureCounts = { } ;
210
+ this . failureCounts = new Map ( ) ;
209
211
}
210
212
211
213
/**
@@ -236,7 +238,7 @@ export class DecryptionFailureTracker {
236
238
private aggregateFailures ( failures : Set < DecryptionFailure > ) : void {
237
239
for ( const failure of failures ) {
238
240
const errorCode = failure . errorCode ;
239
- this . failureCounts [ errorCode ] = ( this . failureCounts [ errorCode ] || 0 ) + 1 ;
241
+ this . failureCounts . set ( errorCode , ( this . failureCounts . get ( errorCode ) ?? 0 ) + 1 ) ;
240
242
}
241
243
}
242
244
@@ -245,12 +247,12 @@ export class DecryptionFailureTracker {
245
247
* function with the number of failures that should be tracked.
246
248
*/
247
249
public trackFailures ( ) : void {
248
- for ( const errorCode of Object . keys ( this . failureCounts ) ) {
249
- if ( this . failureCounts [ errorCode ] > 0 ) {
250
+ for ( const [ errorCode , count ] of this . failureCounts . entries ( ) ) {
251
+ if ( count > 0 ) {
250
252
const trackedErrorCode = this . errorCodeMapFn ( errorCode ) ;
251
253
252
- this . fn ( this . failureCounts [ errorCode ] , trackedErrorCode , errorCode ) ;
253
- this . failureCounts [ errorCode ] = 0 ;
254
+ this . fn ( count , trackedErrorCode , errorCode ) ;
255
+ this . failureCounts . set ( errorCode , 0 ) ;
254
256
}
255
257
}
256
258
}
0 commit comments