Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/gradle/ktor-2.3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugods authored Apr 16, 2024
2 parents 40ccfaa + 9c9cc58 commit 4abe58c
Show file tree
Hide file tree
Showing 46 changed files with 402 additions and 91 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sqldelight = "2.0.1"
sqlcipher-android = "4.5.5"
pbandk = "0.14.2"
turbine = "1.0.0"
avs = "9.7.7"
avs = "9.7.9"
jna = "5.14.0"
core-crypto = "1.0.0-rc.54"
core-crypto-multiplatform = "0.6.0-rc.3-multiplatform-pre1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdater
import com.wire.kalium.network.NetworkStateObserver

class CallManagerTest {

Expand Down Expand Up @@ -85,6 +86,9 @@ class CallManagerTest {
@Mock
private val videoStateChecker = mock(classOf<VideoStateChecker>())

@Mock
private val networkStateObserver = mock(classOf<NetworkStateObserver>())

private val dispatcher = TestKaliumDispatcher

private lateinit var callManagerImpl: CallManagerImpl
Expand All @@ -109,6 +113,7 @@ class CallManagerTest {
videoStateChecker = videoStateChecker,
callMapper = callMapper,
conversationClientsInCallUpdater = conversationClientsInCallUpdater,
networkStateObserver = networkStateObserver,
kaliumConfigs = kaliumConfigs
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ actual class CoreLogic(
override fun getSessionScope(userId: UserId): UserSessionScope =
userSessionScopeProvider.value.getOrCreate(userId)

override fun deleteSessionScope(userId: UserId) {
override suspend fun deleteSessionScope(userId: UserId) {
userSessionScopeProvider.value.get(userId)?.cancel()
userSessionScopeProvider.value.delete(userId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ actual class CoreLogic(
override fun getSessionScope(userId: UserId): UserSessionScope =
userSessionScopeProvider.value.getOrCreate(userId)

override fun deleteSessionScope(userId: UserId) {
override suspend fun deleteSessionScope(userId: UserId) {
userSessionScopeProvider.value.get(userId)?.cancel()
userSessionScopeProvider.value.delete(userId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ class CallManagerImpl : CallManager {
override suspend fun setTestRemoteVideoStates(conversationId: ConversationId, participants: List<Participant>) {
TODO("Not yet implemented")
}

override suspend fun cancelJobs() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly")

package com.wire.kalium.logic.feature.call

import com.wire.kalium.logic.cache.SelfConversationIdProvider
Expand All @@ -32,6 +34,7 @@ import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdater
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.network.NetworkStateObserver

actual class GlobalCallManager {
@Suppress("LongParameterList")
Expand All @@ -48,12 +51,13 @@ actual class GlobalCallManager {
qualifiedIdMapper: QualifiedIdMapper,
videoStateChecker: VideoStateChecker,
conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
networkStateObserver: NetworkStateObserver,
kaliumConfigs: KaliumConfigs
): CallManager {
return CallManagerImpl()
}

actual fun removeInMemoryCallingManagerForUser(userId: UserId) {
actual suspend fun removeInMemoryCallingManagerForUser(userId: UserId) {
TODO("Not yet implemented")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly")

package com.wire.kalium.logic.feature.call

import com.sun.jna.Pointer
Expand Down Expand Up @@ -73,6 +75,7 @@ import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.logic.util.toInt
import com.wire.kalium.network.NetworkStateObserver
import com.wire.kalium.util.DateTimeUtil.toEpochMillis
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
Expand All @@ -84,6 +87,7 @@ import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
Expand All @@ -103,13 +107,14 @@ class CallManagerImpl internal constructor(
private val qualifiedIdMapper: QualifiedIdMapper,
private val videoStateChecker: VideoStateChecker,
private val conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
private val networkStateObserver: NetworkStateObserver,
private val kaliumConfigs: KaliumConfigs,
private val json: Json = Json { ignoreUnknownKeys = true },
private val shouldRemoteMuteChecker: ShouldRemoteMuteChecker = ShouldRemoteMuteCheckerImpl(),
kaliumDispatchers: KaliumDispatcher = KaliumDispatcherImpl
) : CallManager {

private val job = SupervisorJob() // TODO(calling): clear job method
private val job = SupervisorJob()
private val scope = CoroutineScope(job + kaliumDispatchers.io)
private val deferredHandle: Deferred<Handle> = startHandleAsync()

Expand Down Expand Up @@ -185,7 +190,7 @@ class CallManagerImpl internal constructor(
.keepingStrongReference(),
establishedCallHandler = OnEstablishedCall(callRepository, scope, qualifiedIdMapper)
.keepingStrongReference(),
closeCallHandler = OnCloseCall(callRepository, scope, qualifiedIdMapper)
closeCallHandler = OnCloseCall(callRepository, scope, qualifiedIdMapper, networkStateObserver)
.keepingStrongReference(),
metricsHandler = metricsHandler,
callConfigRequestHandler = OnConfigRequest(calling, callRepository, scope)
Expand Down Expand Up @@ -615,6 +620,12 @@ class CallManagerImpl internal constructor(
}
}

override suspend fun cancelJobs() {
deferredHandle.cancel()
scope.cancel()
job.cancel()
}

companion object {
private const val DEFAULT_REQUEST_VIDEO_STREAMS_MODE = 0
const val TAG = "CallManager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly")

package com.wire.kalium.logic.feature.call

import com.sun.jna.Pointer
Expand All @@ -40,6 +42,7 @@ import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.util.CurrentPlatform
import com.wire.kalium.logic.util.PlatformContext
import com.wire.kalium.logic.util.PlatformType
import com.wire.kalium.network.NetworkStateObserver
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap

Expand Down Expand Up @@ -84,6 +87,7 @@ actual class GlobalCallManager(
qualifiedIdMapper: QualifiedIdMapper,
videoStateChecker: VideoStateChecker,
conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
networkStateObserver: NetworkStateObserver,
kaliumConfigs: KaliumConfigs
): CallManager {
return callManagerHolder.computeIfAbsent(userId) {
Expand All @@ -100,12 +104,14 @@ actual class GlobalCallManager(
qualifiedIdMapper = qualifiedIdMapper,
videoStateChecker = videoStateChecker,
conversationClientsInCallUpdater = conversationClientsInCallUpdater,
networkStateObserver = networkStateObserver,
kaliumConfigs = kaliumConfigs
)
}
}

actual fun removeInMemoryCallingManagerForUser(userId: UserId) {
actual suspend fun removeInMemoryCallingManagerForUser(userId: UserId) {
callManagerHolder[userId]?.cancelJobs()
callManagerHolder.remove(userId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly")

package com.wire.kalium.logic.feature.call.scenario

import com.sun.jna.Pointer
Expand All @@ -29,14 +31,17 @@ import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.QualifiedIdMapper
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.network.NetworkState
import com.wire.kalium.network.NetworkStateObserver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Suppress("LongParameterList")
class OnCloseCall(
private val callRepository: CallRepository,
private val scope: CoroutineScope,
private val qualifiedIdMapper: QualifiedIdMapper
private val qualifiedIdMapper: QualifiedIdMapper,
private val networkStateObserver: NetworkStateObserver
) : CloseCallHandler {
override fun onClosedCall(
reason: Int,
Expand All @@ -58,7 +63,8 @@ class OnCloseCall(

scope.launch {

if (shouldPersistMissedCall(conversationIdWithDomain, callStatus)) {
val isConnectedToInternet = networkStateObserver.observeNetworkState().value == NetworkState.ConnectedWithInternet
if (shouldPersistMissedCall(conversationIdWithDomain, callStatus) && isConnectedToInternet) {
callRepository.persistMissedCall(conversationIdWithDomain)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class CoreLogicCommon internal constructor(
@Suppress("MemberVisibilityCanBePrivate") // Can be used by other targets like iOS and JS
abstract fun getSessionScope(userId: UserId): UserSessionScope

abstract fun deleteSessionScope(userId: UserId) // TODO remove when proper use case is ready
abstract suspend fun deleteSessionScope(userId: UserId) // TODO remove when proper use case is ready

// TODO: make globalScope a singleton
inline fun <T> globalScope(action: GlobalKaliumScope.() -> T): T = getGlobalScope().action()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import com.wire.kalium.logic.di.MapperProvider
import com.wire.kalium.logic.failure.InvalidMappingFailure
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.flatMapLeft
import com.wire.kalium.logic.functional.isRight
import com.wire.kalium.logic.functional.left
import com.wire.kalium.logic.functional.map
import com.wire.kalium.logic.functional.onFailure
import com.wire.kalium.logic.functional.onSuccess
Expand Down Expand Up @@ -77,6 +79,7 @@ interface ConnectionRepository {
suspend fun setAllConnectionsAsNotified()
suspend fun deleteConnection(connection: Connection): Either<StorageFailure, Unit>
suspend fun getConnection(conversationId: ConversationId): Either<StorageFailure, ConversationDetails.Connection>
suspend fun ignoreConnectionRequest(userId: UserId): Either<CoreFailure, Unit>
}

@Suppress("LongParameterList", "TooManyFunctions")
Expand Down Expand Up @@ -127,22 +130,36 @@ internal class ConnectionDataSource(
}.map { }
}

override suspend fun updateConnectionStatus(userId: UserId, connectionState: ConnectionState): Either<CoreFailure, Connection> {
private suspend fun updateRemoteConnectionStatus(userId: UserId, connectionState: ConnectionState): Either<CoreFailure, ConnectionDTO> {
val isValidConnectionState = isValidConnectionState(connectionState)
val newConnectionStatus = connectionStatusMapper.toApiModel(connectionState)
if (!isValidConnectionState || newConnectionStatus == null) {
return Either.Left(InvalidMappingFailure)
}

return wrapApiRequest {
connectionApi.updateConnection(userId.toApi(), newConnectionStatus)
}.map { connectionDTO ->
val connectionStatus = connectionDTO.copy(status = newConnectionStatus)
return wrapApiRequest { connectionApi.updateConnection(userId.toApi(), newConnectionStatus) }
}

override suspend fun updateConnectionStatus(userId: UserId, connectionState: ConnectionState): Either<CoreFailure, Connection> =
updateRemoteConnectionStatus(userId, connectionState).map { connectionDTO ->
val connectionStatus = connectionDTO.copy(status = connectionStatusMapper.toApiModel(connectionState)!!)
val connectionModel = connectionMapper.fromApiToModel(connectionDTO)
handleUserConnectionStatusPersistence(connectionMapper.fromApiToModel(connectionStatus))
connectionModel
}
}

override suspend fun ignoreConnectionRequest(userId: UserId): Either<CoreFailure, Unit> =
updateRemoteConnectionStatus(userId, IGNORED)
.flatMapLeft {
if (it is NetworkFailure.FederatedBackendFailure.FailedDomains)
wrapStorageRequest { connectionDAO.getConnectionByUser(userId.toDao()) }
.map { connectionEntity ->
val updatedConnection = connectionMapper.fromDaoToModel(connectionEntity).copy(status = IGNORED)
handleUserConnectionStatusPersistence(updatedConnection)
}
else it.left()
}
.map { Unit }

/**
* Check if we can transition to the correct connection status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import com.wire.kalium.persistence.dao.conversation.ConversationMetaDataDAO
import com.wire.kalium.persistence.dao.conversation.EpochChangesDataEntity
import com.wire.kalium.persistence.dao.member.MemberDAO
import com.wire.kalium.persistence.dao.message.MessageDAO
import com.wire.kalium.persistence.dao.message.draft.MessageDraftDAO
import com.wire.kalium.persistence.dao.unread.UnreadEventTypeEntity
import com.wire.kalium.util.DelicateKaliumApi
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -311,6 +312,7 @@ internal class ConversationDataSource internal constructor(
private val clientDAO: ClientDAO,
private val clientApi: ClientApi,
private val conversationMetaDataDAO: ConversationMetaDataDAO,
private val messageDraftDAO: MessageDraftDAO,
private val idMapper: IdMapper = MapperProvider.idMapper(),
private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId),
private val memberMapper: MemberMapper = MapperProvider.memberMapper(),
Expand Down Expand Up @@ -475,12 +477,17 @@ internal class ConversationDataSource internal constructor(
conversationDAO.getAllConversationDetails(fromArchive),
if (fromArchive) flowOf(listOf()) else messageDAO.observeLastMessages(),
messageDAO.observeConversationsUnreadEvents(),
) { conversationList, lastMessageList, unreadEvents ->
messageDraftDAO.observeMessageDrafts()
) { conversationList, lastMessageList, unreadEvents, drafts ->
val lastMessageMap = lastMessageList.associateBy { it.conversationId }
val messageDraftMap = drafts.filter { it.text.isNotBlank() }.associateBy { it.conversationId }

conversationList.map { conversation ->
conversationMapper.fromDaoModelToDetails(conversation,
lastMessageMap[conversation.id]?.let { messageMapper.fromEntityToMessagePreview(it) },
unreadEvents.firstOrNull { it.conversationId == conversation.id }?.unreadEvents?.mapKeys {
conversationMapper.fromDaoModelToDetails(
conversation,
lastMessage = messageDraftMap[conversation.id]?.let { messageMapper.fromDraftToMessagePreview(it) }
?: lastMessageMap[conversation.id]?.let { messageMapper.fromEntityToMessagePreview(it) },
unreadEventCount = unreadEvents.firstOrNull { it.conversationId == conversation.id }?.unreadEvents?.mapKeys {
when (it.key) {
UnreadEventTypeEntity.KNOCK -> UnreadEventType.KNOCK
UnreadEventTypeEntity.MISSED_CALL -> UnreadEventType.MISSED_CALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ data class MessagePreview(
val id: String,
val conversationId: ConversationId,
val content: MessagePreviewContent,
val date: String,
val visibility: Message.Visibility,
val isSelfMessage: Boolean,
val senderUserId: UserId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,5 @@ sealed interface MessagePreviewContent {
data object DegradedProteus : VerificationChanged()
}

data class Draft(val message: String) : MessagePreviewContent
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.wire.kalium.persistence.dao.message.MessageEntityContent
import com.wire.kalium.persistence.dao.message.MessagePreviewEntity
import com.wire.kalium.persistence.dao.message.MessagePreviewEntityContent
import com.wire.kalium.persistence.dao.message.NotificationMessageEntity
import com.wire.kalium.persistence.dao.message.draft.MessageDraftEntity
import com.wire.kalium.util.DateTimeUtil.toIsoDateTimeString
import kotlinx.datetime.Instant
import kotlinx.datetime.toInstant
Expand All @@ -59,6 +60,7 @@ interface MessageMapper {
fun fromEntityToMessage(message: MessageEntity): Message.Standalone
fun fromAssetEntityToAssetMessage(message: AssetMessageEntity): AssetMessage
fun fromEntityToMessagePreview(message: MessagePreviewEntity): MessagePreview
fun fromDraftToMessagePreview(message: MessageDraftEntity): MessagePreview
fun fromMessageToLocalNotificationMessage(message: NotificationMessageEntity): LocalNotificationMessage?
fun toMessageEntityContent(regularMessage: MessageContent.Regular): MessageEntityContent.Regular
}
Expand Down Expand Up @@ -218,13 +220,23 @@ class MessageMapperImpl(
id = message.id,
conversationId = message.conversationId.toModel(),
content = message.content.toMessageContent(),
date = message.date,
visibility = message.visibility.toModel(),
isSelfMessage = message.isSelfMessage,
senderUserId = message.senderUserId.toModel()
)
}

override fun fromDraftToMessagePreview(message: MessageDraftEntity): MessagePreview {
return MessagePreview(
id = message.conversationId.toString(),
conversationId = message.conversationId.toModel(),
content = MessagePreviewContent.Draft(message.text),
visibility = Message.Visibility.VISIBLE,
isSelfMessage = true,
senderUserId = selfUserId
)
}

@Suppress("ComplexMethod", "LongMethod")
override fun fromMessageToLocalNotificationMessage(
message: NotificationMessageEntity
Expand Down
Loading

0 comments on commit 4abe58c

Please sign in to comment.