From b5b120d09ef921cdb1abf6290d638de06473c590 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 2 Aug 2023 21:53:05 +0200 Subject: [PATCH] Update rust sdk to 0.1.39 (#1024) Co-authored-by: ganfra --- gradle/libs.versions.toml | 2 +- .../impl/notification/NotificationMapper.kt | 22 +++++++------------ ...imelineEventToNotificationContentMapper.kt | 15 ++++++++----- .../matrix/impl/room/RoomContentForwarder.kt | 2 +- .../matrix/impl/room/RustMatrixRoom.kt | 5 ++--- .../timeline/MatrixTimelineDiffProcessor.kt | 9 ++++++-- .../impl/timeline/RoomTimelineExtensions.kt | 4 ++-- .../impl/timeline/RustMatrixTimeline.kt | 21 ++++++++---------- 8 files changed, 39 insertions(+), 41 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e908aba3a7..e5e0539b46 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -145,7 +145,7 @@ jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup" } appyx_core = { module = "com.bumble.appyx:core", version.ref = "appyx" } molecule-runtime = { module = "app.cash.molecule:molecule-runtime", version.ref = "molecule" } timber = "com.jakewharton.timber:timber:5.0.1" -matrix_sdk = "org.matrix.rustcomponents:sdk-android:0.1.38" +matrix_sdk = "org.matrix.rustcomponents:sdk-android:0.1.39" sqldelight-driver-android = { module = "com.squareup.sqldelight:android-driver", version.ref = "sqldelight" } sqldelight-driver-jvm = { module = "com.squareup.sqldelight:sqlite-driver", version.ref = "sqldelight" } sqldelight-coroutines = { module = "com.squareup.sqldelight:coroutines-extensions", version.ref = "sqldelight" } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/NotificationMapper.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/NotificationMapper.kt index 3a18c7f19a..0d9f794173 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/NotificationMapper.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/NotificationMapper.kt @@ -20,7 +20,6 @@ import io.element.android.libraries.core.bool.orFalse import io.element.android.libraries.matrix.api.core.EventId import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.core.SessionId -import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.notification.NotificationContent import io.element.android.libraries.matrix.api.notification.NotificationData import io.element.android.libraries.matrix.api.room.RoomMembershipState @@ -30,7 +29,7 @@ import org.matrix.rustcomponents.sdk.NotificationItem import org.matrix.rustcomponents.sdk.use class NotificationMapper( - private val sessionId: SessionId, + sessionId: SessionId, private val clock: SystemClock, ) { private val notificationContentMapper = NotificationContentMapper(sessionId) @@ -40,7 +39,6 @@ class NotificationMapper( roomId: RoomId, notificationItem: NotificationItem ): NotificationData { - val senderId = UserId(notificationItem.senderInfo.senderId) return notificationItem.use { item -> NotificationData( eventId = eventId, @@ -52,22 +50,20 @@ class NotificationMapper( isDirect = item.roomInfo.isDirect, isEncrypted = item.roomInfo.isEncrypted.orFalse(), isNoisy = item.isNoisy.orFalse(), - timestamp = item.timestamp(clock), - content = item.event.use { notificationContentMapper.map(senderId, it) }, + timestamp = item.timestamp() ?: clock.epochMillis(), + content = item.event.use { notificationContentMapper.map(it) }, contentUrl = null, ) } } } -class NotificationContentMapper( - private val sessionId: SessionId, -) { +class NotificationContentMapper(private val sessionId: SessionId) { private val timelineEventToNotificationContentMapper = TimelineEventToNotificationContentMapper() - fun map(senderId: UserId, notificationEvent: NotificationEvent): NotificationContent = + fun map(notificationEvent: NotificationEvent): NotificationContent = when (notificationEvent) { - is NotificationEvent.Timeline -> timelineEventToNotificationContentMapper.map(senderId, notificationEvent.event) + is NotificationEvent.Timeline -> timelineEventToNotificationContentMapper.map(notificationEvent.event) is NotificationEvent.Invite -> NotificationContent.StateEvent.RoomMemberContent( userId = sessionId.value, membershipState = RoomMembershipState.INVITE, @@ -75,8 +71,6 @@ class NotificationContentMapper( } } -private fun NotificationItem.timestamp(clock: SystemClock): Long { - // FIXME we can't get the timestamp from the notification item anymore, so we need to fake it -// return (this.event as? NotificationEvent.Timeline)?.event?.timestamp()?.toLong() - return clock.epochMillis() +private fun NotificationItem.timestamp(): Long? { + return (this.event as? NotificationEvent.Timeline)?.event?.timestamp()?.toLong() } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/TimelineEventToNotificationContentMapper.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/TimelineEventToNotificationContentMapper.kt index 1bf11b8e93..e30e57113d 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/TimelineEventToNotificationContentMapper.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/TimelineEventToNotificationContentMapper.kt @@ -22,15 +22,18 @@ import io.element.android.libraries.matrix.impl.room.RoomMemberMapper import io.element.android.libraries.matrix.impl.timeline.item.event.EventMessageMapper import org.matrix.rustcomponents.sdk.MessageLikeEventContent import org.matrix.rustcomponents.sdk.StateEventContent +import org.matrix.rustcomponents.sdk.TimelineEvent import org.matrix.rustcomponents.sdk.TimelineEventType import org.matrix.rustcomponents.sdk.use import javax.inject.Inject class TimelineEventToNotificationContentMapper @Inject constructor() { - fun map(senderId: UserId, timelineEventType: TimelineEventType): NotificationContent { - return timelineEventType.use { - it.toContent(senderId = senderId) + fun map(timelineEvent: TimelineEvent): NotificationContent { + return timelineEvent.use { + timelineEvent.eventType().use { eventType -> + eventType.toContent(senderId = UserId(timelineEvent.senderId())) + } } } } @@ -72,7 +75,7 @@ private fun StateEventContent.toContent(): NotificationContent.StateEvent { private fun MessageLikeEventContent.toContent(senderId: UserId): NotificationContent.MessageLike { return use { - when (it) { + when (this) { MessageLikeEventContent.CallAnswer -> NotificationContent.MessageLike.CallAnswer MessageLikeEventContent.CallCandidates -> NotificationContent.MessageLike.CallCandidates MessageLikeEventContent.CallHangup -> NotificationContent.MessageLike.CallHangup @@ -84,10 +87,10 @@ private fun MessageLikeEventContent.toContent(senderId: UserId): NotificationCon MessageLikeEventContent.KeyVerificationMac -> NotificationContent.MessageLike.KeyVerificationMac MessageLikeEventContent.KeyVerificationReady -> NotificationContent.MessageLike.KeyVerificationReady MessageLikeEventContent.KeyVerificationStart -> NotificationContent.MessageLike.KeyVerificationStart - is MessageLikeEventContent.ReactionContent -> NotificationContent.MessageLike.ReactionContent(it.relatedEventId) + is MessageLikeEventContent.ReactionContent -> NotificationContent.MessageLike.ReactionContent(relatedEventId) MessageLikeEventContent.RoomEncrypted -> NotificationContent.MessageLike.RoomEncrypted is MessageLikeEventContent.RoomMessage -> { - NotificationContent.MessageLike.RoomMessage(senderId, EventMessageMapper().mapMessageType(it.messageType)) + NotificationContent.MessageLike.RoomMessage(senderId, EventMessageMapper().mapMessageType(messageType)) } MessageLikeEventContent.RoomRedaction -> NotificationContent.MessageLike.RoomRedaction MessageLikeEventContent.Sticker -> NotificationContent.MessageLike.Sticker diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt index 4e2d63d091..c4f272797a 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt @@ -80,6 +80,6 @@ class RoomContentForwarder( } private object NoOpTimelineListener : TimelineListener { - override fun onUpdate(diff: TimelineDiff) = Unit + override fun onUpdate(diff: List) = Unit } } 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 5503c22b1d..dc74a273f4 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 @@ -372,14 +372,13 @@ class RustMatrixRoom( } //TODO handle cancellation, need refactoring of how we are catching errors - private suspend fun sendAttachment(handle: () -> SendAttachmentJoinHandle): Result = withContext(roomDispatcher) { - runCatching { + private suspend fun sendAttachment(handle: () -> SendAttachmentJoinHandle): Result { + return runCatching { handle().use { it.join() } } } - } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt index b14d459697..fd880e7fe3 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt @@ -23,6 +23,7 @@ import kotlinx.coroutines.sync.withLock import org.matrix.rustcomponents.sdk.TimelineChange import org.matrix.rustcomponents.sdk.TimelineDiff import org.matrix.rustcomponents.sdk.TimelineItem +import timber.log.Timber internal class MatrixTimelineDiffProcessor( private val timelineItems: MutableStateFlow>, @@ -33,14 +34,18 @@ internal class MatrixTimelineDiffProcessor( suspend fun postItems(items: List) { updateTimelineItems { + Timber.v("Update timeline items from postItems (with ${items.size} items) on ${Thread.currentThread()}") val mappedItems = items.map { it.asMatrixTimelineItem() } addAll(0, mappedItems) } } - suspend fun postDiff(diff: TimelineDiff) { + suspend fun postDiffs(diffs: List) { updateTimelineItems { - applyDiff(diff) + Timber.v("Update timeline items from postDiffs (with ${diffs.size} items) on ${Thread.currentThread()}") + diffs.forEach { diff -> + applyDiff(diff) + } } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt index 9f8bccb1d0..bddd2bc872 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt @@ -35,10 +35,10 @@ import org.matrix.rustcomponents.sdk.TimelineItem import org.matrix.rustcomponents.sdk.TimelineListener import timber.log.Timber -internal fun Room.timelineDiffFlow(onInitialList: suspend (List) -> Unit): Flow = +internal fun Room.timelineDiffFlow(onInitialList: suspend (List) -> Unit): Flow> = callbackFlow { val listener = object : TimelineListener { - override fun onUpdate(diff: TimelineDiff) { + override fun onUpdate(diff: List) { trySendBlocking(diff) } } diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt index 9ec44af998..aa8427608d 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt @@ -32,7 +32,6 @@ import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.Flow @@ -43,7 +42,6 @@ import kotlinx.coroutines.flow.getAndUpdate import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.flow.sample import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.matrix.rustcomponents.sdk.BackPaginationStatus @@ -109,11 +107,11 @@ class RustMatrixTimeline( roomCoroutineScope.launch(dispatcher) { innerRoom.timelineDiffFlow { initialList -> postItems(initialList) - }.onEach { diff -> - if (diff.eventOrigin() == EventItemOrigin.SYNC) { + }.onEach { diffs -> + if (diffs.any { diff -> diff.eventOrigin() == EventItemOrigin.SYNC }) { onNewSyncedEvent() } - postDiff(diff) + postDiffs(diffs) }.launchIn(this) innerRoom.backPaginationStatusFlow() @@ -135,11 +133,10 @@ class RustMatrixTimeline( } } - @OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class) - override val timelineItems: Flow> = _timelineItems.sample(50) - .mapLatest { items -> - encryptedHistoryPostProcessor.process(items) - } + @OptIn(ExperimentalCoroutinesApi::class) + override val timelineItems: Flow> = _timelineItems.mapLatest { items -> + encryptedHistoryPostProcessor.process(items) + } private suspend fun postItems(items: List) = coroutineScope { // Split the initial items in multiple list as there is no pagination in the cached data, so we can post timelineItems asap. @@ -151,9 +148,9 @@ class RustMatrixTimeline( initLatch.complete(Unit) } - private suspend fun postDiff(timelineDiff: TimelineDiff) { + private suspend fun postDiffs(diffs: List) { initLatch.await() - timelineDiffProcessor.postDiff(timelineDiff) + timelineDiffProcessor.postDiffs(diffs) } private fun postPaginationStatus(status: BackPaginationStatus) {