lifting the message contents jsonising to the message usecase

This commit is contained in:
Adam Brown 2022-09-21 20:07:03 +01:00
parent 94ad1cb595
commit 06947301ac
4 changed files with 28 additions and 49 deletions

View File

@ -31,8 +31,6 @@ import app.dapk.st.matrix.crypto.cryptoService
import app.dapk.st.matrix.crypto.installCryptoService import app.dapk.st.matrix.crypto.installCryptoService
import app.dapk.st.matrix.device.deviceService import app.dapk.st.matrix.device.deviceService
import app.dapk.st.matrix.device.installEncryptionService 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.http.ktor.KtorMatrixHttpClientFactory
import app.dapk.st.matrix.message.MessageEncrypter import app.dapk.st.matrix.message.MessageEncrypter
import app.dapk.st.matrix.message.MessageService import app.dapk.st.matrix.message.MessageService
@ -277,26 +275,9 @@ internal class MatrixModules(
installMessageService(store.localEchoStore, BackgroundWorkAdapter(workModule.workScheduler()), imageContentReader) { serviceProvider -> installMessageService(store.localEchoStore, BackgroundWorkAdapter(workModule.workScheduler()), imageContentReader) { serviceProvider ->
MessageEncrypter { message -> MessageEncrypter { message ->
val result = serviceProvider.cryptoService().encrypt( val result = serviceProvider.cryptoService().encrypt(
roomId = when (message) { roomId = message.roomId,
is MessageService.Message.TextMessage -> message.roomId
is MessageService.Message.ImageMessage -> message.roomId
},
credentials = credentialsStore.credentials()!!, credentials = credentialsStore.credentials()!!,
when (message) { messageJson = message.contents,
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()
}
) )
MessageEncrypter.EncryptedMessagePayload( MessageEncrypter.EncryptedMessagePayload(

View File

@ -1,15 +1,12 @@
package app.dapk.st.matrix.message package app.dapk.st.matrix.message
import app.dapk.st.matrix.common.AlgorithmName import app.dapk.st.matrix.common.*
import app.dapk.st.matrix.common.CipherText
import app.dapk.st.matrix.common.DeviceId
import app.dapk.st.matrix.common.SessionId
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
fun interface MessageEncrypter { fun interface MessageEncrypter {
suspend fun encrypt(message: MessageService.Message): EncryptedMessagePayload suspend fun encrypt(message: ClearMessagePayload): EncryptedMessagePayload
@Serializable @Serializable
data class EncryptedMessagePayload( data class EncryptedMessagePayload(
@ -19,8 +16,13 @@ fun interface MessageEncrypter {
@SerialName("session_id") val sessionId: SessionId, @SerialName("session_id") val sessionId: SessionId,
@SerialName("device_id") val deviceId: DeviceId @SerialName("device_id") val deviceId: DeviceId
) )
data class ClearMessagePayload(
val roomId: RoomId,
val contents: JsonString,
)
} }
internal object MissingMessageEncrypter : MessageEncrypter { 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")
} }

View File

@ -2,6 +2,7 @@ package app.dapk.st.matrix.message.internal
import app.dapk.st.matrix.common.EventId import app.dapk.st.matrix.common.EventId
import app.dapk.st.matrix.common.EventType 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.http.MatrixHttpClient
import app.dapk.st.matrix.message.MessageEncrypter import app.dapk.st.matrix.message.MessageEncrypter
import app.dapk.st.matrix.message.MessageService import app.dapk.st.matrix.message.MessageService
@ -17,11 +18,25 @@ internal class SendMessageUseCase(
is MessageService.Message.TextMessage -> { is MessageService.Message.TextMessage -> {
val request = when (message.sendEncrypted) { val request = when (message.sendEncrypted) {
true -> { 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( sendRequest(
roomId = message.roomId, roomId = message.roomId,
eventType = EventType.ENCRYPTED, eventType = EventType.ENCRYPTED,
txId = message.localId, txId = message.localId,
content = messageEncrypter.encrypt(message), content = messageEncrypter.encrypt(MessageEncrypter.ClearMessagePayload(message.roomId, content)),
) )
} }

View File

@ -16,8 +16,6 @@ import app.dapk.st.matrix.crypto.cryptoService
import app.dapk.st.matrix.crypto.installCryptoService import app.dapk.st.matrix.crypto.installCryptoService
import app.dapk.st.matrix.device.deviceService import app.dapk.st.matrix.device.deviceService
import app.dapk.st.matrix.device.installEncryptionService 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.http.ktor.KtorMatrixHttpClientFactory
import app.dapk.st.matrix.message.MessageEncrypter import app.dapk.st.matrix.message.MessageEncrypter
import app.dapk.st.matrix.message.MessageService 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.OlmPersistenceWrapper
import app.dapk.st.olm.OlmWrapper import app.dapk.st.olm.OlmWrapper
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.amshove.kluent.fail import org.amshove.kluent.fail
import test.impl.InMemoryDatabase import test.impl.InMemoryDatabase
@ -127,25 +124,9 @@ class TestMatrix(
installMessageService(storeModule.localEchoStore, InstantScheduler(it), JavaImageContentReader()) { serviceProvider -> installMessageService(storeModule.localEchoStore, InstantScheduler(it), JavaImageContentReader()) { serviceProvider ->
MessageEncrypter { message -> MessageEncrypter { message ->
val result = serviceProvider.cryptoService().encrypt( val result = serviceProvider.cryptoService().encrypt(
roomId = when (message) { roomId = message.roomId,
is MessageService.Message.TextMessage -> message.roomId
is MessageService.Message.ImageMessage -> message.roomId
},
credentials = storeModule.credentialsStore().credentials()!!, credentials = storeModule.credentialsStore().credentials()!!,
when (message) { messageJson = message.contents,
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()
}
) )
MessageEncrypter.EncryptedMessagePayload( MessageEncrypter.EncryptedMessagePayload(