@@ -52,7 +52,16 @@ import {
5252 type GroupCallEventHandlerEventHandlerMap ,
5353} from "./webrtc/groupCallEventHandler.ts" ;
5454import * as utils from "./utils.ts" ;
55- import { deepCompare , defer , noUnsafeEventProps , type QueryDict , replaceParam , safeSet , sleep } from "./utils.ts" ;
55+ import {
56+ deepCompare ,
57+ defer ,
58+ MapWithDefault ,
59+ noUnsafeEventProps ,
60+ type QueryDict ,
61+ replaceParam ,
62+ safeSet ,
63+ sleep ,
64+ } from "./utils.ts" ;
5665import { Direction , EventTimeline } from "./models/event-timeline.ts" ;
5766import { type IActionsObject , PushProcessor } from "./pushprocessor.ts" ;
5867import { AutoDiscovery , type AutoDiscoveryAction } from "./autodiscovery.ts" ;
@@ -7948,7 +7957,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
79487957 * @param eventType - The type of event to send
79497958 * @param devices - The list of devices to send the event to.
79507959 * @param payload - The payload to send. This will be encrypted.
7951- * @returns Promise which resolves once queued there is no error feedback when sending fails.
7960+ * @returns Promise which resolves once send there. Can be rejected with an error if sending fails
7961+ * Sending will retry automatically but there is a Max retries limit.
79527962 */
79537963 public async encryptAndSendToDevice (
79547964 eventType : string ,
@@ -7960,9 +7970,20 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
79607970 }
79617971 const batch = await this . cryptoBackend . encryptToDeviceMessages ( eventType , devices , payload ) ;
79627972
7963- // TODO The batch mechanism removes all possibility to get error feedbacks..
7964- // We might want instead to do the API call directly and pass the errors back.
7965- await this . queueToDevice ( batch ) ;
7973+ const contentMap : MapWithDefault < string , Map < string , ToDevicePayload > > = new MapWithDefault ( ( ) => new Map ( ) ) ;
7974+ for ( const item of batch . batch ) {
7975+ contentMap . getOrCreate ( item . userId ) . set ( item . deviceId , item . payload ) ;
7976+ }
7977+
7978+ return new Promise < void > ( ( resolve , reject ) => {
7979+ this . queueToDevice ( batch , ( result ) => {
7980+ if ( result === undefined ) {
7981+ resolve ;
7982+ } else if ( result ) {
7983+ reject ( result ) ;
7984+ }
7985+ } ) ;
7986+ } ) ;
79667987 }
79677988
79687989 /**
@@ -7971,9 +7992,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
79717992 * batches for sending and stored in the store so they can be retried
79727993 * later if they fail to send. Retries will happen automatically.
79737994 * @param batch - The to-device messages to send
7995+ * @param sendCallback - Optional callback to call when the batch is sent
7996+ * @returns Promise which resolves once the batch is queued.
79747997 */
7975- public queueToDevice ( batch : ToDeviceBatch ) : Promise < void > {
7976- return this . toDeviceMessageQueue . queueBatch ( batch ) ;
7998+ public queueToDevice ( batch : ToDeviceBatch , sendCallback ?: ( result : Error | undefined ) => void ) : Promise < void > {
7999+ return this . toDeviceMessageQueue . queueBatch ( batch , sendCallback ) ;
79778000 }
79788001
79798002 /**
0 commit comments