Search in sources :

Example 11 with MatrixError

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

the class EventsRestClient method searchUsers.

/**
 * Search users with a patter,
 *
 * @param text          the text to search for.
 * @param limit         the maximum nbr of users in the response
 * @param userIdsFilter the userIds to exclude from the result
 * @param callback      the request callback
 */
public void searchUsers(final String text, final Integer limit, final Set<String> userIdsFilter, final ApiCallback<SearchUsersResponse> callback) {
    SearchUsersParams searchParams = new SearchUsersParams();
    searchParams.search_term = text;
    searchParams.limit = limit + ((null != userIdsFilter) ? userIdsFilter.size() : 0);
    final String uid = mSearchUsersPatternIdentifier = System.currentTimeMillis() + " " + text + " " + limit;
    final String description = "searchUsers";
    // don't retry to send the request
    // if the search fails, stop it
    mApi.searchUsers(searchParams).enqueue(new RestAdapterCallback<SearchUsersRequestResponse>(description, null, new ApiCallback<SearchUsersRequestResponse>() {

        /**
         * 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(mSearchUsersPatternIdentifier, uid);
        }

        @Override
        public void onSuccess(SearchUsersRequestResponse aResponse) {
            if (isActiveRequest()) {
                SearchUsersResponse response = new SearchUsersResponse();
                response.limited = aResponse.limited;
                response.results = new ArrayList<>();
                Set<String> filter = (null != userIdsFilter) ? userIdsFilter : new HashSet<String>();
                if (null != aResponse.results) {
                    for (SearchUsersRequestResponse.User user : aResponse.results) {
                        if ((null != user.user_id) && !filter.contains(user.user_id)) {
                            User addedUser = new User();
                            addedUser.user_id = user.user_id;
                            addedUser.avatar_url = user.avatar_url;
                            addedUser.displayname = user.display_name;
                            response.results.add(addedUser);
                        }
                    }
                }
                callback.onSuccess(response);
                mSearchUsersPatternIdentifier = null;
            }
        }

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

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

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

        @Override
        public void onRetry() {
            searchUsers(text, limit, userIdsFilter, callback);
        }
    }));
}
Also used : User(org.matrix.androidsdk.rest.model.User) SimpleApiCallback(org.matrix.androidsdk.core.callback.SimpleApiCallback) ApiCallback(org.matrix.androidsdk.core.callback.ApiCallback) SearchUsersParams(org.matrix.androidsdk.rest.model.search.SearchUsersParams) SearchUsersRequestResponse(org.matrix.androidsdk.rest.model.search.SearchUsersRequestResponse) MatrixError(org.matrix.androidsdk.core.model.MatrixError) SearchUsersResponse(org.matrix.androidsdk.rest.model.search.SearchUsersResponse)

Example 12 with MatrixError

use of org.matrix.androidsdk.core.model.MatrixError 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;
    // 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(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);
        }
    }));
}
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) MatrixError(org.matrix.androidsdk.core.model.MatrixError) SearchResponse(org.matrix.androidsdk.rest.model.search.SearchResponse)

Example 13 with MatrixError

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

the class MXOutgoingRoomKeyRequestManager method sendOutgoingRoomKeyRequestCancellation.

/**
 * Given a OutgoingRoomKeyRequest, cancel it and delete the request record
 *
 * @param request the request
 */
private void sendOutgoingRoomKeyRequestCancellation(final OutgoingRoomKeyRequest request) {
    Log.d(LOG_TAG, "## sendOutgoingRoomKeyRequestCancellation() : Sending cancellation for key request for " + request.mRequestBody + " to " + request.mRecipients + " cancellation id  " + request.mCancellationTxnId);
    RoomKeyShareCancellation roomKeyShareCancellation = new RoomKeyShareCancellation();
    roomKeyShareCancellation.requestingDeviceId = mCryptoStore.getDeviceId();
    roomKeyShareCancellation.requestId = request.mCancellationTxnId;
    sendMessageToDevices(roomKeyShareCancellation, request.mRecipients, request.mCancellationTxnId, new ApiCallback<Void>() {

        private void onDone() {
            mWorkingHandler.post(new Runnable() {

                @Override
                public void run() {
                    mCryptoStore.deleteOutgoingRoomKeyRequest(request.mRequestId);
                    mSendOutgoingRoomKeyRequestsRunning = false;
                    startTimer();
                }
            });
        }

        @Override
        public void onSuccess(Void info) {
            Log.d(LOG_TAG, "## sendOutgoingRoomKeyRequestCancellation() : done");
            boolean resend = request.mState == OutgoingRoomKeyRequest.RequestState.CANCELLATION_PENDING_AND_WILL_RESEND;
            onDone();
            // Resend the request with a new ID
            if (resend) {
                sendRoomKeyRequest(request.mRequestBody, request.mRecipients);
            }
        }

        @Override
        public void onNetworkError(Exception e) {
            Log.e(LOG_TAG, "## sendOutgoingRoomKeyRequestCancellation failed " + e.getMessage(), e);
            onDone();
        }

        @Override
        public void onMatrixError(MatrixError e) {
            Log.e(LOG_TAG, "## sendOutgoingRoomKeyRequestCancellation failed " + e.getMessage());
            onDone();
        }

        @Override
        public void onUnexpectedError(Exception e) {
            Log.e(LOG_TAG, "## sendOutgoingRoomKeyRequestCancellation failed " + e.getMessage(), e);
            onDone();
        }
    });
}
Also used : RoomKeyShareCancellation(org.matrix.androidsdk.crypto.rest.model.crypto.RoomKeyShareCancellation) MatrixError(org.matrix.androidsdk.core.model.MatrixError)

Example 14 with MatrixError

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

the class MXOutgoingRoomKeyRequestManager method sendOutgoingRoomKeyRequest.

/**
 * Send the outgoing key request.
 *
 * @param request the request
 */
private void sendOutgoingRoomKeyRequest(final OutgoingRoomKeyRequest request) {
    Log.d(LOG_TAG, "## sendOutgoingRoomKeyRequest() : Requesting keys " + request.mRequestBody + " from " + request.mRecipients + " id " + request.mRequestId);
    RoomKeyShareRequest requestMessage = new RoomKeyShareRequest();
    requestMessage.requestingDeviceId = mCryptoStore.getDeviceId();
    requestMessage.requestId = request.mRequestId;
    requestMessage.body = request.mRequestBody;
    sendMessageToDevices(requestMessage, request.mRecipients, request.mRequestId, new ApiCallback<Void>() {

        private void onDone(final OutgoingRoomKeyRequest.RequestState state) {
            mWorkingHandler.post(new Runnable() {

                @Override
                public void run() {
                    if (request.mState != OutgoingRoomKeyRequest.RequestState.UNSENT) {
                        Log.d(LOG_TAG, "## sendOutgoingRoomKeyRequest() : Cannot update room key request from UNSENT as it was already updated to " + request.mState);
                    } else {
                        request.mState = state;
                        mCryptoStore.updateOutgoingRoomKeyRequest(request);
                    }
                    mSendOutgoingRoomKeyRequestsRunning = false;
                    startTimer();
                }
            });
        }

        @Override
        public void onSuccess(Void info) {
            Log.d(LOG_TAG, "## sendOutgoingRoomKeyRequest succeed");
            onDone(OutgoingRoomKeyRequest.RequestState.SENT);
        }

        @Override
        public void onNetworkError(Exception e) {
            Log.e(LOG_TAG, "## sendOutgoingRoomKeyRequest failed " + e.getMessage(), e);
            onDone(OutgoingRoomKeyRequest.RequestState.FAILED);
        }

        @Override
        public void onMatrixError(MatrixError e) {
            Log.e(LOG_TAG, "## sendOutgoingRoomKeyRequest failed " + e.getMessage());
            onDone(OutgoingRoomKeyRequest.RequestState.FAILED);
        }

        @Override
        public void onUnexpectedError(Exception e) {
            Log.e(LOG_TAG, "## sendOutgoingRoomKeyRequest failed " + e.getMessage(), e);
            onDone(OutgoingRoomKeyRequest.RequestState.FAILED);
        }
    });
}
Also used : MatrixError(org.matrix.androidsdk.core.model.MatrixError) RoomKeyShareRequest(org.matrix.androidsdk.crypto.rest.model.crypto.RoomKeyShareRequest)

Example 15 with MatrixError

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

the class MXMegolmEncryption method ensureOutboundSession.

/**
 * Ensure the outbound session
 *
 * @param devicesInRoom the devices list
 * @param callback      the asynchronous callback.
 */
private void ensureOutboundSession(MXUsersDevicesMap<MXDeviceInfo> devicesInRoom, final ApiCallback<MXOutboundSessionInfo> callback) {
    MXOutboundSessionInfo session = mOutboundSession;
    if ((null == session) || // Need to make a brand new session?
    session.needsRotation(mSessionRotationPeriodMsgs, mSessionRotationPeriodMs) || // Determine if we have shared with anyone we shouldn't have
    session.sharedWithTooManyDevices(devicesInRoom)) {
        mOutboundSession = session = prepareNewSessionInRoom();
    }
    if (mShareOperationIsProgress) {
        Log.d(LOG_TAG, "## ensureOutboundSessionInRoom() : already in progress");
        // Key share already in progress
        return;
    }
    final MXOutboundSessionInfo fSession = session;
    Map<String, List<MXDeviceInfo>> /* userId */
    shareMap = new HashMap<>();
    List<String> userIds = devicesInRoom.getUserIds();
    for (String userId : userIds) {
        List<String> deviceIds = devicesInRoom.getUserDeviceIds(userId);
        for (String deviceId : deviceIds) {
            MXDeviceInfo deviceInfo = devicesInRoom.getObject(deviceId, userId);
            if (null == fSession.mSharedWithDevices.getObject(deviceId, userId)) {
                if (!shareMap.containsKey(userId)) {
                    shareMap.put(userId, new ArrayList<MXDeviceInfo>());
                }
                shareMap.get(userId).add(deviceInfo);
            }
        }
    }
    shareKey(fSession, shareMap, new ApiCallback<Void>() {

        @Override
        public void onSuccess(Void anything) {
            mShareOperationIsProgress = false;
            if (null != callback) {
                callback.onSuccess(fSession);
            }
        }

        @Override
        public void onNetworkError(final Exception e) {
            Log.e(LOG_TAG, "## ensureOutboundSessionInRoom() : shareKey onNetworkError " + e.getMessage(), e);
            if (null != callback) {
                callback.onNetworkError(e);
            }
            mShareOperationIsProgress = false;
        }

        @Override
        public void onMatrixError(final MatrixError e) {
            Log.e(LOG_TAG, "## ensureOutboundSessionInRoom() : shareKey onMatrixError " + e.getMessage());
            if (null != callback) {
                callback.onMatrixError(e);
            }
            mShareOperationIsProgress = false;
        }

        @Override
        public void onUnexpectedError(final Exception e) {
            Log.e(LOG_TAG, "## ensureOutboundSessionInRoom() : shareKey onUnexpectedError " + e.getMessage(), e);
            if (null != callback) {
                callback.onUnexpectedError(e);
            }
            mShareOperationIsProgress = false;
        }
    });
}
Also used : HashMap(java.util.HashMap) MXDeviceInfo(org.matrix.androidsdk.crypto.data.MXDeviceInfo) ArrayList(java.util.ArrayList) List(java.util.List) MatrixError(org.matrix.androidsdk.core.model.MatrixError)

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