From f2d240c66d7a065c17daf2ad8e077f519e262a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Wed, 20 Mar 2024 09:36:35 +0100 Subject: [PATCH] Fix SDK integration --- .../MessageComposerPresenter.kt | 7 +++- .../libraries/matrix/api/room/MatrixRoom.kt | 18 ++++++++- .../matrix/impl/room/RustMatrixRoom.kt | 40 +++++++++++++++++-- .../matrix/test/room/FakeMatrixRoom.kt | 4 ++ .../libraries/mediaupload/api/MediaSender.kt | 24 +++++++++-- 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/messagecomposer/MessageComposerPresenter.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/messagecomposer/MessageComposerPresenter.kt index b98f5fcb39..37e404d314 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/messagecomposer/MessageComposerPresenter.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/messagecomposer/MessageComposerPresenter.kt @@ -463,7 +463,12 @@ class MessageComposerPresenter @Inject constructor( } } } - mediaSender.sendMedia(uri, mimeType, compressIfPossible = false, progressCallback).getOrThrow() + mediaSender.sendMedia( + uri = uri, + mimeType = mimeType, + compressIfPossible = false, + progressCallback = progressCallback + ).getOrThrow() } .onSuccess { attachmentState.value = AttachmentsState.None diff --git a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt index 07b72e3c5f..7e407aad4f 100644 --- a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt +++ b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt @@ -127,9 +127,23 @@ interface MatrixRoom : Closeable { suspend fun redactEvent(eventId: EventId, reason: String? = null): Result - suspend fun sendImage(file: File, thumbnailFile: File?, imageInfo: ImageInfo, progressCallback: ProgressCallback?): Result + suspend fun sendImage( + file: File, + thumbnailFile: File?, + imageInfo: ImageInfo, + body: String?, + formattedBody: String?, + progressCallback: ProgressCallback? + ): Result - suspend fun sendVideo(file: File, thumbnailFile: File?, videoInfo: VideoInfo, progressCallback: ProgressCallback?): Result + suspend fun sendVideo( + file: File, + thumbnailFile: File?, + videoInfo: VideoInfo, + body: String?, + formattedBody: String?, + progressCallback: ProgressCallback? + ): Result suspend fun sendAudio(file: File, audioInfo: AudioInfo, progressCallback: ProgressCallback?): Result diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt index 1eb10bb4e8..622d39bd61 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt @@ -75,6 +75,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.matrix.rustcomponents.sdk.EventTimelineItem +import org.matrix.rustcomponents.sdk.MessageFormat import org.matrix.rustcomponents.sdk.RoomInfo import org.matrix.rustcomponents.sdk.RoomInfoListener import org.matrix.rustcomponents.sdk.RoomListItem @@ -90,6 +91,7 @@ import org.matrix.rustcomponents.sdk.use import timber.log.Timber import uniffi.matrix_sdk.RoomPowerLevelChanges import java.io.File +import org.matrix.rustcomponents.sdk.FormattedBody as RustFormattedBody import org.matrix.rustcomponents.sdk.Room as InnerRoom import org.matrix.rustcomponents.sdk.Timeline as InnerTimeline @@ -436,10 +438,21 @@ class RustMatrixRoom( file: File, thumbnailFile: File?, imageInfo: ImageInfo, + body: String?, + formattedBody: String?, progressCallback: ProgressCallback?, ): Result { return sendAttachment(listOfNotNull(file, thumbnailFile)) { - innerTimeline.sendImage(file.path, thumbnailFile?.path, imageInfo.map(), progressCallback?.toProgressWatcher()) + innerTimeline.sendImage( + url = file.path, + thumbnailUrl = thumbnailFile?.path, + imageInfo = imageInfo.map(), + caption = body, + formattedCaption = formattedBody?.let { + RustFormattedBody(body = it, format = MessageFormat.Html) + }, + progressWatcher = progressCallback?.toProgressWatcher() + ) } } @@ -447,16 +460,34 @@ class RustMatrixRoom( file: File, thumbnailFile: File?, videoInfo: VideoInfo, + body: String?, + formattedBody: String?, progressCallback: ProgressCallback?, ): Result { return sendAttachment(listOfNotNull(file, thumbnailFile)) { - innerTimeline.sendVideo(file.path, thumbnailFile?.path, videoInfo.map(), progressCallback?.toProgressWatcher()) + innerTimeline.sendVideo( + url = file.path, + thumbnailUrl = thumbnailFile?.path, + videoInfo = videoInfo.map(), + caption = body, + formattedCaption = formattedBody?.let { + RustFormattedBody(body = it, format = MessageFormat.Html) + }, + progressWatcher = progressCallback?.toProgressWatcher() + ) } } override suspend fun sendAudio(file: File, audioInfo: AudioInfo, progressCallback: ProgressCallback?): Result { return sendAttachment(listOf(file)) { - innerTimeline.sendAudio(file.path, audioInfo.map(), progressCallback?.toProgressWatcher()) + innerTimeline.sendAudio( + url = file.path, + audioInfo = audioInfo.map(), + // Maybe allow a caption in the future? + caption = null, + formattedCaption = null, + progressWatcher = progressCallback?.toProgressWatcher() + ) } } @@ -653,6 +684,9 @@ class RustMatrixRoom( url = file.path, audioInfo = audioInfo.map(), waveform = waveform.toMSC3246range(), + // Maybe allow a caption in the future? + caption = null, + formattedCaption = null, progressWatcher = progressCallback?.toProgressWatcher(), ) } diff --git a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt index 5bab9e6a84..1872d2e60d 100644 --- a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt +++ b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt @@ -357,6 +357,8 @@ class FakeMatrixRoom( file: File, thumbnailFile: File?, imageInfo: ImageInfo, + body: String?, + formattedBody: String?, progressCallback: ProgressCallback? ): Result = fakeSendMedia(progressCallback) @@ -364,6 +366,8 @@ class FakeMatrixRoom( file: File, thumbnailFile: File?, videoInfo: VideoInfo, + body: String?, + formattedBody: String?, progressCallback: ProgressCallback? ): Result = fakeSendMedia( progressCallback diff --git a/libraries/mediaupload/api/src/main/kotlin/io/element/android/libraries/mediaupload/api/MediaSender.kt b/libraries/mediaupload/api/src/main/kotlin/io/element/android/libraries/mediaupload/api/MediaSender.kt index 89698a577e..16c166fd94 100644 --- a/libraries/mediaupload/api/src/main/kotlin/io/element/android/libraries/mediaupload/api/MediaSender.kt +++ b/libraries/mediaupload/api/src/main/kotlin/io/element/android/libraries/mediaupload/api/MediaSender.kt @@ -37,6 +37,8 @@ class MediaSender @Inject constructor( uri: Uri, mimeType: String, compressIfPossible: Boolean, + caption: String? = null, + formattedCaption: String? = null, progressCallback: ProgressCallback? = null ): Result { return preProcessor @@ -44,10 +46,15 @@ class MediaSender @Inject constructor( uri = uri, mimeType = mimeType, deleteOriginal = true, - compressIfPossible = compressIfPossible + compressIfPossible = compressIfPossible, ) .flatMapCatching { info -> - room.sendMedia(info, progressCallback) + room.sendMedia( + uploadInfo = info, + progressCallback = progressCallback, + caption = caption, + formattedCaption = formattedCaption + ) } .handleSendResult() } @@ -71,7 +78,12 @@ class MediaSender @Inject constructor( audioInfo = audioInfo, waveform = waveForm, ) - room.sendMedia(newInfo, progressCallback) + room.sendMedia( + uploadInfo = newInfo, + progressCallback = progressCallback, + caption = null, + formattedCaption = null + ) } .handleSendResult() } @@ -90,6 +102,8 @@ class MediaSender @Inject constructor( private suspend fun MatrixRoom.sendMedia( uploadInfo: MediaUploadInfo, progressCallback: ProgressCallback?, + caption: String?, + formattedCaption: String?, ): Result { val handler = when (uploadInfo) { is MediaUploadInfo.Image -> { @@ -97,6 +111,8 @@ class MediaSender @Inject constructor( file = uploadInfo.file, thumbnailFile = uploadInfo.thumbnailFile, imageInfo = uploadInfo.imageInfo, + body = caption, + formattedBody = formattedCaption, progressCallback = progressCallback ) } @@ -105,6 +121,8 @@ class MediaSender @Inject constructor( file = uploadInfo.file, thumbnailFile = uploadInfo.thumbnailFile, videoInfo = uploadInfo.videoInfo, + body = caption, + formattedBody = formattedCaption, progressCallback = progressCallback ) }