diff --git a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt index 4bbca82..b44cf68 100644 --- a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt +++ b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt @@ -31,8 +31,6 @@ import app.dapk.st.matrix.crypto.cryptoService import app.dapk.st.matrix.crypto.installCryptoService import app.dapk.st.matrix.device.deviceService import app.dapk.st.matrix.device.installEncryptionService -import app.dapk.st.matrix.message.internal.ApiMessage -import app.dapk.st.matrix.http.MatrixHttpClient import app.dapk.st.matrix.http.ktor.KtorMatrixHttpClientFactory import app.dapk.st.matrix.message.MessageEncrypter import app.dapk.st.matrix.message.MessageService @@ -277,26 +275,9 @@ internal class MatrixModules( installMessageService(store.localEchoStore, BackgroundWorkAdapter(workModule.workScheduler()), imageContentReader) { serviceProvider -> MessageEncrypter { message -> val result = serviceProvider.cryptoService().encrypt( - roomId = when (message) { - is MessageService.Message.TextMessage -> message.roomId - is MessageService.Message.ImageMessage -> message.roomId - }, + roomId = message.roomId, credentials = credentialsStore.credentials()!!, - when (message) { - is MessageService.Message.TextMessage -> JsonString( - MatrixHttpClient.jsonWithDefaults.encodeToString( - ApiMessage.TextMessage.serializer(), - ApiMessage.TextMessage( - ApiMessage.TextMessage.TextContent( - message.content.body, - message.content.type, - ), message.roomId, type = EventType.ROOM_MESSAGE.value - ) - ) - ) - - is MessageService.Message.ImageMessage -> TODO() - } + messageJson = message.contents, ) MessageEncrypter.EncryptedMessagePayload( diff --git a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/MessageEncrypter.kt b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/MessageEncrypter.kt index 94300cd..13b67c2 100644 --- a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/MessageEncrypter.kt +++ b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/MessageEncrypter.kt @@ -1,15 +1,12 @@ package app.dapk.st.matrix.message -import app.dapk.st.matrix.common.AlgorithmName -import app.dapk.st.matrix.common.CipherText -import app.dapk.st.matrix.common.DeviceId -import app.dapk.st.matrix.common.SessionId +import app.dapk.st.matrix.common.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable fun interface MessageEncrypter { - suspend fun encrypt(message: MessageService.Message): EncryptedMessagePayload + suspend fun encrypt(message: ClearMessagePayload): EncryptedMessagePayload @Serializable data class EncryptedMessagePayload( @@ -19,8 +16,13 @@ fun interface MessageEncrypter { @SerialName("session_id") val sessionId: SessionId, @SerialName("device_id") val deviceId: DeviceId ) + + data class ClearMessagePayload( + val roomId: RoomId, + val contents: JsonString, + ) } internal object MissingMessageEncrypter : MessageEncrypter { - override suspend fun encrypt(message: MessageService.Message) = throw IllegalStateException("No encrypter instance set") + override suspend fun encrypt(message: MessageEncrypter.ClearMessagePayload) = throw IllegalStateException("No encrypter instance set") } \ No newline at end of file diff --git a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt index 646b716..b0a91a2 100644 --- a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt +++ b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt @@ -2,6 +2,7 @@ package app.dapk.st.matrix.message.internal import app.dapk.st.matrix.common.EventId import app.dapk.st.matrix.common.EventType +import app.dapk.st.matrix.common.JsonString import app.dapk.st.matrix.http.MatrixHttpClient import app.dapk.st.matrix.message.MessageEncrypter import app.dapk.st.matrix.message.MessageService @@ -17,11 +18,25 @@ internal class SendMessageUseCase( is MessageService.Message.TextMessage -> { val request = when (message.sendEncrypted) { true -> { + val content = JsonString( + MatrixHttpClient.jsonWithDefaults.encodeToString( + ApiMessage.TextMessage.serializer(), + ApiMessage.TextMessage( + content = ApiMessage.TextMessage.TextContent( + message.content.body, + message.content.type, + ), + roomId = message.roomId, + type = EventType.ROOM_MESSAGE.value + ) + ) + ) + sendRequest( roomId = message.roomId, eventType = EventType.ENCRYPTED, txId = message.localId, - content = messageEncrypter.encrypt(message), + content = messageEncrypter.encrypt(MessageEncrypter.ClearMessagePayload(message.roomId, content)), ) } diff --git a/test-harness/src/test/kotlin/test/TestMatrix.kt b/test-harness/src/test/kotlin/test/TestMatrix.kt index 55206ac..4e89e7b 100644 --- a/test-harness/src/test/kotlin/test/TestMatrix.kt +++ b/test-harness/src/test/kotlin/test/TestMatrix.kt @@ -16,8 +16,6 @@ import app.dapk.st.matrix.crypto.cryptoService import app.dapk.st.matrix.crypto.installCryptoService import app.dapk.st.matrix.device.deviceService import app.dapk.st.matrix.device.installEncryptionService -import app.dapk.st.matrix.message.internal.ApiMessage -import app.dapk.st.matrix.http.MatrixHttpClient import app.dapk.st.matrix.http.ktor.KtorMatrixHttpClientFactory import app.dapk.st.matrix.message.MessageEncrypter import app.dapk.st.matrix.message.MessageService @@ -35,7 +33,6 @@ import app.dapk.st.olm.DeviceKeyFactory import app.dapk.st.olm.OlmPersistenceWrapper import app.dapk.st.olm.OlmWrapper import kotlinx.coroutines.* -import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import org.amshove.kluent.fail import test.impl.InMemoryDatabase @@ -127,25 +124,9 @@ class TestMatrix( installMessageService(storeModule.localEchoStore, InstantScheduler(it), JavaImageContentReader()) { serviceProvider -> MessageEncrypter { message -> val result = serviceProvider.cryptoService().encrypt( - roomId = when (message) { - is MessageService.Message.TextMessage -> message.roomId - is MessageService.Message.ImageMessage -> message.roomId - }, + roomId = message.roomId, credentials = storeModule.credentialsStore().credentials()!!, - when (message) { - is MessageService.Message.TextMessage -> JsonString( - MatrixHttpClient.jsonWithDefaults.encodeToString( - ApiMessage.TextMessage( - ApiMessage.TextMessage.TextContent( - message.content.body, - message.content.type, - ), message.roomId, type = EventType.ROOM_MESSAGE.value - ) - ) - ) - - is MessageService.Message.ImageMessage -> TODO() - } + messageJson = message.contents, ) MessageEncrypter.EncryptedMessagePayload(