diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt index 700507735b..d3a7cf7df0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt @@ -61,6 +61,8 @@ import org.matrix.android.sdk.internal.session.room.relation.FindReactionEventFo import org.matrix.android.sdk.internal.session.room.relation.UpdateQuickReactionTask import org.matrix.android.sdk.internal.session.room.reporting.DefaultReportContentTask import org.matrix.android.sdk.internal.session.room.reporting.ReportContentTask +import org.matrix.android.sdk.internal.session.room.search.DefaultSearchTask +import org.matrix.android.sdk.internal.session.room.search.SearchTask import org.matrix.android.sdk.internal.session.room.state.DefaultSendStateTask import org.matrix.android.sdk.internal.session.room.state.SendStateTask import org.matrix.android.sdk.internal.session.room.tags.AddTagToRoomTask @@ -195,4 +197,7 @@ internal abstract class RoomModule { @Binds abstract fun bindDeleteTagFromRoomTask(task: DefaultDeleteTagFromRoomTask): DeleteTagFromRoomTask + + @Binds + abstract fun bindSearchTask(task: DefaultSearchTask): SearchTask } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/SearchTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/SearchTask.kt new file mode 100644 index 0000000000..db9e1fc604 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/SearchTask.kt @@ -0,0 +1,74 @@ +/* + * 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.internal.session.room.search + +import org.greenrobot.eventbus.EventBus +import org.matrix.android.sdk.internal.network.executeRequest +import org.matrix.android.sdk.internal.session.room.RoomAPI +import org.matrix.android.sdk.internal.session.room.search.request.SearchRequestBody +import org.matrix.android.sdk.internal.session.room.search.request.SearchRequestCategories +import org.matrix.android.sdk.internal.session.room.search.request.SearchRequestEventContext +import org.matrix.android.sdk.internal.session.room.search.request.SearchRequestFilter +import org.matrix.android.sdk.internal.session.room.search.request.SearchRequestOrder +import org.matrix.android.sdk.internal.session.room.search.request.SearchRequestRoomEvents +import org.matrix.android.sdk.internal.session.room.search.response.SearchResponse +import org.matrix.android.sdk.internal.task.Task +import javax.inject.Inject + +internal interface SearchTask : Task { + + data class Params( + val searchTerm: String, + val rooms: List, + val nextBatch: String? = null, + val orderByRecent: Boolean, + val limit: Int, + val beforeLimit: Int, + val afterLimit: Int, + val includeProfile: Boolean + ) +} + +internal class DefaultSearchTask @Inject constructor( + private val roomAPI: RoomAPI, + private val eventBus: EventBus +) : SearchTask { + + override suspend fun execute(params: SearchTask.Params): SearchResponse { + return executeRequest(eventBus) { + val searchRequestBody = SearchRequestBody( + searchCategories = SearchRequestCategories( + roomEvents = SearchRequestRoomEvents( + searchTerm = params.searchTerm, + orderBy = if (params.orderByRecent) SearchRequestOrder.RECENT else SearchRequestOrder.RANK, + filter = SearchRequestFilter( + limit = params.limit, + rooms = params.rooms + ), + eventContext = SearchRequestEventContext( + beforeLimit = params.beforeLimit, + afterLimit = params.afterLimit, + includeProfile = params.includeProfile + ) + ) + ) + ) + apiCall = roomAPI.search(params.nextBatch, searchRequestBody) + } + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/response/SearchResponseRoomEvents.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/response/SearchResponseRoomEvents.kt index 236a93e2c6..a14f78ee00 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/response/SearchResponseRoomEvents.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/search/response/SearchResponseRoomEvents.kt @@ -29,5 +29,8 @@ class SearchResponseRoomEvents( val count: Int? = null, // List of words which should be highlighted, useful for stemming which may change the query terms. @Json(name = "highlights") - val highlights: List? = null + val highlights: List? = null, + // 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. + @Json(name = "next_batch") + val nextBatch: String? = null )