diff --git a/lib/src/client.dart b/lib/src/client.dart index 5d8cb94..71f7504 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -61,6 +61,8 @@ class Client extends MatrixApi { Set roomPreviewLastEvents; + int sendMessageTimeoutSeconds; + /// Create a client /// [clientName] = unique identifier of this client /// [database]: The database instance to use @@ -92,6 +94,7 @@ class Client extends MatrixApi { this.importantStateEvents, this.roomPreviewLastEvents, this.pinUnreadRooms = false, + this.sendMessageTimeoutSeconds = 60, @deprecated bool debug, }) { verificationMethods ??= {}; diff --git a/lib/src/room.dart b/lib/src/room.dart index f30a534..19fa640 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -722,7 +722,7 @@ class Room { content['formatted_body'] = '* ' + content['formatted_body']; } } - + final sentDate = DateTime.now(); final syncUpdate = SyncUpdate() ..rooms = (RoomsUpdate() ..join = ({}..[id] = (JoinedRoomUpdate() @@ -733,7 +733,7 @@ class Room { ..type = type ..eventId = messageID ..senderId = client.userID - ..originServerTs = DateTime.now() + ..originServerTs = sentDate ..unsigned = { MessageSendingStatusKey: 0, 'transaction_id': messageID, @@ -742,26 +742,38 @@ class Room { await _handleFakeSync(syncUpdate); // Send the text and on success, store and display a *sent* event. - try { - final res = await _sendContent( - type, - content, - txid: messageID, - ); - syncUpdate.rooms.join.values.first.timeline.events.first - .unsigned[MessageSendingStatusKey] = 1; - syncUpdate.rooms.join.values.first.timeline.events.first.eventId = res; - await _handleFakeSync(syncUpdate); - - return res; - } catch (e, s) { - Logs.warning( - '[Client] Problem while sending message: ' + e.toString(), s); - syncUpdate.rooms.join.values.first.timeline.events.first - .unsigned[MessageSendingStatusKey] = -1; - await _handleFakeSync(syncUpdate); + String res; + while (res == null) { + try { + res = await _sendContent( + type, + content, + txid: messageID, + ); + } catch (e, s) { + if ((DateTime.now().millisecondsSinceEpoch - + sentDate.millisecondsSinceEpoch) < + (1000 * client.sendMessageTimeoutSeconds)) { + Logs.warning('[Client] Problem while sending message because of "' + + e.toString() + + '". Try again in 1 seconds...'); + await Future.delayed(Duration(seconds: 1)); + } else { + Logs.warning( + '[Client] Problem while sending message: ' + e.toString(), s); + syncUpdate.rooms.join.values.first.timeline.events.first + .unsigned[MessageSendingStatusKey] = -1; + await _handleFakeSync(syncUpdate); + return null; + } + } } - return null; + syncUpdate.rooms.join.values.first.timeline.events.first + .unsigned[MessageSendingStatusKey] = 1; + syncUpdate.rooms.join.values.first.timeline.events.first.eventId = res; + await _handleFakeSync(syncUpdate); + + return res; } /// Call the Matrix API to join this room if the user is not already a member. diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 3ce1a09..8717e75 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -33,7 +33,8 @@ void main() { var updateCount = 0; var insertList = []; - var client = Client('testclient', httpClient: FakeMatrixApi()); + var client = Client('testclient', + httpClient: FakeMatrixApi(), sendMessageTimeoutSeconds: 5); var room = Room( id: roomID, client: client, prev_batch: '1234', roomAccountData: {});