diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/search/SearchMessagesTest.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/search/SearchMessagesTest.kt index 3e2960af43..b7d253ca00 100644 --- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/search/SearchMessagesTest.kt +++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/session/search/SearchMessagesTest.kt @@ -47,7 +47,7 @@ class SearchMessagesTest : InstrumentedTest { private val cryptoTestHelper = CryptoTestHelper(commonTestHelper) @Test - fun sendTextMessageAndSearchPartOfIt() { + fun sendTextMessageAndSearchPartOfItUsingSession() { val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom(false) val aliceSession = cryptoTestData.firstSession val aliceRoomId = cryptoTestData.roomId @@ -110,5 +110,69 @@ class SearchMessagesTest : InstrumentedTest { } aliceSession.startSync(true) -} + } + + @Test + fun sendTextMessageAndSearchPartOfItUsingRoom() { + val cryptoTestData = cryptoTestHelper.doE2ETestWithAliceAndBobInARoom(false) + val aliceSession = cryptoTestData.firstSession + val aliceRoomId = cryptoTestData.roomId + aliceSession.cryptoService().setWarnOnUnknownDevices(false) + val roomFromAlicePOV = aliceSession.getRoom(aliceRoomId)!! + val aliceTimeline = roomFromAlicePOV.createTimeline(null, TimelineSettings(10)) + aliceTimeline.start() + + commonTestHelper.sendTextMessage( + roomFromAlicePOV, + MESSAGE, + 2) + + run { + var lock = CountDownLatch(1) + + val eventListener = commonTestHelper.createEventListener(lock) { snapshot -> + snapshot.count { it.root.content.toModel()?.body?.startsWith(MESSAGE).orFalse() } == 2 + } + + aliceTimeline.addListener(eventListener) + commonTestHelper.await(lock) + + lock = CountDownLatch(1) + roomFromAlicePOV + .search( + searchTerm = "lore", + limit = 10, + includeProfile = true, + afterLimit = 0, + beforeLimit = 10, + orderByRecent = true, + nextBatch = null, + callback = object : MatrixCallback { + override fun onSuccess(data: SearchResponse) { + super.onSuccess(data) + assertTrue(data.searchCategories.roomEvents?.results?.size == 2) + assertTrue( + data.searchCategories.roomEvents?.results + ?.all { + (it.event.content?.get("body") as? String)?.startsWith(MESSAGE).orFalse() + }.orFalse() + ) + lock.countDown() + } + + override fun onFailure(failure: Throwable) { + super.onFailure(failure) + fail(failure.localizedMessage) + lock.countDown() + } + } + ) + lock.await(TestConstants.timeOutMillis, TimeUnit.MILLISECONDS) + + aliceTimeline.removeAllListeners() + cryptoTestData.cleanUp(commonTestHelper) + } + + aliceSession.startSync(true) + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt index 3d8550adf0..d09a42753b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/Room.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.api.session.room import androidx.lifecycle.LiveData +import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.room.call.RoomCallService import org.matrix.android.sdk.api.session.room.crypto.RoomCryptoService import org.matrix.android.sdk.api.session.room.members.MembershipService @@ -33,7 +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.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. @@ -69,4 +72,23 @@ interface Room : * A current snapshot of [RoomSummary] associated with the room */ fun roomSummary(): RoomSummary? + + /** + * Generic function to search a term in a room. + * Ref: https://matrix.org/docs/spec/client_server/latest#module-search + * @param searchTerm the term to search + * @param nextBatch the token that retrieved from the previous response. Should be provided to get the next batch of results + * @param limit the maximum number of events to return. + * @param beforeLimit how many events before the result are returned. + * @param afterLimit how many events after the result are returned. + * @param includeProfile requests that the server returns the historic profile information for the users that sent the events that were returned. + */ + fun search(searchTerm: String, + nextBatch: String?, + orderByRecent: Boolean, + limit: Int, + beforeLimit: Int, + afterLimit: Int, + includeProfile: Boolean, + callback: MatrixCallback): Cancelable } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/search/SearchService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/search/SearchService.kt index 68fc4e5d6f..2087af6a81 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/search/SearchService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/search/SearchService.kt @@ -27,7 +27,7 @@ import org.matrix.android.sdk.internal.session.search.response.SearchResponse interface SearchService { /** - * Generic function to search a term in multiple rooms. + * Generic function to search a term in a room. * Ref: https://matrix.org/docs/spec/client_server/latest#module-search * @param searchTerm the term to search * @param roomId the roomId to search term inside diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt index d615d9e35b..54478cdc02 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionModule.kt @@ -84,6 +84,8 @@ import org.matrix.android.sdk.internal.session.room.create.RoomCreateEventProces import org.matrix.android.sdk.internal.session.room.prune.RedactionEventProcessor import org.matrix.android.sdk.internal.session.search.DefaultSearchService import org.matrix.android.sdk.internal.session.room.tombstone.RoomTombstoneEventProcessor +import org.matrix.android.sdk.internal.session.search.DefaultSearchTask +import org.matrix.android.sdk.internal.session.search.SearchTask import org.matrix.android.sdk.internal.session.securestorage.DefaultSecureStorageService import org.matrix.android.sdk.internal.session.typing.DefaultTypingUsersTracker import org.matrix.android.sdk.internal.session.user.accountdata.DefaultAccountDataService @@ -373,4 +375,7 @@ internal abstract class SessionModule { @Binds abstract fun bindSearchService(service: DefaultSearchService): SearchService + + @Binds + abstract fun bindSearchTask(task: DefaultSearchTask): SearchTask } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt index 27a51594c3..33fa98f8ce 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoom.kt @@ -36,10 +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.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 @@ -62,7 +65,8 @@ internal class DefaultRoom @Inject constructor(override val roomId: String, private val roomMembersService: MembershipService, private val roomPushRuleService: RoomPushRuleService, private val taskExecutor: TaskExecutor, - private val sendStateTask: SendStateTask) : + private val sendStateTask: SendStateTask, + private val searchTask: SearchTask) : Room, TimelineService by timelineService, SendService by sendService, @@ -123,4 +127,27 @@ internal class DefaultRoom @Inject constructor(override val roomId: String, } } } + + override fun search(searchTerm: String, + nextBatch: String?, + orderByRecent: Boolean, + limit: Int, + beforeLimit: Int, + afterLimit: Int, + includeProfile: Boolean, + callback: MatrixCallback): Cancelable { + return searchTask + .configureWith(SearchTask.Params( + searchTerm = searchTerm, + roomId = roomId, + nextBatch = nextBatch, + orderByRecent = orderByRecent, + limit = limit, + beforeLimit = beforeLimit, + afterLimit = afterLimit, + includeProfile = includeProfile + )) { + this.callback = callback + }.executeBy(taskExecutor) + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomFactory.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomFactory.kt index ac8bdb3992..28eb17314a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomFactory.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomFactory.kt @@ -35,6 +35,7 @@ import org.matrix.android.sdk.internal.session.room.tags.DefaultTagsService import org.matrix.android.sdk.internal.session.room.timeline.DefaultTimelineService import org.matrix.android.sdk.internal.session.room.typing.DefaultTypingService import org.matrix.android.sdk.internal.session.room.uploads.DefaultUploadsService +import org.matrix.android.sdk.internal.session.search.SearchTask import org.matrix.android.sdk.internal.task.TaskExecutor import javax.inject.Inject @@ -59,7 +60,8 @@ internal class DefaultRoomFactory @Inject constructor(private val cryptoService: private val membershipServiceFactory: DefaultMembershipService.Factory, private val roomPushRuleServiceFactory: DefaultRoomPushRuleService.Factory, private val taskExecutor: TaskExecutor, - private val sendStateTask: SendStateTask) : + private val sendStateTask: SendStateTask, + private val searchTask: SearchTask) : RoomFactory { override fun create(roomId: String): Room { @@ -81,7 +83,8 @@ internal class DefaultRoomFactory @Inject constructor(private val cryptoService: roomMembersService = membershipServiceFactory.create(roomId), roomPushRuleService = roomPushRuleServiceFactory.create(roomId), taskExecutor = taskExecutor, - sendStateTask = sendStateTask + sendStateTask = sendStateTask, + searchTask = searchTask ) } } 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 77f8f38410..700507735b 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,8 +61,6 @@ 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.search.DefaultSearchTask -import org.matrix.android.sdk.internal.session.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 @@ -197,7 +195,4 @@ internal abstract class RoomModule { @Binds abstract fun bindDeleteTagFromRoomTask(task: DefaultDeleteTagFromRoomTask): DeleteTagFromRoomTask - - @Binds - abstract fun bindSearchTask(task: DefaultSearchTask): SearchTask }