From 6a57f99c004ddbfa66fe9793b5345b3829bf2656 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 30 Mar 2022 10:35:17 +0200 Subject: [PATCH] feat: Pass through a custom image resize function to the client This allows the use of the native imaging package in a more easy way. --- lib/src/event.dart | 4 ++++ lib/src/room.dart | 12 +++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index 305b1dd0..85170d09 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -518,6 +518,10 @@ class Event extends MatrixEvent { if (![EventTypes.Message, EventTypes.Sticker].contains(type)) { throw ("This event has the type '$type' and so it can't contain an attachment."); } + if (status.isSending) { + final localFile = room.sendingFilePlaceholders[eventId]; + if (localFile != null) return localFile; + } final database = room.client.database; final mxcUrl = attachmentOrThumbnailMxcUrl(getThumbnail: getThumbnail); if (mxcUrl == null) { diff --git a/lib/src/room.dart b/lib/src/room.dart index 64cf3527..81c1fb37 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -682,6 +682,8 @@ class Room { return sendEvent(event, txid: txid); } + final Map sendingFilePlaceholders = {}; + /// Sends a [file] to this room after uploading it. Returns the mxc uri of /// the uploaded file. If [waitUntilSent] is true, the future will wait until /// the message event has received the server. Otherwise the future will only @@ -698,12 +700,12 @@ class Room { String? txid, Event? inReplyTo, String? editEventId, - bool waitUntilSent = false, int? shrinkImageMaxDimension, MatrixImageFile? thumbnail, Map? extraContent, }) async { txid ??= client.generateUniqueTransactionId(); + sendingFilePlaceholders[txid] = file; // Create a fake Event object as a placeholder for the uploading file: final syncUpdate = SyncUpdate( @@ -798,12 +800,14 @@ class Room { syncUpdate.rooms!.join!.values.first.timeline!.events!.first .unsigned![messageSendingStatusKey] = EventStatus.error.intValue; await _handleFakeSync(syncUpdate); + sendingFilePlaceholders.remove(txid); rethrow; } catch (_) { if (DateTime.now().isAfter(timeoutDate)) { syncUpdate.rooms!.join!.values.first.timeline!.events!.first .unsigned![messageSendingStatusKey] = EventStatus.error.intValue; await _handleFakeSync(syncUpdate); + sendingFilePlaceholders.remove(txid); rethrow; } Logs().v('Send File into room failed. Try again...'); @@ -855,15 +859,13 @@ class Room { }, if (extraContent != null) ...extraContent, }; - final sendResponse = sendEvent( + await sendEvent( content, txid: txid, inReplyTo: inReplyTo, editEventId: editEventId, ); - if (waitUntilSent) { - await sendResponse; - } + sendingFilePlaceholders.remove(txid); return uploadResp; }