From 34aa7f9502c81cfbfaf2426ff63d1005d45ad8d2 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 2 Nov 2022 14:28:04 +0000 Subject: [PATCH] observe unread events for notifications from rooms which are not muted --- .../dapk/st/domain/sync/RoomPersistence.kt | 25 ++++++++++++++----- .../sqldelight/app/dapk/db/model/RoomEvent.sq | 10 ++++++++ .../ObserveUnreadNotificationsUseCaseImpl.kt | 2 +- .../kotlin/app/dapk/st/matrix/sync/Store.kt | 1 + 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt b/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt index f7a2571..0e97c7d 100644 --- a/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt +++ b/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt @@ -8,6 +8,7 @@ import app.dapk.st.domain.room.MutedStorePersistence import app.dapk.st.matrix.common.EventId import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.sync.* +import com.squareup.sqldelight.Query import com.squareup.sqldelight.runtime.coroutines.asFlow import com.squareup.sqldelight.runtime.coroutines.mapToList import com.squareup.sqldelight.runtime.coroutines.mapToOneNotNull @@ -56,10 +57,8 @@ internal class RoomPersistence( }.distinctUntilChanged() return database.roomEventQueries.selectRoom(roomId.value) - .asFlow() - .mapToList() + .distinctFlowList() .map { it.map { json.decodeFromString(RoomEvent.serializer(), it) } } - .distinctUntilChanged() .combine(overviewFlow) { events, overview -> RoomState(overview, events) } @@ -91,9 +90,7 @@ internal class RoomPersistence( override fun observeUnread(): Flow>> { return database.roomEventQueries.selectAllUnread() - .asFlow() - .mapToList() - .distinctUntilChanged() + .distinctFlowList() .map { it.groupBy { RoomId(it.room_id) } .mapKeys { overviewPersistence.retrieve(it.key)!! } @@ -115,6 +112,22 @@ internal class RoomPersistence( } } + override fun observeNotMutedUnread(): Flow>> { + return database.roomEventQueries.selectNotMutedUnread() + .distinctFlowList() + .map { + it.groupBy { RoomId(it.room_id) } + .mapKeys { overviewPersistence.retrieve(it.key)!! } + .mapValues { + it.value.map { + json.decodeFromString(RoomEvent.serializer(), it.blob) + } + } + } + } + + private fun Query.distinctFlowList() = this.asFlow().mapToList().distinctUntilChanged() + override suspend fun markRead(roomId: RoomId) { coroutineDispatchers.withIoContext { database.unreadEventQueries.removeRead(room_id = roomId.value) diff --git a/domains/store/src/main/sqldelight/app/dapk/db/model/RoomEvent.sq b/domains/store/src/main/sqldelight/app/dapk/db/model/RoomEvent.sq index d6067ed..9fb7553 100644 --- a/domains/store/src/main/sqldelight/app/dapk/db/model/RoomEvent.sq +++ b/domains/store/src/main/sqldelight/app/dapk/db/model/RoomEvent.sq @@ -34,6 +34,16 @@ INNER JOIN dbRoomEvent ON dbUnreadEvent.event_id = dbRoomEvent.event_id ORDER BY dbRoomEvent.timestamp_utc DESC LIMIT 100; +selectNotMutedUnread: +SELECT dbRoomEvent.blob, dbRoomEvent.room_id +FROM dbUnreadEvent +INNER JOIN dbRoomEvent ON dbUnreadEvent.event_id = dbRoomEvent.event_id +LEFT OUTER JOIN dbMutedRoom + ON dbUnreadEvent.room_id = dbMutedRoom.room_id + WHERE dbMutedRoom.room_id IS NULL +ORDER BY dbRoomEvent.timestamp_utc DESC +LIMIT 100; + remove: DELETE FROM dbRoomEvent WHERE room_id = ?; diff --git a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/ObserveUnreadNotificationsUseCaseImpl.kt b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/ObserveUnreadNotificationsUseCaseImpl.kt index 83f2981..d1008cc 100644 --- a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/ObserveUnreadNotificationsUseCaseImpl.kt +++ b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/ObserveUnreadNotificationsUseCaseImpl.kt @@ -16,7 +16,7 @@ internal typealias ObserveUnreadNotificationsUseCase = () -> Flow { - return roomStore.observeUnread() + return roomStore.observeNotMutedUnread() .mapWithDiff() .avoidShowingPreviousNotificationsOnLaunch() .onlyRenderableChanges() diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/Store.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/Store.kt index db07b13..d5b644c 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/Store.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/Store.kt @@ -16,6 +16,7 @@ interface RoomStore : MuteableStore { suspend fun markRead(roomId: RoomId) fun observeUnread(): Flow>> fun observeUnreadCountById(): Flow> + fun observeNotMutedUnread(): Flow>> fun observeEvent(eventId: EventId): Flow suspend fun findEvent(eventId: EventId): RoomEvent?