@@ -1150,47 +1150,7 @@ export class SyncApi {
1150
1150
toDeviceMessages = await this . syncOpts . cryptoCallbacks . preprocessToDeviceMessages ( toDeviceMessages ) ;
1151
1151
}
1152
1152
1153
- const cancelledKeyVerificationTxns : string [ ] = [ ] ;
1154
- toDeviceMessages
1155
- . map ( mapToDeviceEvent )
1156
- . map ( ( toDeviceEvent ) => {
1157
- // map is a cheap inline forEach
1158
- // We want to flag m.key.verification.start events as cancelled
1159
- // if there's an accompanying m.key.verification.cancel event, so
1160
- // we pull out the transaction IDs from the cancellation events
1161
- // so we can flag the verification events as cancelled in the loop
1162
- // below.
1163
- if ( toDeviceEvent . getType ( ) === "m.key.verification.cancel" ) {
1164
- const txnId : string = toDeviceEvent . getContent ( ) [ "transaction_id" ] ;
1165
- if ( txnId ) {
1166
- cancelledKeyVerificationTxns . push ( txnId ) ;
1167
- }
1168
- }
1169
-
1170
- // as mentioned above, .map is a cheap inline forEach, so return
1171
- // the unmodified event.
1172
- return toDeviceEvent ;
1173
- } )
1174
- . forEach ( function ( toDeviceEvent ) {
1175
- const content = toDeviceEvent . getContent ( ) ;
1176
- if ( toDeviceEvent . getType ( ) == "m.room.message" && content . msgtype == "m.bad.encrypted" ) {
1177
- // the mapper already logged a warning.
1178
- logger . log ( "Ignoring undecryptable to-device event from " + toDeviceEvent . getSender ( ) ) ;
1179
- return ;
1180
- }
1181
-
1182
- if (
1183
- toDeviceEvent . getType ( ) === "m.key.verification.start" ||
1184
- toDeviceEvent . getType ( ) === "m.key.verification.request"
1185
- ) {
1186
- const txnId = content [ "transaction_id" ] ;
1187
- if ( cancelledKeyVerificationTxns . includes ( txnId ) ) {
1188
- toDeviceEvent . flagCancelled ( ) ;
1189
- }
1190
- }
1191
-
1192
- client . emit ( ClientEvent . ToDeviceEvent , toDeviceEvent ) ;
1193
- } ) ;
1153
+ processToDeviceMessages ( toDeviceMessages , client ) ;
1194
1154
} else {
1195
1155
// no more to-device events: we can stop polling with a short timeout.
1196
1156
this . catchingUp = false ;
@@ -1946,7 +1906,56 @@ export function _createAndReEmitRoom(client: MatrixClient, roomId: string, opts:
1946
1906
return room ;
1947
1907
}
1948
1908
1949
- export function mapToDeviceEvent ( plainOldJsObject : Partial < IEvent > ) : MatrixEvent {
1909
+ /**
1910
+ * Process a list of (decrypted, where possible) received to-device events.
1911
+ *
1912
+ * Converts the events into `MatrixEvent`s, and emits appropriate {@link ClientEvent.ToDeviceEvent} events.
1913
+ * */
1914
+ export function processToDeviceMessages ( toDeviceMessages : IToDeviceEvent [ ] , client : MatrixClient ) : void {
1915
+ const cancelledKeyVerificationTxns : string [ ] = [ ] ;
1916
+ toDeviceMessages
1917
+ . map ( mapToDeviceEvent )
1918
+ . map ( ( toDeviceEvent ) => {
1919
+ // map is a cheap inline forEach
1920
+ // We want to flag m.key.verification.start events as cancelled
1921
+ // if there's an accompanying m.key.verification.cancel event, so
1922
+ // we pull out the transaction IDs from the cancellation events
1923
+ // so we can flag the verification events as cancelled in the loop
1924
+ // below.
1925
+ if ( toDeviceEvent . getType ( ) === "m.key.verification.cancel" ) {
1926
+ const txnId : string = toDeviceEvent . getContent ( ) [ "transaction_id" ] ;
1927
+ if ( txnId ) {
1928
+ cancelledKeyVerificationTxns . push ( txnId ) ;
1929
+ }
1930
+ }
1931
+
1932
+ // as mentioned above, .map is a cheap inline forEach, so return
1933
+ // the unmodified event.
1934
+ return toDeviceEvent ;
1935
+ } )
1936
+ . forEach ( function ( toDeviceEvent ) {
1937
+ const content = toDeviceEvent . getContent ( ) ;
1938
+ if ( toDeviceEvent . getType ( ) == "m.room.message" && content . msgtype == "m.bad.encrypted" ) {
1939
+ // the mapper already logged a warning.
1940
+ logger . log ( "Ignoring undecryptable to-device event from " + toDeviceEvent . getSender ( ) ) ;
1941
+ return ;
1942
+ }
1943
+
1944
+ if (
1945
+ toDeviceEvent . getType ( ) === "m.key.verification.start" ||
1946
+ toDeviceEvent . getType ( ) === "m.key.verification.request"
1947
+ ) {
1948
+ const txnId = content [ "transaction_id" ] ;
1949
+ if ( cancelledKeyVerificationTxns . includes ( txnId ) ) {
1950
+ toDeviceEvent . flagCancelled ( ) ;
1951
+ }
1952
+ }
1953
+
1954
+ client . emit ( ClientEvent . ToDeviceEvent , toDeviceEvent ) ;
1955
+ } ) ;
1956
+ }
1957
+
1958
+ function mapToDeviceEvent ( plainOldJsObject : Partial < IEvent > ) : MatrixEvent {
1950
1959
// to-device events should not have a `room_id` property, but let's be sure
1951
1960
delete plainOldJsObject . room_id ;
1952
1961
return new MatrixEvent ( plainOldJsObject ) ;
0 commit comments