From 3ddec73ac5abbef054b49138a7a1cde35ee46bb4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 3 Jun 2024 13:15:05 +0200 Subject: [PATCH] When replying from notification, do not interfere with `specialModeEventTimelineItem` --- .../MessageComposerPresenterTest.kt | 4 +-- .../libraries/matrix/api/timeline/Timeline.kt | 8 +++++- .../matrix/impl/timeline/RustTimeline.kt | 25 +++++++++++++++---- .../matrix/test/timeline/FakeTimeline.kt | 7 ++++-- .../NotificationBroadcastReceiver.kt | 3 ++- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/textcomposer/MessageComposerPresenterTest.kt b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/textcomposer/MessageComposerPresenterTest.kt index a9a5dfe356..1103f4f484 100644 --- a/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/textcomposer/MessageComposerPresenterTest.kt +++ b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/textcomposer/MessageComposerPresenterTest.kt @@ -395,7 +395,7 @@ class MessageComposerPresenterTest { @Test fun `present - reply message`() = runTest { - val replyMessageLambda = lambdaRecorder { _: EventId, _: String, _: String?, _: List -> + val replyMessageLambda = lambdaRecorder { _: EventId, _: String, _: String?, _: List, _: Boolean -> Result.success(Unit) } val timeline = FakeTimeline().apply { @@ -909,7 +909,7 @@ class MessageComposerPresenterTest { @OptIn(ExperimentalCoroutinesApi::class) @Test fun `present - send messages with intentional mentions`() = runTest { - val replyMessageLambda = lambdaRecorder { _: EventId, _: String, _: String?, _: List -> + val replyMessageLambda = lambdaRecorder { _: EventId, _: String, _: String?, _: List, _: Boolean -> Result.success(Unit) } val editMessageLambda = lambdaRecorder { _: EventId?, _: TransactionId?, _: String, _: String?, _: List -> diff --git a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/timeline/Timeline.kt b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/timeline/Timeline.kt index 6d0850db83..5c1da08b65 100644 --- a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/timeline/Timeline.kt +++ b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/timeline/Timeline.kt @@ -57,7 +57,13 @@ interface Timeline : AutoCloseable { suspend fun enterSpecialMode(eventId: EventId?): Result - suspend fun replyMessage(eventId: EventId, body: String, htmlBody: String?, mentions: List): Result + suspend fun replyMessage( + eventId: EventId, + body: String, + htmlBody: String?, + mentions: List, + fromNotification: Boolean = false, + ): Result suspend fun sendImage( file: File, diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustTimeline.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustTimeline.kt index fee898988e..d15565a3f4 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustTimeline.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustTimeline.kt @@ -308,13 +308,28 @@ class RustTimeline( } } - override suspend fun replyMessage(eventId: EventId, body: String, htmlBody: String?, mentions: List): Result = withContext(dispatcher) { + override suspend fun replyMessage( + eventId: EventId, + body: String, + htmlBody: String?, + mentions: List, + fromNotification: Boolean, + ): Result = withContext(dispatcher) { runCatching { - val inReplyTo = specialModeEventTimelineItem ?: inner.getEventTimelineItemByEventId(eventId.value) - inReplyTo.use { eventTimelineItem -> - inner.sendReply(messageEventContentFromParts(body, htmlBody).withMentions(mentions.map()), eventTimelineItem) + val msg = messageEventContentFromParts(body, htmlBody).withMentions(mentions.map()) + if (fromNotification) { + // When replying from a notification, do not interfere with `specialModeEventTimelineItem` + val inReplyTo = inner.getEventTimelineItemByEventId(eventId.value) + inReplyTo.use { eventTimelineItem -> + inner.sendReply(msg, eventTimelineItem) + } + } else { + val inReplyTo = specialModeEventTimelineItem ?: inner.getEventTimelineItemByEventId(eventId.value) + inReplyTo.use { eventTimelineItem -> + inner.sendReply(msg, eventTimelineItem) + } + specialModeEventTimelineItem = null } - specialModeEventTimelineItem = null } } diff --git a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/timeline/FakeTimeline.kt b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/timeline/FakeTimeline.kt index 6ec8386651..1cb4a3c3eb 100644 --- a/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/timeline/FakeTimeline.kt +++ b/libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/timeline/FakeTimeline.kt @@ -104,7 +104,8 @@ class FakeTimeline( body: String, htmlBody: String?, mentions: List, - ) -> Result = { _, _, _, _ -> + fromNotification: Boolean, + ) -> Result = { _, _, _, _, _ -> Result.success(Unit) } @@ -113,11 +114,13 @@ class FakeTimeline( body: String, htmlBody: String?, mentions: List, + fromNotification: Boolean, ): Result = replyMessageLambda( eventId, body, htmlBody, - mentions + mentions, + fromNotification, ) var sendImageLambda: ( diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationBroadcastReceiver.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationBroadcastReceiver.kt index b071d61482..6a10f49a3b 100644 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationBroadcastReceiver.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationBroadcastReceiver.kt @@ -179,7 +179,8 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { eventId = threadId.asEventId(), body = message, htmlBody = null, - mentions = emptyList() + mentions = emptyList(), + fromNotification = true, ) } else { room.liveTimeline.sendMessage(