lifting the message contents jsonising to the message usecase

This commit is contained in:
Adam Brown 2022-09-21 20:07:03 +01:00 committed by Adam Brown
parent 49ae369e38
commit c0d2f1445d
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.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(

View File

@ -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")
}

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.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)),
)
}

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.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(