Skip to content

Commit b5b120d

Browse files
ganfraganfra
and
ganfra
authored
Update rust sdk to 0.1.39 (#1024)
Co-authored-by: ganfra <[email protected]>
1 parent a68564b commit b5b120d

File tree

8 files changed

+39
-41
lines changed

8 files changed

+39
-41
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup" }
145145
appyx_core = { module = "com.bumble.appyx:core", version.ref = "appyx" }
146146
molecule-runtime = { module = "app.cash.molecule:molecule-runtime", version.ref = "molecule" }
147147
timber = "com.jakewharton.timber:timber:5.0.1"
148-
matrix_sdk = "org.matrix.rustcomponents:sdk-android:0.1.38"
148+
matrix_sdk = "org.matrix.rustcomponents:sdk-android:0.1.39"
149149
sqldelight-driver-android = { module = "com.squareup.sqldelight:android-driver", version.ref = "sqldelight" }
150150
sqldelight-driver-jvm = { module = "com.squareup.sqldelight:sqlite-driver", version.ref = "sqldelight" }
151151
sqldelight-coroutines = { module = "com.squareup.sqldelight:coroutines-extensions", version.ref = "sqldelight" }

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/NotificationMapper.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import io.element.android.libraries.core.bool.orFalse
2020
import io.element.android.libraries.matrix.api.core.EventId
2121
import io.element.android.libraries.matrix.api.core.RoomId
2222
import io.element.android.libraries.matrix.api.core.SessionId
23-
import io.element.android.libraries.matrix.api.core.UserId
2423
import io.element.android.libraries.matrix.api.notification.NotificationContent
2524
import io.element.android.libraries.matrix.api.notification.NotificationData
2625
import io.element.android.libraries.matrix.api.room.RoomMembershipState
@@ -30,7 +29,7 @@ import org.matrix.rustcomponents.sdk.NotificationItem
3029
import org.matrix.rustcomponents.sdk.use
3130

3231
class NotificationMapper(
33-
private val sessionId: SessionId,
32+
sessionId: SessionId,
3433
private val clock: SystemClock,
3534
) {
3635
private val notificationContentMapper = NotificationContentMapper(sessionId)
@@ -40,7 +39,6 @@ class NotificationMapper(
4039
roomId: RoomId,
4140
notificationItem: NotificationItem
4241
): NotificationData {
43-
val senderId = UserId(notificationItem.senderInfo.senderId)
4442
return notificationItem.use { item ->
4543
NotificationData(
4644
eventId = eventId,
@@ -52,31 +50,27 @@ class NotificationMapper(
5250
isDirect = item.roomInfo.isDirect,
5351
isEncrypted = item.roomInfo.isEncrypted.orFalse(),
5452
isNoisy = item.isNoisy.orFalse(),
55-
timestamp = item.timestamp(clock),
56-
content = item.event.use { notificationContentMapper.map(senderId, it) },
53+
timestamp = item.timestamp() ?: clock.epochMillis(),
54+
content = item.event.use { notificationContentMapper.map(it) },
5755
contentUrl = null,
5856
)
5957
}
6058
}
6159
}
6260

63-
class NotificationContentMapper(
64-
private val sessionId: SessionId,
65-
) {
61+
class NotificationContentMapper(private val sessionId: SessionId) {
6662
private val timelineEventToNotificationContentMapper = TimelineEventToNotificationContentMapper()
6763

68-
fun map(senderId: UserId, notificationEvent: NotificationEvent): NotificationContent =
64+
fun map(notificationEvent: NotificationEvent): NotificationContent =
6965
when (notificationEvent) {
70-
is NotificationEvent.Timeline -> timelineEventToNotificationContentMapper.map(senderId, notificationEvent.event)
66+
is NotificationEvent.Timeline -> timelineEventToNotificationContentMapper.map(notificationEvent.event)
7167
is NotificationEvent.Invite -> NotificationContent.StateEvent.RoomMemberContent(
7268
userId = sessionId.value,
7369
membershipState = RoomMembershipState.INVITE,
7470
)
7571
}
7672
}
7773

78-
private fun NotificationItem.timestamp(clock: SystemClock): Long {
79-
// FIXME we can't get the timestamp from the notification item anymore, so we need to fake it
80-
// return (this.event as? NotificationEvent.Timeline)?.event?.timestamp()?.toLong()
81-
return clock.epochMillis()
74+
private fun NotificationItem.timestamp(): Long? {
75+
return (this.event as? NotificationEvent.Timeline)?.event?.timestamp()?.toLong()
8276
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/TimelineEventToNotificationContentMapper.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ import io.element.android.libraries.matrix.impl.room.RoomMemberMapper
2222
import io.element.android.libraries.matrix.impl.timeline.item.event.EventMessageMapper
2323
import org.matrix.rustcomponents.sdk.MessageLikeEventContent
2424
import org.matrix.rustcomponents.sdk.StateEventContent
25+
import org.matrix.rustcomponents.sdk.TimelineEvent
2526
import org.matrix.rustcomponents.sdk.TimelineEventType
2627
import org.matrix.rustcomponents.sdk.use
2728
import javax.inject.Inject
2829

2930
class TimelineEventToNotificationContentMapper @Inject constructor() {
3031

31-
fun map(senderId: UserId, timelineEventType: TimelineEventType): NotificationContent {
32-
return timelineEventType.use {
33-
it.toContent(senderId = senderId)
32+
fun map(timelineEvent: TimelineEvent): NotificationContent {
33+
return timelineEvent.use {
34+
timelineEvent.eventType().use { eventType ->
35+
eventType.toContent(senderId = UserId(timelineEvent.senderId()))
36+
}
3437
}
3538
}
3639
}
@@ -72,7 +75,7 @@ private fun StateEventContent.toContent(): NotificationContent.StateEvent {
7275

7376
private fun MessageLikeEventContent.toContent(senderId: UserId): NotificationContent.MessageLike {
7477
return use {
75-
when (it) {
78+
when (this) {
7679
MessageLikeEventContent.CallAnswer -> NotificationContent.MessageLike.CallAnswer
7780
MessageLikeEventContent.CallCandidates -> NotificationContent.MessageLike.CallCandidates
7881
MessageLikeEventContent.CallHangup -> NotificationContent.MessageLike.CallHangup
@@ -84,10 +87,10 @@ private fun MessageLikeEventContent.toContent(senderId: UserId): NotificationCon
8487
MessageLikeEventContent.KeyVerificationMac -> NotificationContent.MessageLike.KeyVerificationMac
8588
MessageLikeEventContent.KeyVerificationReady -> NotificationContent.MessageLike.KeyVerificationReady
8689
MessageLikeEventContent.KeyVerificationStart -> NotificationContent.MessageLike.KeyVerificationStart
87-
is MessageLikeEventContent.ReactionContent -> NotificationContent.MessageLike.ReactionContent(it.relatedEventId)
90+
is MessageLikeEventContent.ReactionContent -> NotificationContent.MessageLike.ReactionContent(relatedEventId)
8891
MessageLikeEventContent.RoomEncrypted -> NotificationContent.MessageLike.RoomEncrypted
8992
is MessageLikeEventContent.RoomMessage -> {
90-
NotificationContent.MessageLike.RoomMessage(senderId, EventMessageMapper().mapMessageType(it.messageType))
93+
NotificationContent.MessageLike.RoomMessage(senderId, EventMessageMapper().mapMessageType(messageType))
9194
}
9295
MessageLikeEventContent.RoomRedaction -> NotificationContent.MessageLike.RoomRedaction
9396
MessageLikeEventContent.Sticker -> NotificationContent.MessageLike.Sticker

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RoomContentForwarder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ class RoomContentForwarder(
8080
}
8181

8282
private object NoOpTimelineListener : TimelineListener {
83-
override fun onUpdate(diff: TimelineDiff) = Unit
83+
override fun onUpdate(diff: List<TimelineDiff>) = Unit
8484
}
8585
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,13 @@ class RustMatrixRoom(
372372
}
373373

374374
//TODO handle cancellation, need refactoring of how we are catching errors
375-
private suspend fun sendAttachment(handle: () -> SendAttachmentJoinHandle): Result<Unit> = withContext(roomDispatcher) {
376-
runCatching {
375+
private suspend fun sendAttachment(handle: () -> SendAttachmentJoinHandle): Result<Unit> {
376+
return runCatching {
377377
handle().use {
378378
it.join()
379379
}
380380
}
381381
}
382-
383382
}
384383

385384

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import kotlinx.coroutines.sync.withLock
2323
import org.matrix.rustcomponents.sdk.TimelineChange
2424
import org.matrix.rustcomponents.sdk.TimelineDiff
2525
import org.matrix.rustcomponents.sdk.TimelineItem
26+
import timber.log.Timber
2627

2728
internal class MatrixTimelineDiffProcessor(
2829
private val timelineItems: MutableStateFlow<List<MatrixTimelineItem>>,
@@ -33,14 +34,18 @@ internal class MatrixTimelineDiffProcessor(
3334

3435
suspend fun postItems(items: List<TimelineItem>) {
3536
updateTimelineItems {
37+
Timber.v("Update timeline items from postItems (with ${items.size} items) on ${Thread.currentThread()}")
3638
val mappedItems = items.map { it.asMatrixTimelineItem() }
3739
addAll(0, mappedItems)
3840
}
3941
}
4042

41-
suspend fun postDiff(diff: TimelineDiff) {
43+
suspend fun postDiffs(diffs: List<TimelineDiff>) {
4244
updateTimelineItems {
43-
applyDiff(diff)
45+
Timber.v("Update timeline items from postDiffs (with ${diffs.size} items) on ${Thread.currentThread()}")
46+
diffs.forEach { diff ->
47+
applyDiff(diff)
48+
}
4449
}
4550
}
4651

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RoomTimelineExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ import org.matrix.rustcomponents.sdk.TimelineItem
3535
import org.matrix.rustcomponents.sdk.TimelineListener
3636
import timber.log.Timber
3737

38-
internal fun Room.timelineDiffFlow(onInitialList: suspend (List<TimelineItem>) -> Unit): Flow<TimelineDiff> =
38+
internal fun Room.timelineDiffFlow(onInitialList: suspend (List<TimelineItem>) -> Unit): Flow<List<TimelineDiff>> =
3939
callbackFlow {
4040
val listener = object : TimelineListener {
41-
override fun onUpdate(diff: TimelineDiff) {
41+
override fun onUpdate(diff: List<TimelineDiff>) {
4242
trySendBlocking(diff)
4343
}
4444
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import kotlinx.coroutines.CompletableDeferred
3232
import kotlinx.coroutines.CoroutineDispatcher
3333
import kotlinx.coroutines.CoroutineScope
3434
import kotlinx.coroutines.ExperimentalCoroutinesApi
35-
import kotlinx.coroutines.FlowPreview
3635
import kotlinx.coroutines.coroutineScope
3736
import kotlinx.coroutines.ensureActive
3837
import kotlinx.coroutines.flow.Flow
@@ -43,7 +42,6 @@ import kotlinx.coroutines.flow.getAndUpdate
4342
import kotlinx.coroutines.flow.launchIn
4443
import kotlinx.coroutines.flow.mapLatest
4544
import kotlinx.coroutines.flow.onEach
46-
import kotlinx.coroutines.flow.sample
4745
import kotlinx.coroutines.launch
4846
import kotlinx.coroutines.withContext
4947
import org.matrix.rustcomponents.sdk.BackPaginationStatus
@@ -109,11 +107,11 @@ class RustMatrixTimeline(
109107
roomCoroutineScope.launch(dispatcher) {
110108
innerRoom.timelineDiffFlow { initialList ->
111109
postItems(initialList)
112-
}.onEach { diff ->
113-
if (diff.eventOrigin() == EventItemOrigin.SYNC) {
110+
}.onEach { diffs ->
111+
if (diffs.any { diff -> diff.eventOrigin() == EventItemOrigin.SYNC }) {
114112
onNewSyncedEvent()
115113
}
116-
postDiff(diff)
114+
postDiffs(diffs)
117115
}.launchIn(this)
118116

119117
innerRoom.backPaginationStatusFlow()
@@ -135,11 +133,10 @@ class RustMatrixTimeline(
135133
}
136134
}
137135

138-
@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
139-
override val timelineItems: Flow<List<MatrixTimelineItem>> = _timelineItems.sample(50)
140-
.mapLatest { items ->
141-
encryptedHistoryPostProcessor.process(items)
142-
}
136+
@OptIn(ExperimentalCoroutinesApi::class)
137+
override val timelineItems: Flow<List<MatrixTimelineItem>> = _timelineItems.mapLatest { items ->
138+
encryptedHistoryPostProcessor.process(items)
139+
}
143140

144141
private suspend fun postItems(items: List<TimelineItem>) = coroutineScope {
145142
// 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(
151148
initLatch.complete(Unit)
152149
}
153150

154-
private suspend fun postDiff(timelineDiff: TimelineDiff) {
151+
private suspend fun postDiffs(diffs: List<TimelineDiff>) {
155152
initLatch.await()
156-
timelineDiffProcessor.postDiff(timelineDiff)
153+
timelineDiffProcessor.postDiffs(diffs)
157154
}
158155

159156
private fun postPaginationStatus(status: BackPaginationStatus) {

0 commit comments

Comments
 (0)