Merge branch 'krille/try-again-sending' into 'main'

feat: Try again uploading file events for one minute

See merge request famedly/company/frontend/famedlysdk!986
This commit is contained in:
Krille Fear 2022-03-30 06:38:01 +00:00
commit 23e0d29a0d
2 changed files with 38 additions and 21 deletions

View File

@ -102,6 +102,8 @@ class Client extends MatrixApi {
return await function(arg); return await function(arg);
} }
final Duration sendTimelineEventTimeout;
/// Create a client /// Create a client
/// [clientName] = unique identifier of this client /// [clientName] = unique identifier of this client
/// [databaseBuilder]: A function that creates the database instance, that will be used. /// [databaseBuilder]: A function that creates the database instance, that will be used.
@ -137,6 +139,8 @@ class Client extends MatrixApi {
/// will use lazy_load_members. /// will use lazy_load_members.
/// Set [compute] to the Flutter compute method to enable the SDK to run some /// Set [compute] to the Flutter compute method to enable the SDK to run some
/// code in background. /// code in background.
/// Set [timelineEventTimeout] to the preferred time the Client should retry
/// sending events on connection problems or to `Duration.zero` to disable it.
Client( Client(
this.clientName, { this.clientName, {
this.databaseBuilder, this.databaseBuilder,
@ -159,6 +163,7 @@ class Client extends MatrixApi {
this.compute, this.compute,
Level? logLevel, Level? logLevel,
Filter? syncFilter, Filter? syncFilter,
this.sendTimelineEventTimeout = const Duration(minutes: 1),
@deprecated bool? debug, @deprecated bool? debug,
}) : syncFilter = syncFilter ?? }) : syncFilter = syncFilter ??
Filter( Filter(

View File

@ -714,18 +714,34 @@ class Room {
uploadThumbnail = encryptedThumbnail.toMatrixFile(); uploadThumbnail = encryptedThumbnail.toMatrixFile();
} }
} }
final uploadResp = await client.uploadContent( Uri? uploadResp, thumbnailUploadResp;
uploadFile.bytes,
filename: uploadFile.name, final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout);
contentType: uploadFile.mimeType, while (uploadResp == null ||
); (uploadThumbnail != null && thumbnailUploadResp == null)) {
final thumbnailUploadResp = uploadThumbnail != null try {
? await client.uploadContent( uploadResp = await client.uploadContent(
uploadThumbnail.bytes, uploadFile.bytes,
filename: uploadThumbnail.name, filename: uploadFile.name,
contentType: uploadThumbnail.mimeType, contentType: uploadFile.mimeType,
) );
: null; thumbnailUploadResp = uploadThumbnail != null
? await client.uploadContent(
uploadThumbnail.bytes,
filename: uploadThumbnail.name,
contentType: uploadThumbnail.mimeType,
)
: null;
} on MatrixException catch (_) {
rethrow;
} catch (_) {
if (DateTime.now().isAfter(timeoutDate)) {
rethrow;
}
Logs().v('Send File into room failed. Try again...');
await Future.delayed(Duration(seconds: 1));
}
}
// Send event // Send event
final content = <String, dynamic>{ final content = <String, dynamic>{
@ -912,6 +928,7 @@ class Room {
); );
await _handleFakeSync(syncUpdate); await _handleFakeSync(syncUpdate);
final timeoutDate = DateTime.now().add(client.sendTimelineEventTimeout);
// Send the text and on success, store and display a *sent* event. // Send the text and on success, store and display a *sent* event.
String? res; String? res;
while (res == null) { while (res == null) {
@ -922,20 +939,15 @@ class Room {
txid: messageID, txid: messageID,
); );
} catch (e, s) { } catch (e, s) {
if ((DateTime.now().millisecondsSinceEpoch - if (e is MatrixException || DateTime.now().isAfter(timeoutDate)) {
sentDate.millisecondsSinceEpoch) < Logs().w('Problem while sending message', e, s);
(1000 * client.sendMessageTimeoutSeconds)) {
Logs().w('[Client] Problem while sending message because of "' +
e.toString() +
'". Try again in 1 seconds...');
await Future.delayed(Duration(seconds: 1));
} else {
Logs().w('[Client] Problem while sending message', e, s);
syncUpdate.rooms!.join!.values.first.timeline!.events!.first syncUpdate.rooms!.join!.values.first.timeline!.events!.first
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue; .unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
await _handleFakeSync(syncUpdate); await _handleFakeSync(syncUpdate);
return null; return null;
} }
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 syncUpdate.rooms!.join!.values.first.timeline!.events!.first