Skip to content

Commit

Permalink
feat: Add pagination in searched messages result screen (WPB-5498) (#…
Browse files Browse the repository at this point in the history
…2277)

* feat: add db query change and repository for paginating result

* feat: return empty list of normal message for now

* feat: add paginated data usecase and its repository extension

* feat: remove unnecessary messageId from pagination as it was needed for count

* feat: remove unused functions for db query

* chore: adjust detekt

* test: remove existing and add new tests

* chore: remove unused usecase, repository and tests
  • Loading branch information
alexandreferris authored and MohamadJaara committed Dec 1, 2023
1 parent ebd7489 commit 93beb20
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ actual interface MessageRepositoryExtensions {
pagingConfig: PagingConfig,
startingOffset: Int
): Flow<PagingData<Message.Standalone>>

suspend fun getPaginatedMessagesSearchBySearchQueryAndConversationId(
searchQuery: String,
conversationId: ConversationId,
pagingConfig: PagingConfig,
startingOffset: Int
): Flow<PagingData<Message.Standalone>>
}

actual class MessageRepositoryExtensionsImpl actual constructor(
Expand All @@ -60,4 +67,22 @@ actual class MessageRepositoryExtensionsImpl actual constructor(
pagingData.map(messageMapper::fromEntityToMessage)
}
}

override suspend fun getPaginatedMessagesSearchBySearchQueryAndConversationId(
searchQuery: String,
conversationId: ConversationId,
pagingConfig: PagingConfig,
startingOffset: Int
): Flow<PagingData<Message.Standalone>> {
val pager: KaliumPager<MessageEntity> = messageDAO.platformExtensions.getPagerForMessagesSearch(
searchQuery = searchQuery,
conversationId = conversationId.toDao(),
pagingConfig = pagingConfig,
startingOffset = startingOffset
)

return pager.pagingDataFlow.map { pagingData: PagingData<MessageEntity> ->
pagingData.map(messageMapper::fromEntityToMessage)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,31 @@
*/
package com.wire.kalium.logic.feature.message

import com.wire.kalium.logic.CoreFailure
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageRepository
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.util.KaliumDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn

/**
* Retrieves a list of messages defined by the search query.
* @param searchQuery The search term used to define which messages will be returned.
* @param conversationId The conversation ID that it will look for messages in.
* @return A [Either<CoreFailure, List<Message.Standalone>>] indicating the success of the operation.
* This use case will observe and return a flow of paginated searched messages for a given conversation.
* @see PagingData
* @see Message
*/
interface GetConversationMessagesFromSearchQueryUseCase {

suspend operator fun invoke(
searchQuery: String,
conversationId: ConversationId
): Either<CoreFailure, List<Message.Standalone>>
}

internal class GetConversationMessagesFromSearchQueryUseCaseImpl internal constructor(
class GetPaginatedFlowOfMessagesBySearchQueryAndConversationIdUseCase internal constructor(
private val dispatcher: KaliumDispatcher,
private val messageRepository: MessageRepository
) : GetConversationMessagesFromSearchQueryUseCase {
) {

override suspend fun invoke(
suspend operator fun invoke(
searchQuery: String,
conversationId: ConversationId
): Either<CoreFailure, List<Message.Standalone>> = messageRepository.getConversationMessagesFromSearch(
searchQuery = searchQuery,
conversationId = conversationId
)
conversationId: ConversationId,
startingOffset: Int,
pagingConfig: PagingConfig
): Flow<PagingData<Message.Standalone>> = messageRepository.extensions.getPaginatedMessagesSearchBySearchQueryAndConversationId(
searchQuery, conversationId, pagingConfig, startingOffset
).flowOn(dispatcher.io)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ package com.wire.kalium.logic.feature.message

val MessageScope.getPaginatedFlowOfMessagesByConversation
get() = GetPaginatedFlowOfMessagesByConversationUseCase(dispatcher, messageRepository)

val MessageScope.getPaginatedFlowOfMessagesBySearchQueryAndConversation
get() = GetPaginatedFlowOfMessagesBySearchQueryAndConversationIdUseCase(dispatcher, messageRepository)
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,6 @@ interface MessageRepository {
targetConversation: ConversationId
): Either<StorageFailure, Unit>

suspend fun getConversationMessagesFromSearch(
searchQuery: String,
conversationId: ConversationId
): Either<CoreFailure, List<Message.Standalone>>

suspend fun getSearchedConversationMessagePosition(
conversationId: ConversationId,
messageId: String
Expand Down Expand Up @@ -645,16 +640,6 @@ class MessageDataSource(
)
}

override suspend fun getConversationMessagesFromSearch(
searchQuery: String,
conversationId: ConversationId
): Either<CoreFailure, List<Message.Standalone>> = wrapStorageRequest {
messageDAO.getConversationMessagesFromSearch(
searchQuery = searchQuery,
conversationId = conversationId.toDao()
).map(messageMapper::fromEntityToMessage)
}

override suspend fun getSearchedConversationMessagePosition(
conversationId: ConversationId,
messageId: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,6 @@ class MessageScope internal constructor(
selfConversationIdProvider = selfConversationIdProvider
)

val getConversationMessagesFromSearchQuery: GetConversationMessagesFromSearchQueryUseCase
get() = GetConversationMessagesFromSearchQueryUseCaseImpl(
messageRepository = messageRepository
)

val getSearchedConversationMessagePosition: GetSearchedConversationMessagePositionUseCase
get() = GetSearchedConversationMessagePositionUseCaseImpl(
messageRepository = messageRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,58 +469,6 @@ class MessageRepositoryTest {
}
}

@Test
fun givenConversationWithMessages_whenSearchingForSpecificMessages_thenReturnOnlyMetCriteriaMessages() = runTest {
// given
val qualifiedIdEntity = TEST_QUALIFIED_ID_ENTITY
val conversationId = TEST_CONVERSATION_ID
val searchTerm = "message 1"

val messageEntity1 = TEST_MESSAGE_ENTITY.copy(
id = "msg1",
conversationId = qualifiedIdEntity,
content = MessageEntityContent.Text("message 10")
)

val messages = listOf(messageEntity1)

val message1 = TEST_MESSAGE.copy(
id = "msg1",
conversationId = conversationId,
content = MessageContent.Text("message 10")
)

val expectedMessages = listOf(message1)

val (_, messageRepository) = Arrangement()
.withMessagesFromSearch(
searchTerm = searchTerm,
conversationId = qualifiedIdEntity,
messages = messages
)
.withMappedMessageModel(
result = message1,
param = messageEntity1
)
.arrange()

// when
val result = messageRepository.getConversationMessagesFromSearch(
searchQuery = searchTerm,
conversationId = conversationId
)

// then
assertEquals(
expectedMessages.size,
(result as Either.Right).value.size
)
assertEquals(
expectedMessages.first().id,
(result as Either.Right).value.first().id
)
}

@Test
fun givenSearchedMessages_whenMessageIsSelected_thenReturnMessagePosition() = runTest {
// given
Expand Down Expand Up @@ -694,17 +642,6 @@ class MessageRepositoryTest {
.thenThrow(throwable)
}

fun withMessagesFromSearch(
searchTerm: String,
conversationId: QualifiedIDEntity,
messages: List<MessageEntity>
) = apply {
given(messageDAO)
.suspendFunction(messageDAO::getConversationMessagesFromSearch)
.whenInvokedWith(eq(searchTerm), eq(conversationId))
.thenReturn(messages)
}

fun withSelectedMessagePosition(
conversationId: QualifiedIDEntity,
messageId: String,
Expand Down

This file was deleted.

Loading

0 comments on commit 93beb20

Please sign in to comment.