From 97bf7723ddfad8770a48fc961dcfdbd1016b45a0 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 11 Sep 2023 16:10:04 +0200 Subject: [PATCH] fix: Wait for rate limit to pass while sending messages Otherwise the user can't handle that exception themselves, since we don't rethrow. --- lib/src/room.dart | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index f4b3e6e8..861f41d2 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1100,7 +1100,16 @@ class Room { txid: messageID, ); } catch (e, s) { - if (e is MatrixException || DateTime.now().isAfter(timeoutDate)) { + if (e is MatrixException && + e.retryAfterMs != null && + !DateTime.now() + .add(Duration(milliseconds: e.retryAfterMs!)) + .isAfter(timeoutDate)) { + Logs().w( + 'Ratelimited while sending message, waiting for ${e.retryAfterMs}ms'); + await Future.delayed(Duration(milliseconds: e.retryAfterMs!)); + } else if (e is MatrixException || + DateTime.now().isAfter(timeoutDate)) { Logs().w('Problem while sending message', e, s); syncUpdate.rooms!.join!.values.first.timeline!.events!.first .unsigned![messageSendingStatusKey] = EventStatus.error.intValue; @@ -1108,9 +1117,11 @@ class Room { completer.complete(); _sendingQueue.remove(completer); return null; + } else { + Logs() + .w('Problem while sending message: $e Try again in 1 seconds...'); + await Future.delayed(Duration(seconds: 1)); } - Logs().w('Problem while sending message: $e Try again in 1 seconds...'); - await Future.delayed(Duration(seconds: 1)); } } syncUpdate.rooms!.join!.values.first.timeline!.events!.first