diff --git a/lib/matrix_api_lite/generated/api.dart b/lib/matrix_api_lite/generated/api.dart index 062af4fb..1e244738 100644 --- a/lib/matrix_api_lite/generated/api.dart +++ b/lib/matrix_api_lite/generated/api.dart @@ -22,6 +22,10 @@ class Api { throw Exception('http error response'); } + Never bodySizeExceeded(int expected, int actual) { + throw Exception('body size $actual exceeded $expected'); + } + /// Gets discovery information about the domain. The file may include /// additional keys, which MUST follow the Java package naming convention, /// e.g. `com.example.myapp.property`. This ensures property names are @@ -3798,6 +3802,10 @@ class Api { request.headers['authorization'] = 'Bearer ${bearerToken!}'; request.headers['content-type'] = 'application/json'; request.bodyBytes = utf8.encode(jsonEncode(body)); + const maxBodySize = 60000; + if (request.bodyBytes.length > maxBodySize) { + bodySizeExceeded(maxBodySize, request.bodyBytes.length); + } final response = await httpClient.send(request); final responseBody = await response.stream.toBytes(); if (response.statusCode != 200) unexpectedResponse(response, responseBody); diff --git a/lib/matrix_api_lite/matrix_api.dart b/lib/matrix_api_lite/matrix_api.dart index b9434ae4..9a73b6bc 100644 --- a/lib/matrix_api_lite/matrix_api.dart +++ b/lib/matrix_api_lite/matrix_api.dart @@ -56,6 +56,11 @@ class MatrixApi extends Api { super.unexpectedResponse(response, body); } + @override + Never bodySizeExceeded(int expected, int actual) { + throw EventTooLarge(expected, actual); + } + MatrixApi({ Uri? homeserver, String? accessToken, @@ -236,3 +241,8 @@ class MatrixApi extends Api { return getRoomKeyBySessionId(roomId, sessionId, version); } } + +class EventTooLarge implements Exception { + int maxSize, actualSize; + EventTooLarge(this.maxSize, this.actualSize); +} diff --git a/lib/src/room.dart b/lib/src/room.dart index 9d0b7865..cb2cd94f 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -966,12 +966,6 @@ class Room { .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') @@ -2380,8 +2374,3 @@ enum EncryptionHealthState { allVerified, unverifiedDevices, } - -class EventTooLarge implements Exception { - int length; - EventTooLarge(this.length); -}