From 1f3c62e9c358448821eb664e7b08a2c8d68117cb Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 22 Oct 2022 20:06:31 +0100 Subject: [PATCH] allow state events to be missing/null - dendrite can omit the state field from the sync response --- .../dapk/st/matrix/sync/internal/request/ApiSyncResponse.kt | 2 +- .../st/matrix/sync/internal/sync/RoomOverviewProcessor.kt | 2 +- .../app/dapk/st/matrix/sync/internal/sync/RoomProcessor.kt | 2 +- .../app/dapk/st/matrix/sync/internal/sync/SyncReducer.kt | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiSyncResponse.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiSyncResponse.kt index 39b8a90..b4f417c 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiSyncResponse.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiSyncResponse.kt @@ -53,7 +53,7 @@ internal data class ApiInviteEvents( @Serializable internal data class ApiSyncRoom( @SerialName("timeline") val timeline: ApiSyncRoomTimeline, - @SerialName("state") val state: ApiSyncRoomState, + @SerialName("state") val state: ApiSyncRoomState? = null, @SerialName("account_data") val accountData: ApiAccountData? = null, @SerialName("ephemeral") val ephemeral: ApiEphemeral? = null, @SerialName("summary") val summary: ApiRoomSummary? = null, diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomOverviewProcessor.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomOverviewProcessor.kt index f4a9149..0218d81 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomOverviewProcessor.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomOverviewProcessor.kt @@ -13,7 +13,7 @@ internal class RoomOverviewProcessor( ) { suspend fun process(roomToProcess: RoomToProcess, previousState: RoomOverview?, lastMessage: LastMessage?): RoomOverview? { - val combinedEvents = roomToProcess.apiSyncRoom.state.stateEvents + roomToProcess.apiSyncRoom.timeline.apiTimelineEvents + val combinedEvents = (roomToProcess.apiSyncRoom.state?.stateEvents.orEmpty()) + roomToProcess.apiSyncRoom.timeline.apiTimelineEvents val isEncrypted = combinedEvents.any { it is ApiTimelineEvent.Encryption } val readMarker = roomToProcess.apiSyncRoom.accountData?.events?.filterIsInstance()?.firstOrNull()?.content?.eventId return when (previousState) { diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomProcessor.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomProcessor.kt index eca9c77..9691686 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomProcessor.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomProcessor.kt @@ -50,7 +50,7 @@ internal class RoomProcessor( } private fun ApiSyncRoom.collectMembers(userCredentials: UserCredentials): List { - return (this.state.stateEvents + this.timeline.apiTimelineEvents) + return (this.state?.stateEvents.orEmpty() + this.timeline.apiTimelineEvents) .filterIsInstance() .mapNotNull { when { diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/SyncReducer.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/SyncReducer.kt index 9bba338..a606895 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/SyncReducer.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/SyncReducer.kt @@ -70,7 +70,7 @@ internal class SyncReducer( } private fun findRoomsLeft(response: ApiSyncResponse, userCredentials: UserCredentials) = response.rooms?.leave?.filter { - it.value.state.stateEvents.filterIsInstance().any { + it.value.state?.stateEvents.orEmpty().filterIsInstance().any { it.content.membership.isLeave() && it.senderId == userCredentials.userId } }?.map { it.key } ?: emptyList() @@ -91,7 +91,7 @@ internal class SyncReducer( } private fun Map.keepRoomsWithChanges() = this.filter { - it.value.state.stateEvents.isNotEmpty() || + it.value.state?.stateEvents.orEmpty().isNotEmpty() || it.value.timeline.apiTimelineEvents.isNotEmpty() || it.value.accountData?.events?.isNotEmpty() == true || it.value.ephemeral?.events?.isNotEmpty() == true