Search in sources :

Example 41 with MatrixError

use of org.matrix.androidsdk.core.model.MatrixError in project matrix-android-sdk by matrix-org.

the class EventsRestClient method searchMediaByText.

/**
 * Search a media from its name.
 *
 * @param name        the text to search for.
 * @param rooms       a list of rooms to search in. nil means all rooms the user is in.
 * @param beforeLimit the number of events to get before the matching results.
 * @param afterLimit  the number of events to get after the matching results.
 * @param nextBatch   the token to pass for doing pagination from a previous response.
 * @param callback    the request callback
 */
public void searchMediaByText(final String name, final List<String> rooms, final int beforeLimit, final int afterLimit, final String nextBatch, final ApiCallback<SearchResponse> callback) {
    SearchParams searchParams = new SearchParams();
    SearchRoomEventCategoryParams searchEventParams = new SearchRoomEventCategoryParams();
    searchEventParams.search_term = name;
    searchEventParams.order_by = "recent";
    searchEventParams.event_context = new HashMap<>();
    searchEventParams.event_context.put("before_limit", beforeLimit);
    searchEventParams.event_context.put("after_limit", afterLimit);
    searchEventParams.event_context.put("include_profile", true);
    searchEventParams.filter = new HashMap<>();
    if (null != rooms) {
        searchEventParams.filter.put("rooms", rooms);
    }
    List<String> types = new ArrayList<>();
    types.add(Event.EVENT_TYPE_MESSAGE);
    searchEventParams.filter.put("types", types);
    searchEventParams.filter.put("contains_url", true);
    searchParams.search_categories = new HashMap<>();
    searchParams.search_categories.put("room_events", searchEventParams);
    // other unused filter items
    // not_types
    // not_rooms
    // senders
    // not_senders
    final String uid = System.currentTimeMillis() + "";
    mSearchEventsMediaNameIdentifier = uid + name;
    final String description = "searchMediaByText";
    // don't retry to send the request
    // if the search fails, stop it
    mApi.searchEvents(searchParams, nextBatch).enqueue(new RestAdapterCallback<SearchResponse>(description, null, new ApiCallback<SearchResponse>() {

        /**
         * Tells if the current response for the latest request.
         *
         * @return true if it is the response of the latest request.
         */
        private boolean isActiveRequest() {
            return TextUtils.equals(mSearchEventsMediaNameIdentifier, uid + name);
        }

        @Override
        public void onSuccess(SearchResponse newSearchResponse) {
            if (isActiveRequest()) {
                callback.onSuccess(newSearchResponse);
                mSearchEventsMediaNameIdentifier = null;
            }
        }

        @Override
        public void onNetworkError(Exception e) {
            if (isActiveRequest()) {
                callback.onNetworkError(e);
                mSearchEventsMediaNameIdentifier = null;
            }
        }

        @Override
        public void onMatrixError(MatrixError e) {
            if (isActiveRequest()) {
                callback.onMatrixError(e);
                mSearchEventsMediaNameIdentifier = null;
            }
        }

        @Override
        public void onUnexpectedError(Exception e) {
            if (isActiveRequest()) {
                callback.onUnexpectedError(e);
                mSearchEventsMediaNameIdentifier = null;
            }
        }
    }, new RestAdapterCallback.RequestRetryCallBack() {

        @Override
        public void onRetry() {
            searchMediaByText(name, rooms, beforeLimit, afterLimit, nextBatch, callback);
        }
    }));
}
Also used : SearchRoomEventCategoryParams(org.matrix.androidsdk.rest.model.search.SearchRoomEventCategoryParams) SearchParams(org.matrix.androidsdk.rest.model.search.SearchParams) SimpleApiCallback(org.matrix.androidsdk.core.callback.SimpleApiCallback) ApiCallback(org.matrix.androidsdk.core.callback.ApiCallback) ArrayList(java.util.ArrayList) MatrixError(org.matrix.androidsdk.core.model.MatrixError) SearchResponse(org.matrix.androidsdk.rest.model.search.SearchResponse)

Aggregations

MatrixError (org.matrix.androidsdk.core.model.MatrixError)41 SimpleApiCallback (org.matrix.androidsdk.core.callback.SimpleApiCallback)15 ApiCallback (org.matrix.androidsdk.core.callback.ApiCallback)14 ArrayList (java.util.ArrayList)12 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)9 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 List (java.util.List)7 Room (org.matrix.androidsdk.data.Room)7 Event (org.matrix.androidsdk.rest.model.Event)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 MXDecryptionException (org.matrix.androidsdk.crypto.MXDecryptionException)6 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)6 JsonObject (com.google.gson.JsonObject)5 MXCryptoError (org.matrix.androidsdk.crypto.MXCryptoError)4 JsonElement (com.google.gson.JsonElement)3 MXSession (org.matrix.androidsdk.MXSession)3 MXOlmSessionResult (org.matrix.androidsdk.crypto.data.MXOlmSessionResult)3 RoomMember (org.matrix.androidsdk.rest.model.RoomMember)3 Activity (android.app.Activity)2