Map api response to a domain object.

This commit is contained in:
Onuray Sahin 2020-09-24 10:14:36 +03:00 committed by Benoit Marty
parent 86b46d1442
commit 8759bcca84
8 changed files with 64 additions and 24 deletions

View File

@ -30,10 +30,10 @@ import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
import org.matrix.android.sdk.api.session.search.SearchResult
import org.matrix.android.sdk.common.CommonTestHelper
import org.matrix.android.sdk.common.CryptoTestHelper
import org.matrix.android.sdk.common.TestConstants
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
@ -83,14 +83,14 @@ class SearchMessagesTest : InstrumentedTest {
orderByRecent = true,
nextBatch = null,
roomId = aliceRoomId,
callback = object : MatrixCallback<SearchResponse> {
override fun onSuccess(data: SearchResponse) {
callback = object : MatrixCallback<SearchResult> {
override fun onSuccess(data: SearchResult) {
super.onSuccess(data)
assertTrue(data.searchCategories.roomEvents?.results?.size == 2)
assertTrue(data.results?.size == 2)
assertTrue(
data.searchCategories.roomEvents?.results
data.results
?.all {
(it.event.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
(it.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
}.orFalse()
)
lock.countDown()
@ -147,14 +147,14 @@ class SearchMessagesTest : InstrumentedTest {
beforeLimit = 10,
orderByRecent = true,
nextBatch = null,
callback = object : MatrixCallback<SearchResponse> {
override fun onSuccess(data: SearchResponse) {
callback = object : MatrixCallback<SearchResult> {
override fun onSuccess(data: SearchResult) {
super.onSuccess(data)
assertTrue(data.searchCategories.roomEvents?.results?.size == 2)
assertTrue(data.results?.size == 2)
assertTrue(
data.searchCategories.roomEvents?.results
data.results
?.all {
(it.event.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
(it.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse()
}.orFalse()
)
lock.countDown()

View File

@ -34,9 +34,9 @@ import org.matrix.android.sdk.api.session.room.tags.TagsService
import org.matrix.android.sdk.api.session.room.timeline.TimelineService
import org.matrix.android.sdk.api.session.room.typing.TypingService
import org.matrix.android.sdk.api.session.room.uploads.UploadsService
import org.matrix.android.sdk.api.session.search.SearchResult
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
/**
* This interface defines methods to interact within a room.
@ -90,5 +90,5 @@ interface Room :
beforeLimit: Int,
afterLimit: Int,
includeProfile: Boolean,
callback: MatrixCallback<SearchResponse>): Cancelable
callback: MatrixCallback<SearchResult>): Cancelable
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2020 New Vector Ltd
* Copyright 2020 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.api.session.search
import org.matrix.android.sdk.api.session.events.model.Event
/**
* Domain class to represent the response of a search request in a room.
*/
data class SearchResult(
// Token that can be used to get the next batch of results, by passing as the next_batch parameter to the next call. If this field is absent, there are no more results.
val nextBatch: String? = null,
// List of words which should be highlighted, useful for stemming which may change the query terms.
var highlights: List<String>? = null,
// List of results in the requested order.
var results: List<Event>? = null
)

View File

@ -19,7 +19,6 @@ package org.matrix.android.sdk.api.session.search
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
/**
* This interface defines methods to search messages in rooms.
@ -45,5 +44,5 @@ interface SearchService {
beforeLimit: Int,
afterLimit: Int,
includeProfile: Boolean,
callback: MatrixCallback<SearchResponse>): Cancelable
callback: MatrixCallback<SearchResult>): Cancelable
}

View File

@ -36,13 +36,13 @@ import org.matrix.android.sdk.api.session.room.tags.TagsService
import org.matrix.android.sdk.api.session.room.timeline.TimelineService
import org.matrix.android.sdk.api.session.room.typing.TypingService
import org.matrix.android.sdk.api.session.room.uploads.UploadsService
import org.matrix.android.sdk.api.session.search.SearchResult
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
import org.matrix.android.sdk.internal.session.room.state.SendStateTask
import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryDataSource
import org.matrix.android.sdk.internal.session.search.SearchTask
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import java.security.InvalidParameterException
@ -135,7 +135,7 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
beforeLimit: Int,
afterLimit: Int,
includeProfile: Boolean,
callback: MatrixCallback<SearchResponse>): Cancelable {
callback: MatrixCallback<SearchResult>): Cancelable {
return searchTask
.configureWith(SearchTask.Params(
searchTerm = searchTerm,

View File

@ -36,12 +36,12 @@ import org.matrix.android.sdk.internal.session.room.membership.threepid.ThreePid
import org.matrix.android.sdk.internal.session.room.relation.RelationsResponse
import org.matrix.android.sdk.internal.session.room.reporting.ReportContentBody
import org.matrix.android.sdk.internal.session.search.request.SearchRequestBody
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
import org.matrix.android.sdk.internal.session.room.send.SendResponse
import org.matrix.android.sdk.internal.session.room.tags.TagBody
import org.matrix.android.sdk.internal.session.room.timeline.EventContextResponse
import org.matrix.android.sdk.internal.session.room.timeline.PaginationResponse
import org.matrix.android.sdk.internal.session.room.typing.TypingBody
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.DELETE

View File

@ -18,9 +18,9 @@
package org.matrix.android.sdk.internal.session.search
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.search.SearchResult
import org.matrix.android.sdk.api.session.search.SearchService
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.session.search.response.SearchResponse
import javax.inject.Inject
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
@ -38,7 +38,7 @@ internal class DefaultSearchService @Inject constructor(
beforeLimit: Int,
afterLimit: Int,
includeProfile: Boolean,
callback: MatrixCallback<SearchResponse>): Cancelable {
callback: MatrixCallback<SearchResult>): Cancelable {
return searchTask
.configureWith(SearchTask.Params(
searchTerm = searchTerm,

View File

@ -18,6 +18,7 @@
package org.matrix.android.sdk.internal.session.search
import org.greenrobot.eventbus.EventBus
import org.matrix.android.sdk.api.session.search.SearchResult
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.session.room.RoomAPI
import org.matrix.android.sdk.internal.session.search.request.SearchRequestBody
@ -30,7 +31,7 @@ import org.matrix.android.sdk.internal.session.search.response.SearchResponse
import org.matrix.android.sdk.internal.task.Task
import javax.inject.Inject
internal interface SearchTask : Task<SearchTask.Params, SearchResponse> {
internal interface SearchTask : Task<SearchTask.Params, SearchResult> {
data class Params(
val searchTerm: String,
@ -49,8 +50,8 @@ internal class DefaultSearchTask @Inject constructor(
private val eventBus: EventBus
) : SearchTask {
override suspend fun execute(params: SearchTask.Params): SearchResponse {
return executeRequest(eventBus) {
override suspend fun execute(params: SearchTask.Params): SearchResult {
return executeRequest<SearchResponse>(eventBus) {
val searchRequestBody = SearchRequestBody(
searchCategories = SearchRequestCategories(
roomEvents = SearchRequestRoomEvents(
@ -69,6 +70,14 @@ internal class DefaultSearchTask @Inject constructor(
)
)
apiCall = roomAPI.search(params.nextBatch, searchRequestBody)
}
}.toDomain().apply { results = results?.reversed() }
}
private fun SearchResponse.toDomain(): SearchResult {
return SearchResult(
nextBatch = searchCategories.roomEvents?.nextBatch,
highlights = searchCategories.roomEvents?.highlights,
results = searchCategories.roomEvents?.results?.map { it.event }
)
}
}