Search in sources :

Example 1 with SearchRoomEventCategoryParams

use of org.matrix.androidsdk.rest.model.search.SearchRoomEventCategoryParams in project matrix-android-sdk by matrix-org.

the class EventsRestClient method searchMessagesByText.

/**
 * Search a text in room messages.
 *
 * @param text        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 searchMessagesByText(final String text, 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 = text;
    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);
    if (null != rooms) {
        searchEventParams.filter = new HashMap<>();
        searchEventParams.filter.put("rooms", rooms);
    }
    searchParams.search_categories = new HashMap<>();
    searchParams.search_categories.put("room_events", searchEventParams);
    final String description = "searchMessageText";
    final String uid = System.currentTimeMillis() + "";
    mSearchEventsPatternIdentifier = uid + text;
    try {
        // don't retry to send the request
        // if the search fails, stop it
        mApi.searchEvents(searchParams, nextBatch, 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(mSearchEventsPatternIdentifier, uid + text);
            }

            @Override
            public void onSuccess(SearchResponse response) {
                if (isActiveRequest()) {
                    if (null != callback) {
                        callback.onSuccess(response);
                    }
                    mSearchEventsPatternIdentifier = null;
                }
            }

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

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

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

            @Override
            public void onRetry() {
                searchMessagesByText(text, rooms, beforeLimit, afterLimit, nextBatch, callback);
            }
        }));
    } catch (Throwable t) {
        callback.onUnexpectedError(new Exception(t));
    }
}
Also used : SearchRoomEventCategoryParams(org.matrix.androidsdk.rest.model.search.SearchRoomEventCategoryParams) SearchParams(org.matrix.androidsdk.rest.model.search.SearchParams) ApiCallback(org.matrix.androidsdk.rest.callback.ApiCallback) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) SearchResponse(org.matrix.androidsdk.rest.model.search.SearchResponse)

Example 2 with SearchRoomEventCategoryParams

use of org.matrix.androidsdk.rest.model.search.SearchRoomEventCategoryParams in project matrix-android-sdk by matrix-org.

the class EventsRestClient method searchMediasByText.

/**
 * 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 searchMediasByText(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);
    }
    ArrayList<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 = "searchMediasByText";
    try {
        // don't retry to send the request
        // if the search fails, stop it
        mApi.searchEvents(searchParams, nextBatch, 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() {
                searchMediasByText(name, rooms, beforeLimit, afterLimit, nextBatch, callback);
            }
        }));
    } catch (Throwable t) {
        callback.onUnexpectedError(new Exception(t));
    }
}
Also used : SearchRoomEventCategoryParams(org.matrix.androidsdk.rest.model.search.SearchRoomEventCategoryParams) SearchParams(org.matrix.androidsdk.rest.model.search.SearchParams) ApiCallback(org.matrix.androidsdk.rest.callback.ApiCallback) ArrayList(java.util.ArrayList) SearchResponse(org.matrix.androidsdk.rest.model.search.SearchResponse) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Aggregations

ApiCallback (org.matrix.androidsdk.rest.callback.ApiCallback)2 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)2 SearchParams (org.matrix.androidsdk.rest.model.search.SearchParams)2 SearchResponse (org.matrix.androidsdk.rest.model.search.SearchResponse)2 SearchRoomEventCategoryParams (org.matrix.androidsdk.rest.model.search.SearchRoomEventCategoryParams)2 ArrayList (java.util.ArrayList)1