diff --git a/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt b/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt index 1b00d15..14e038b 100644 --- a/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt +++ b/chat-engine/src/testFixtures/kotlin/fixture/Fixtures.kt @@ -63,4 +63,10 @@ fun anImageMeta( fun aRoomState( roomOverview: RoomOverview = aRoomOverview(), events: List = listOf(aRoomMessageEvent()), -) = RoomState(roomOverview, events) \ No newline at end of file +) = RoomState(roomOverview, events) + +fun aRoomInvite( + from: RoomMember = aRoomMember(), + roomId: RoomId = aRoomId(), + inviteMeta: RoomInvite.InviteMeta = RoomInvite.InviteMeta.DirectMessage, +) = RoomInvite(from, roomId, inviteMeta) diff --git a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/InviteUseCase.kt b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/InviteUseCase.kt index d688bdf..e903473 100644 --- a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/InviteUseCase.kt +++ b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/InviteUseCase.kt @@ -2,7 +2,6 @@ package app.dapk.st.engine import app.dapk.st.matrix.sync.SyncService import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.map class InviteUseCase( @@ -14,6 +13,6 @@ class InviteUseCase( private fun invitesDatasource() = combine( syncService.startSyncing(), syncService.invites().map { it.map { it.engine() } } - ) { _, invites -> invites }.filterNotNull() + ) { _, invites -> invites } } \ No newline at end of file diff --git a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixEngine.kt b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixEngine.kt index 6239ad3..4d6977d 100644 --- a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixEngine.kt +++ b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixEngine.kt @@ -3,6 +3,7 @@ package app.dapk.st.engine import app.dapk.st.core.Base64 import app.dapk.st.core.BuildMeta import app.dapk.st.core.CoroutineDispatchers +import app.dapk.st.core.JobBag import app.dapk.st.core.extensions.ErrorTracker import app.dapk.st.matrix.MatrixClient import app.dapk.st.matrix.MatrixTaskRunner @@ -190,7 +191,16 @@ class MatrixEngine internal constructor( } val mediaDecrypter = unsafeLazy { MatrixMediaDecrypter(base64) } - val pushHandler = unsafeLazy { MatrixPushHandler(backgroundScheduler, credentialsStore, lazyMatrix.value.syncService(), roomStore) } + val pushHandler = unsafeLazy { + MatrixPushHandler( + backgroundScheduler, + credentialsStore, + lazyMatrix.value.syncService(), + roomStore, + coroutineDispatchers, + JobBag(), + ) + } val invitesUseCase = unsafeLazy { InviteUseCase(lazyMatrix.value.syncService()) } diff --git a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixPushHandler.kt b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixPushHandler.kt index f9cac91..17e0a47 100644 --- a/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixPushHandler.kt +++ b/matrix-chat-engine/src/main/kotlin/app/dapk/st/engine/MatrixPushHandler.kt @@ -1,6 +1,8 @@ package app.dapk.st.engine import app.dapk.st.core.AppLogTag +import app.dapk.st.core.CoroutineDispatchers +import app.dapk.st.core.JobBag import app.dapk.st.core.log import app.dapk.st.matrix.common.CredentialsStore import app.dapk.st.matrix.common.EventId @@ -9,17 +11,20 @@ import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.message.BackgroundScheduler import app.dapk.st.matrix.sync.RoomStore import app.dapk.st.matrix.sync.SyncService -import kotlinx.coroutines.* -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.launch +import kotlinx.coroutines.withTimeoutOrNull -private var previousJob: Job? = null - -@OptIn(DelicateCoroutinesApi::class) class MatrixPushHandler( private val backgroundScheduler: BackgroundScheduler, private val credentialsStore: CredentialsStore, private val syncService: SyncService, private val roomStore: RoomStore, + private val dispatchers: CoroutineDispatchers, + private val jobBag: JobBag, ) : PushHandler { override fun onNewToken(payload: JsonString) { @@ -35,13 +40,12 @@ class MatrixPushHandler( override fun onMessageReceived(eventId: EventId?, roomId: RoomId?) { log(AppLogTag.PUSH, "push received") - previousJob?.cancel() - previousJob = GlobalScope.launch { + jobBag.replace(MatrixPushHandler::class, dispatchers.global.launch { when (credentialsStore.credentials()) { null -> log(AppLogTag.PUSH, "push ignored due to missing api credentials") else -> doSync(roomId, eventId) } - } + }) } private suspend fun doSync(roomId: RoomId?, eventId: EventId?) { diff --git a/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/InviteUseCaseTest.kt b/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/InviteUseCaseTest.kt new file mode 100644 index 0000000..d534bfd --- /dev/null +++ b/matrix-chat-engine/src/test/kotlin/app/dapk/st/engine/InviteUseCaseTest.kt @@ -0,0 +1,39 @@ +package app.dapk.st.engine + +import app.dapk.st.matrix.common.RoomId +import app.dapk.st.matrix.common.RoomMember +import app.dapk.st.matrix.sync.InviteMeta +import fake.FakeSyncService +import fixture.aRoomId +import fixture.aRoomMember +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test +import app.dapk.st.matrix.sync.RoomInvite as MatrixRoomInvite + +class InviteUseCaseTest { + + private val fakeSyncService = FakeSyncService() + private val useCase = InviteUseCase(fakeSyncService) + + @Test + fun `reads invites from sync service and maps to engine`() = runTest { + val aMatrixRoomInvite = aMatrixRoomInvite() + fakeSyncService.givenStartsSyncing() + fakeSyncService.givenInvites().returns(flowOf(listOf(aMatrixRoomInvite))) + + val result = useCase.invites().first() + + result shouldBeEqualTo listOf(aMatrixRoomInvite.engine()) + } + +} + +fun aMatrixRoomInvite( + from: RoomMember = aRoomMember(), + roomId: RoomId = aRoomId(), + inviteMeta: InviteMeta = InviteMeta.DirectMessage, +) = MatrixRoomInvite(from, roomId, inviteMeta) + diff --git a/matrix/services/sync/src/testFixtures/kotlin/fake/FakeSyncService.kt b/matrix/services/sync/src/testFixtures/kotlin/fake/FakeSyncService.kt index 2c1b0bf..1721c48 100644 --- a/matrix/services/sync/src/testFixtures/kotlin/fake/FakeSyncService.kt +++ b/matrix/services/sync/src/testFixtures/kotlin/fake/FakeSyncService.kt @@ -17,4 +17,6 @@ class FakeSyncService : SyncService by mockk() { fun givenEvents(roomId: RoomId) = every { events(roomId) }.delegateReturn() + fun givenInvites() = every { invites() }.delegateReturn() + }