Skip to content

Commit

Permalink
feat: Add packing and sending DataTransfer message - (WPB-8978) (#2896)
Browse files Browse the repository at this point in the history
* feat: add syncManager to AnalyticsIdentifierManager in order to wait for current device to be available

* feat: add implementation of packDataTransfer message

* test: add tests
  • Loading branch information
alexandreferris authored Jul 25, 2024
1 parent fc1d21f commit 337c415
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.wire.kalium.protobuf.messages.Cleared
import com.wire.kalium.protobuf.messages.ClientAction
import com.wire.kalium.protobuf.messages.Composite
import com.wire.kalium.protobuf.messages.Confirmation
import com.wire.kalium.protobuf.messages.DataTransfer
import com.wire.kalium.protobuf.messages.Ephemeral
import com.wire.kalium.protobuf.messages.External
import com.wire.kalium.protobuf.messages.GenericMessage
Expand All @@ -55,6 +56,7 @@ import com.wire.kalium.protobuf.messages.QualifiedConversationId
import com.wire.kalium.protobuf.messages.Quote
import com.wire.kalium.protobuf.messages.Reaction
import com.wire.kalium.protobuf.messages.Text
import com.wire.kalium.protobuf.messages.TrackingIdentifier
import kotlinx.datetime.Instant
import pbandk.ByteArr

Expand Down Expand Up @@ -139,7 +141,7 @@ class ProtoContentMapperImpl(
is MessageContent.ButtonActionConfirmation -> packButtonActionConfirmation(readableContent)
is MessageContent.Location -> packLocation(readableContent, expectsReadConfirmation, legalHoldStatus)

is MessageContent.DataTransfer -> TODO("Analytics: Not yet implemented")
is MessageContent.DataTransfer -> packDataTransfer(readableContent)
}
}

Expand Down Expand Up @@ -539,6 +541,14 @@ class ProtoContentMapperImpl(
conversationId = protoContent.value.qualifiedConversationId?.let { idMapper.fromProtoModel(it) }
)

private fun packDataTransfer(readableContent: MessageContent.DataTransfer) = GenericMessage.Content.DataTransfer(
DataTransfer(
readableContent.trackingIdentifier?.identifier?.let { identifier ->
TrackingIdentifier(identifier)
}
)
)

private fun unpackDataTransfer(protoContent: GenericMessage.Content.DataTransfer) = MessageContent.DataTransfer(
trackingIdentifier = protoContent.value.trackingIdentifier?.let { trackingIdentifier ->
MessageContent.DataTransfer.TrackingIdentifier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ class UserSessionScope internal constructor(
userId,
clientIdProvider,
selfConversationIdProvider,
syncManager,
userScopedLogger
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.foldToEitherWhileRight
import com.wire.kalium.logic.kaliumLogger
import com.wire.kalium.logic.sync.SyncManager
import kotlinx.datetime.Clock

interface AnalyticsIdentifierManager {
Expand Down Expand Up @@ -57,6 +58,7 @@ internal fun AnalyticsIdentifierManager(
selfUserId: UserId,
selfClientIdProvider: CurrentClientIdProvider,
selfConversationIdProvider: SelfConversationIdProvider,
syncManager: SyncManager,
defaultLogger: KaliumLogger = kaliumLogger
) = object : AnalyticsIdentifierManager {

Expand All @@ -70,6 +72,8 @@ internal fun AnalyticsIdentifierManager(
}

override suspend fun propagateTrackingIdentifier(identifier: String) {
syncManager.waitUntilLive()

val messageContent = MessageContent.DataTransfer(
trackingIdentifier = MessageContent.DataTransfer.TrackingIdentifier(
identifier = identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,26 @@ class ProtoContentMapperTest {
assertEquals(decoded, protoContent)
}

@Test
fun givenDataTransferContent_whenMappingToProtoDataAndBack_thenTheContentsShouldMatchTheOriginal() {
val messageContent = MessageContent.DataTransfer(
MessageContent.DataTransfer.TrackingIdentifier(
"abcd-1234"
)
)
val protoContent = ProtoContent.Readable(
TEST_MESSAGE_UUID,
messageContent,
false,
legalHoldStatus = Conversation.LegalHoldStatus.UNKNOWN
)

val encoded = protoContentMapper.encodeToProtobuf(protoContent)
val decoded = protoContentMapper.decodeFromProtobuf(encoded)

assertEquals(decoded, protoContent)
}

private companion object {
const val TEST_MESSAGE_UUID = "testUuid"
val TEST_CONVERSATION_ID = TestConversation.ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.sync.SyncManager
import com.wire.kalium.logic.util.arrangement.MessageSenderArrangement
import com.wire.kalium.logic.util.arrangement.MessageSenderArrangementImpl
import com.wire.kalium.logic.util.arrangement.SelfConversationIdProviderArrangement
Expand All @@ -31,9 +33,12 @@ import com.wire.kalium.logic.util.arrangement.provider.CurrentClientIdProviderAr
import com.wire.kalium.logic.util.arrangement.provider.CurrentClientIdProviderArrangementImpl
import com.wire.kalium.logic.util.arrangement.repository.UserConfigRepositoryArrangement
import com.wire.kalium.logic.util.arrangement.repository.UserConfigRepositoryArrangementImpl
import io.mockative.Mock
import io.mockative.any
import io.mockative.coEvery
import io.mockative.coVerify
import io.mockative.matches
import io.mockative.mock
import io.mockative.once
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
Expand All @@ -45,6 +50,7 @@ class AnalyticsIdentifierManagerTest {
fun givenAnalyticsMigrationIsComplete_whenDeletingPreviousTrackingIdentifier_thenUserConfigRepositoryIsCalled() = runTest {
// given
val (arrangement, manager) = Arrangement().arrange {
withWaitUntilLiveSuccessful()
withDeletePreviousTrackingIdentifier()
}

Expand All @@ -61,6 +67,7 @@ class AnalyticsIdentifierManagerTest {
fun givenAnIdentifier_whenPropagatingTrackingIdentifier_thenSignalingMessageIsSent() = runTest {
// given
val (arrangement, manager) = Arrangement().arrange {
withWaitUntilLiveSuccessful()
withCurrentClientIdSuccess(SELF_CLIENT_ID)
withSelfConversationIds(listOf(SELF_CONVERSATION_ID))
withSendMessageSucceed()
Expand All @@ -81,6 +88,7 @@ class AnalyticsIdentifierManagerTest {
fun givenAnIdentifierAndNoClientId_whenPropagatingTrackingIdentifier_thenMessageIsNotSent() = runTest {
// given
val (arrangement, manager) = Arrangement().arrange {
withWaitUntilLiveSuccessful()
withCurrentClientIdFailure(StorageFailure.DataNotFound)
}

Expand All @@ -105,14 +113,24 @@ class AnalyticsIdentifierManagerTest {
CurrentClientIdProviderArrangement by CurrentClientIdProviderArrangementImpl(),
SelfConversationIdProviderArrangement by SelfConversationIdProviderArrangementImpl() {

@Mock
val syncManager = mock(SyncManager::class)

private val useCase: AnalyticsIdentifierManager = AnalyticsIdentifierManager(
messageSender = messageSender,
userConfigRepository = userConfigRepository,
selfUserId = SELF_USER_ID,
selfClientIdProvider = currentClientIdProvider,
selfConversationIdProvider = selfConversationIdProvider
selfConversationIdProvider = selfConversationIdProvider,
syncManager = syncManager
)

suspend fun withWaitUntilLiveSuccessful() = apply {
coEvery {
syncManager.waitUntilLiveOrFailure()
}.returns(Either.Right(Unit))
}

fun arrange(block: suspend Arrangement.() -> Unit): Pair<Arrangement, AnalyticsIdentifierManager> {
runBlocking { block() }
return this to useCase
Expand Down

0 comments on commit 337c415

Please sign in to comment.