Skip to content

Commit

Permalink
refactor: create a data module [WPB-8645] (#2875)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugods authored Jul 17, 2024
1 parent 26a248f commit 5881d0d
Show file tree
Hide file tree
Showing 80 changed files with 256 additions and 144 deletions.
49 changes: 49 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
plugins {
id(libs.plugins.android.library.get().pluginId)
id(libs.plugins.kotlin.multiplatform.get().pluginId)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.ksp)
id(libs.plugins.kalium.library.get().pluginId)
}

kaliumLibrary {
multiplatform {
enableJs.set(false)
}
}
kotlin {
sourceSets {
@Suppress("UnusedPrivateProperty")
val commonMain by getting {
dependencies {
implementation(project(":network-model"))
implementation(project(":util"))

implementation(libs.ktor.utils)
implementation(libs.coroutines.core)
implementation(libs.ktxDateTime)
implementation(libs.ktxSerialization)
implementation(libs.ktor.serialization)

implementation(libs.okio.core)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,11 @@

package com.wire.kalium.logic.data.client

import com.wire.kalium.cryptography.PreKeyCrypto
import com.wire.kalium.logic.data.conversation.ClientId
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.time.Duration.Companion.days

data class RegisterClientParam(
val password: String?,
val preKeys: List<PreKeyCrypto>,
val lastKey: PreKeyCrypto,
val deviceType: DeviceType?,
val label: String?,
val capabilities: List<ClientCapability>?,
val clientType: ClientType?,
val model: String?,
val cookieLabel: String?,
val secondFactorVerificationCode: String? = null,
val modelPostfix: String? = null
)

data class DeleteClientParam(
val password: String?,
val clientId: ClientId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.wire.kalium.logic.data.message

import com.wire.kalium.logic.sync.receiver.conversation.message.hasValidRemoteData

data class AssetContent(
val sizeInBytes: Long,
val name: String? = null,
Expand Down Expand Up @@ -101,3 +99,7 @@ data class AssetContent(
}
}
}

fun AssetContent.hasValidRemoteData() = remoteData.hasValidData()

fun AssetContent.RemoteData.hasValidData() = assetId.isNotEmpty() && sha256.isNotEmpty() && otrKey.isNotEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

package com.wire.kalium.logic.data.message

import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.message.ProtoContent.ExternalMessageInstructions
import com.wire.kalium.persistence.dao.conversation.ConversationEntity.ProtocolInfo.Proteus
import com.wire.kalium.protobuf.messages.GenericMessage

/**
* The result of the [protobuf model](https://github.com/wireapp/generic-message-proto) parsing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package com.wire.kalium.logic.data.message.linkpreview

import com.wire.kalium.cryptography.utils.AES256Key
import com.wire.kalium.cryptography.utils.SHA256Key
import com.wire.kalium.logic.data.message.AssetContent.AssetMetadata
import com.wire.kalium.logic.data.message.MessageEncryptionAlgorithm
import okio.Path
Expand All @@ -35,7 +33,7 @@ data class LinkPreviewAsset(
var assetKey: String? = null,
var assetToken: String? = null,
var assetDomain: String? = null,
var otrKey: AES256Key = AES256Key(ByteArray(0)),
var sha256Key: SHA256Key = SHA256Key(ByteArray(0)),
var otrKey: ByteArray = ByteArray(0),
var sha256Key: ByteArray = ByteArray(0),
var encryptionAlgorithm: MessageEncryptionAlgorithm = MessageEncryptionAlgorithm.AES_CBC,
)
1 change: 1 addition & 0 deletions logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(project(":network"))
api(project(":data"))
api(project(":network-util"))
implementation(project(":cryptography"))
implementation(project(":persistence"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,24 @@ class AssetMapperImpl(
mimeType = mimeType,
size = sizeInBytes,
name = name,
metaData = when (metadata) {
is Image -> Asset.Original.MetaData.Image(
Asset.ImageMetaData(
width = metadata.width,
height = metadata.height,
metaData = metadata.let {
when (it) {
is Image -> Asset.Original.MetaData.Image(
Asset.ImageMetaData(
width = it.width,
height = it.height,
)
)
)

is Audio -> Asset.Original.MetaData.Audio(
audio = Asset.AudioMetaData(
durationInMillis = metadata.durationMs,
normalizedLoudness = metadata.normalizedLoudness?.let { ByteArr(it) }
is Audio -> Asset.Original.MetaData.Audio(
audio = Asset.AudioMetaData(
durationInMillis = it.durationMs,
normalizedLoudness = it.normalizedLoudness?.let { ByteArr(it) }
)
)
)

else -> null
else -> null
}
}
),
status = Asset.Status.Uploaded(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.data.client

import com.wire.kalium.cryptography.PreKeyCrypto

data class RegisterClientParam(
val password: String?,
val preKeys: List<PreKeyCrypto>,
val lastKey: PreKeyCrypto,
val deviceType: DeviceType?,
val label: String?,
val capabilities: List<ClientCapability>?,
val clientType: ClientType?,
val model: String?,
val cookieLabel: String?,
val secondFactorVerificationCode: String? = null,
val modelPostfix: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@ class MessageMapperImpl(
senderUserId = message.senderUserId.toDao(),
senderClientId = message.senderClientId.value,
status = message.status.toEntityStatus(),
readCount = if (message.status is Message.Status.Read) message.status.readCount else 0,
editStatus = when (message.editStatus) {
is Message.EditStatus.NotEdited -> MessageEntity.EditStatus.NotEdited
is Message.EditStatus.Edited -> MessageEntity.EditStatus.Edited(message.editStatus.lastEditInstant)
readCount = message.status.let { if (it is Message.Status.Read) it.readCount else 0 },
editStatus = message.editStatus.let {
when (it) {
is Message.EditStatus.NotEdited -> MessageEntity.EditStatus.NotEdited
is Message.EditStatus.Edited -> MessageEntity.EditStatus.Edited(it.lastEditInstant)
}
},
expireAfterMs = message.expirationData?.expireAfter?.inWholeMilliseconds,
selfDeletionEndDate = message.expirationData?.let {
when (it.selfDeletionStatus) {
is Message.ExpirationData.SelfDeletionStatus.Started -> it.selfDeletionStatus.selfDeletionEndDate
selfDeletionEndDate = message.expirationData?.selfDeletionStatus?.let {
when (it) {
is Message.ExpirationData.SelfDeletionStatus.Started -> it.selfDeletionEndDate
is Message.ExpirationData.SelfDeletionStatus.NotStarted -> null
}
},
Expand All @@ -118,10 +120,10 @@ class MessageMapperImpl(
visibility = message.visibility.toEntityVisibility(),
senderName = message.senderUserName,
expireAfterMs = message.expirationData?.expireAfter?.inWholeMilliseconds,
readCount = if (message.status is Message.Status.Read) message.status.readCount else 0,
selfDeletionEndDate = message.expirationData?.let {
when (it.selfDeletionStatus) {
is Message.ExpirationData.SelfDeletionStatus.Started -> it.selfDeletionStatus.selfDeletionEndDate
readCount = message.status.let { if (it is Message.Status.Read) it.readCount else 0 },
selfDeletionEndDate = message.expirationData?.selfDeletionStatus?.let {
when (it) {
is Message.ExpirationData.SelfDeletionStatus.Started -> it.selfDeletionEndDate
is Message.ExpirationData.SelfDeletionStatus.NotStarted -> null
}
}
Expand Down Expand Up @@ -331,6 +333,7 @@ class MessageMapperImpl(
is MessageContent.Text -> toTextEntity(regularMessage)

is MessageContent.Asset -> with(regularMessage.value) {
val metadata = metadata
val assetWidth = when (metadata) {
is Image -> metadata.width
is Video -> metadata.width
Expand Down Expand Up @@ -421,10 +424,13 @@ fun MessageEntityContent.System.toMessageContent(): MessageContent.System = when
MessageEntity.MemberChangeType.CREATION_ADDED -> MessageContent.MemberChange.CreationAdded(memberList)
MessageEntity.MemberChangeType.FAILED_TO_ADD_FEDERATION ->
MessageContent.MemberChange.FailedToAdd(memberList, MessageContent.MemberChange.FailedToAdd.Type.Federation)

MessageEntity.MemberChangeType.FAILED_TO_ADD_LEGAL_HOLD ->
MessageContent.MemberChange.FailedToAdd(memberList, MessageContent.MemberChange.FailedToAdd.Type.LegalHold)

MessageEntity.MemberChangeType.FAILED_TO_ADD_UNKNOWN ->
MessageContent.MemberChange.FailedToAdd(memberList, MessageContent.MemberChange.FailedToAdd.Type.Unknown)

MessageEntity.MemberChangeType.FEDERATION_REMOVED -> MessageContent.MemberChange.FederationRemoved(memberList)
MessageEntity.MemberChangeType.REMOVED_FROM_TEAM -> MessageContent.MemberChange.RemovedFromTeam(memberList)
}
Expand Down Expand Up @@ -669,8 +675,10 @@ fun MessageContent.System.toMessageEntityContent(): MessageEntityContent.System
when (this.type) {
MessageContent.MemberChange.FailedToAdd.Type.Federation ->
MessageEntityContent.MemberChange(memberUserIdList, MessageEntity.MemberChangeType.FAILED_TO_ADD_FEDERATION)

MessageContent.MemberChange.FailedToAdd.Type.LegalHold ->
MessageEntityContent.MemberChange(memberUserIdList, MessageEntity.MemberChangeType.FAILED_TO_ADD_LEGAL_HOLD)

MessageContent.MemberChange.FailedToAdd.Type.Unknown ->
MessageEntityContent.MemberChange(memberUserIdList, MessageEntity.MemberChangeType.FAILED_TO_ADD_UNKNOWN)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ class ProtoContentMapperImpl(

@Suppress("ComplexMethod")
private fun mapReadableContentToProtobuf(protoContent: ProtoContent.Readable): GenericMessage.Content<out Any> {
return if (protoContent.expiresAfterMillis != null) {
val expiration = protoContent.expiresAfterMillis
return if (expiration != null) {
mapEphemeralContent(
protoContent.messageContent,
protoContent.expiresAfterMillis,
expiration,
protoContent.expectsReadConfirmation,
protoContent.legalHoldStatus
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,26 @@ class LinkPreviewMapperImpl(
permanentUrl = linkPreview.permanentUrl,
title = linkPreview.title,
summary = linkPreview.summary,
image = linkPreview.image?.let {
image = linkPreview.image?.let { image ->
Asset(
original = Asset.Original(
mimeType = linkPreview.image.mimeType,
size = linkPreview.image.assetDataSize,
mimeType = image.mimeType,
size = image.assetDataSize,
metaData = Asset.Original.MetaData.Image(
image = Asset.ImageMetaData(
height = linkPreview.image.assetHeight,
width = linkPreview.image.assetWidth
height = image.assetHeight,
width = image.assetWidth
)
)
),
status = Asset.Status.Uploaded(
uploaded = Asset.RemoteData(
otrKey = ByteArr(linkPreview.image.otrKey.data),
sha256 = ByteArr(linkPreview.image.sha256Key.data),
assetId = linkPreview.image.assetKey,
assetToken = linkPreview.image.assetToken,
assetDomain = linkPreview.image.assetDomain,
encryption = encryptionAlgorithmMapper.toProtoBufModel(linkPreview.image.encryptionAlgorithm)
otrKey = ByteArr(image.otrKey),
sha256 = ByteArr(image.sha256Key),
assetId = image.assetKey,
assetToken = image.assetToken,
assetDomain = image.assetDomain,
encryption = encryptionAlgorithmMapper.toProtoBufModel(image.encryptionAlgorithm)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ internal class RecoverMLSConversationsUseCaseImpl(
}

private suspend fun recoverMLSGroup(conversation: Conversation): Either<CoreFailure, Unit> {
return if (conversation.protocol is Conversation.ProtocolInfo.MLS) {
mlsConversationRepository.isGroupOutOfSync(conversation.protocol.groupId, conversation.protocol.epoch)
val protocol = conversation.protocol
return if (protocol is Conversation.ProtocolInfo.MLS) {
mlsConversationRepository.isGroupOutOfSync(protocol.groupId, protocol.epoch)
.fold({ checkEpochFailure ->
Either.Left(checkEpochFailure)
}, { isGroupOutOfSync ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ internal class FetchConversationMLSVerificationStatusUseCaseImpl(

override suspend fun invoke(conversationId: ConversationId) {
conversationRepository.detailsById(conversationId).onSuccess {
if (it.protocol is Conversation.ProtocolInfo.MLSCapable)
fetchMLSVerificationStatusUseCase(it.protocol.groupId)
val protocol = it.protocol
if (protocol is Conversation.ProtocolInfo.MLSCapable)
fetchMLSVerificationStatusUseCase(protocol.groupId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ internal class MessageSendingInterceptorImpl internal constructor(

val replyMessageContent = originalMessage.content

val quotedReference = (replyMessageContent as? MessageContent.Text)?.quotedMessageReference
if (replyMessageContent !is MessageContent.Text
|| originalMessage !is Message.Regular
|| replyMessageContent.quotedMessageReference == null
|| quotedReference == null
) {
return Either.Right(originalMessage)
}

return messageRepository.getMessageById(originalMessage.conversationId, replyMessageContent.quotedMessageReference.quotedMessageId)
return messageRepository.getMessageById(originalMessage.conversationId, quotedReference.quotedMessageId)
.map { persistedMessage ->
val encodedMessageContent = messageContentEncoder.encodeMessageContent(
messageInstant = persistedMessage.date,
Expand All @@ -55,7 +56,7 @@ internal class MessageSendingInterceptorImpl internal constructor(

originalMessage.copy(
content = replyMessageContent.copy(
quotedMessageReference = replyMessageContent.quotedMessageReference.copy(
quotedMessageReference = quotedReference.copy(
quotedMessageSha256 = encodedMessageContent?.sha256Digest
)
)
Expand Down
Loading

0 comments on commit 5881d0d

Please sign in to comment.