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);
}
}));
}
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);
}
}));
}
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();
}
});
}
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);
}
});
}
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;
}
});
}
Aggregations