Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add use case to get all ConversationDetailsWithEvents for not paginated list [WPB-12070] #3085

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ import com.wire.kalium.network.api.base.authenticated.conversation.ConversationA
import com.wire.kalium.persistence.dao.QualifiedIDEntity
import com.wire.kalium.persistence.dao.client.ClientDAO
import com.wire.kalium.persistence.dao.conversation.ConversationDAO
import com.wire.kalium.persistence.dao.conversation.ConversationDetailsWithEventsEntity
import com.wire.kalium.persistence.dao.conversation.ConversationEntity
import com.wire.kalium.persistence.dao.conversation.ConversationMetaDataDAO
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.ConversationUnreadEventEntity
import com.wire.kalium.util.DelicateKaliumApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.datetime.Instant

Expand Down Expand Up @@ -127,11 +132,7 @@ interface ConversationRepository {
suspend fun getConversationList(): Either<StorageFailure, Flow<List<Conversation>>>
suspend fun observeConversationList(): Flow<List<Conversation>>
suspend fun observeConversationListDetails(fromArchive: Boolean): Flow<List<ConversationDetails>>
suspend fun observeConversationListDetailsWithEvents(
fromArchive: Boolean = false,
onlyInteractionsEnabled: Boolean = false,
newActivitiesOnTop: Boolean = false,
): Flow<List<ConversationDetailsWithEvents>>
suspend fun observeConversationListDetailsWithEvents(fromArchive: Boolean = false): Flow<List<ConversationDetailsWithEvents>>
suspend fun getConversationIds(
type: Conversation.Type,
protocol: Conversation.Protocol,
Expand Down Expand Up @@ -317,6 +318,7 @@ internal class ConversationDataSource internal constructor(
private val memberDAO: MemberDAO,
private val conversationApi: ConversationApi,
private val messageDAO: MessageDAO,
private val messageDraftDAO: MessageDraftDAO,
private val clientDAO: ClientDAO,
private val clientApi: ClientApi,
private val conversationMetaDataDAO: ConversationMetaDataDAO,
Expand Down Expand Up @@ -520,17 +522,28 @@ internal class ConversationDataSource internal constructor(
conversationViewEntityList.map { conversationViewEntity -> conversationMapper.fromDaoModelToDetails(conversationViewEntity) }
}

override suspend fun observeConversationListDetailsWithEvents(
fromArchive: Boolean,
onlyInteractionsEnabled: Boolean,
newActivitiesOnTop: Boolean,
): Flow<List<ConversationDetailsWithEvents>> =
conversationDAO.getAllConversationDetailsWithEvents(fromArchive, onlyInteractionsEnabled, newActivitiesOnTop)
.map { conversationDetailsWithEventsViewEntityList ->
conversationDetailsWithEventsViewEntityList.map { conversationDetailsWithEventsViewEntity ->
conversationMapper.fromDaoModelToDetailsWithEvents(conversationDetailsWithEventsViewEntity)
}
override suspend fun observeConversationListDetailsWithEvents(fromArchive: Boolean): Flow<List<ConversationDetailsWithEvents>> =
combine(
conversationDAO.getAllConversationDetails(fromArchive),
if (fromArchive) flowOf(listOf()) else messageDAO.observeLastMessages(),
messageDAO.observeConversationsUnreadEvents(),
messageDraftDAO.observeMessageDrafts()
) { conversationList, lastMessageList, unreadEvents, drafts ->
val lastMessageMap = lastMessageList.associateBy { it.conversationId }
val messageDraftMap = drafts.filter { it.text.isNotBlank() }.associateBy { it.conversationId }
val unreadEventsMap = unreadEvents.associateBy { it.conversationId }

conversationList.map { conversation ->
conversationMapper.fromDaoModelToDetailsWithEvents(
ConversationDetailsWithEventsEntity(
conversationViewEntity = conversation,
lastMessage = lastMessageMap[conversation.id],
messageDraft = messageDraftMap[conversation.id],
unreadEvents = unreadEventsMap[conversation.id] ?: ConversationUnreadEventEntity(conversation.id, mapOf()),
)
)
}
}

override suspend fun fetchMlsOneToOneConversation(userId: UserId): Either<CoreFailure, Conversation> =
wrapApiRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ class UserSessionScope internal constructor(
userStorage.database.memberDAO,
authenticatedNetworkContainer.conversationApi,
userStorage.database.messageDAO,
userStorage.database.messageDraftDAO,
userStorage.database.clientDAO,
authenticatedNetworkContainer.clientApi,
userStorage.database.conversationMetaDataDAO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class ConversationScope internal constructor(
val observeConversationListDetails: ObserveConversationListDetailsUseCase
get() = ObserveConversationListDetailsUseCaseImpl(conversationRepository)

val observeConversationListDetailsWithEvents: ObserveConversationListDetailsWithEventsUseCase
get() = ObserveConversationListDetailsWithEventsUseCaseImpl(conversationRepository)

val observeConversationMembers: ObserveConversationMembersUseCase
get() = ObserveConversationMembersUseCaseImpl(conversationRepository, userRepository)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.feature.conversation

import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.conversation.ConversationDetailsWithEvents
import com.wire.kalium.logic.data.conversation.ConversationRepository
import kotlinx.coroutines.flow.Flow

/**
* This use case will observe and return the list of conversation details for the current user.
* @see ConversationDetails
*/
fun interface ObserveConversationListDetailsWithEventsUseCase {
suspend operator fun invoke(fromArchive: Boolean): Flow<List<ConversationDetailsWithEvents>>
}

internal class ObserveConversationListDetailsWithEventsUseCaseImpl(
private val conversationRepository: ConversationRepository,
) : ObserveConversationListDetailsWithEventsUseCase {

override suspend operator fun invoke(fromArchive: Boolean): Flow<List<ConversationDetailsWithEvents>> {
return conversationRepository.observeConversationListDetailsWithEvents(fromArchive)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.wire.kalium.logic.data.conversation

import com.wire.kalium.logic.data.connection.ConnectionStatusMapper
import com.wire.kalium.logic.data.conversation.ConversationRepositoryTest.Companion.MESSAGE_DRAFT_ENTITY
import com.wire.kalium.logic.data.conversation.ConversationRepositoryTest.Companion.MESSAGE_PREVIEW_ENTITY
import com.wire.kalium.logic.data.id.IdMapper
import com.wire.kalium.logic.data.id.TeamId
Expand Down Expand Up @@ -427,6 +426,8 @@ class ConversationMapperTest {
val OTHER_MEMBERS =
listOf(ConversationMemberDTO.Other(service = null, id = UserId("other1", "domain1"), conversationRole = "wire_admin"))
val MEMBERS_RESPONSE = ConversationMembersResponse(SELF_MEMBER_RESPONSE, OTHER_MEMBERS)
val MESSAGE_DRAFT_ENTITY = MessageDraftEntity(TestConversation.VIEW_ENTITY.id, "text", null, null, listOf())

val CONVERSATION_RESPONSE = ConversationResponse(
"creator",
MEMBERS_RESPONSE,
Expand Down
Loading
Loading