From 981c3ea94dc1e99e365f0549becea3c77bfc0c25 Mon Sep 17 00:00:00 2001 From: td Date: Mon, 25 Mar 2024 19:31:01 +0530 Subject: [PATCH] chore: check message size before fake sync --- lib/src/room.dart | 36 ++++++++++++++++++------------------ lib/src/voip/call.dart | 22 +++++++++++----------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index fb32f9f0..0b7f9950 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -923,24 +923,11 @@ class Room { Future _sendContent( String type, - Map content, { + Map sendMessageContent, { String? txid, }) async { txid ??= client.generateUniqueTransactionId(); - final mustEncrypt = encrypted && client.encryptionEnabled; - - final sendMessageContent = mustEncrypt - ? await client.encryption! - .encryptGroupMessagePayload(id, content, type: type) - : content; - - final utf8EncodedJsonLength = - utf8.encode(jsonEncode(sendMessageContent)).length; - - if (utf8EncodedJsonLength > maxPDUSize) { - throw EventTooLarge(utf8EncodedJsonLength); - } return await client.sendMessage( id, sendMessageContent.containsKey('ciphertext') @@ -1053,6 +1040,21 @@ class Room { } } final sentDate = DateTime.now(); + + final mustEncrypt = encrypted && client.encryptionEnabled; + + final sendMessageContent = mustEncrypt + ? await client.encryption! + .encryptGroupMessagePayload(id, content, type: type) + : content; + + final utf8EncodedJsonLength = + utf8.encode(jsonEncode(sendMessageContent)).length; + + if (utf8EncodedJsonLength > maxPDUSize) { + throw EventTooLarge(utf8EncodedJsonLength); + } + final syncUpdate = SyncUpdate( nextBatch: '', rooms: RoomsUpdate( @@ -1092,13 +1094,11 @@ class Room { try { res = await _sendContent( type, - content, + sendMessageContent, txid: messageID, ); } catch (e, s) { - if (e is EventTooLarge) { - rethrow; - } else if (e is MatrixException && + if (e is MatrixException && e.retryAfterMs != null && !DateTime.now() .add(Duration(milliseconds: e.retryAfterMs!)) diff --git a/lib/src/voip/call.dart b/lib/src/voip/call.dart index 12815de3..514f15cb 100644 --- a/lib/src/voip/call.dart +++ b/lib/src/voip/call.dart @@ -1718,7 +1718,7 @@ class CallSession { if (capabilities != null) 'capabilities': capabilities.toJson(), if (metadata != null) sdpStreamMetadataKey: metadata.toJson(), }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallInvite, content, @@ -1746,7 +1746,7 @@ class CallSession { 'selected_party_id': selected_party_id, }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallSelectAnswer, content, @@ -1769,7 +1769,7 @@ class CallSession { 'version': version, }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallReject, content, @@ -1799,7 +1799,7 @@ class CallSession { if (capabilities != null) 'capabilities': capabilities.toJson(), if (metadata != null) sdpStreamMetadataKey: metadata.toJson(), }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallNegotiate, content, @@ -1842,7 +1842,7 @@ class CallSession { 'version': version, 'candidates': candidates, }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallCandidates, content, @@ -1872,7 +1872,7 @@ class CallSession { if (capabilities != null) 'capabilities': capabilities.toJson(), if (metadata != null) sdpStreamMetadataKey: metadata.toJson(), }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallAnswer, content, @@ -1894,7 +1894,7 @@ class CallSession { 'version': version, if (hangupCause != null) 'reason': hangupCause, }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallHangup, content, @@ -1926,7 +1926,7 @@ class CallSession { 'version': version, sdpStreamMetadataKey: metadata.toJson(), }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallSDPStreamMetadataChangedPrefix, content, @@ -1950,7 +1950,7 @@ class CallSession { 'version': version, ...callReplaces.toJson(), }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallReplaces, content, @@ -1974,7 +1974,7 @@ class CallSession { 'version': version, 'asserted_identity': assertedIdentity.toJson(), }; - return await _sendContent( + return await _sendCallContent( room, EventTypes.CallAssertedIdentity, content, @@ -1982,7 +1982,7 @@ class CallSession { ); } - Future _sendContent( + Future _sendCallContent( Room room, String type, Map content, {