Search in sources :

Example 1 with CreatedEvent

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

the class Room method sendEvent.

// ==============================================================================================================
// Send methods
// ==============================================================================================================
/**
 * Send an event content to the room.
 * The event is updated with the data provided by the server
 * The provided event contains the error description.
 *
 * @param event    the message
 * @param callback the callback with the created event
 */
public void sendEvent(final Event event, final ApiCallback<Void> callback) {
    // wait that the room is synced before sending messages
    if (!mIsReady || !isJoined()) {
        mDataHandler.updateEventState(event, Event.SentState.WAITING_RETRY);
        try {
            callback.onNetworkError(null);
        } catch (Exception e) {
            Log.e(LOG_TAG, "sendEvent exception " + e.getMessage(), e);
        }
        return;
    }
    final String prevEventId = event.eventId;
    final ApiCallback<CreatedEvent> localCB = new ApiCallback<CreatedEvent>() {

        @Override
        public void onSuccess(final CreatedEvent createdEvent) {
            if (null != getStore()) {
                // remove the tmp event
                getStore().deleteEvent(event);
            }
            // replace the tmp event id by the final one
            boolean isReadMarkerUpdated = TextUtils.equals(getReadMarkerEventId(), event.eventId);
            // update the event with the server response
            event.eventId = createdEvent.eventId;
            event.originServerTs = System.currentTimeMillis();
            mDataHandler.updateEventState(event, Event.SentState.SENT);
            // the message echo is not yet echoed
            if (null != getStore() && !getStore().doesEventExist(createdEvent.eventId, getRoomId())) {
                getStore().storeLiveRoomEvent(event);
            }
            // send the dedicated read receipt asap
            markAllAsRead(isReadMarkerUpdated, null);
            if (null != getStore()) {
                getStore().commit();
            }
            mDataHandler.onEventSent(event, prevEventId);
            try {
                callback.onSuccess(null);
            } catch (Exception e) {
                Log.e(LOG_TAG, "sendEvent exception " + e.getMessage(), e);
            }
        }

        @Override
        public void onNetworkError(Exception e) {
            event.unsentException = e;
            mDataHandler.updateEventState(event, Event.SentState.UNDELIVERED);
            try {
                callback.onNetworkError(e);
            } catch (Exception anException) {
                Log.e(LOG_TAG, "sendEvent exception " + anException.getMessage(), anException);
            }
        }

        @Override
        public void onMatrixError(MatrixError e) {
            event.unsentMatrixError = e;
            mDataHandler.updateEventState(event, Event.SentState.UNDELIVERED);
            if (MatrixError.isConfigurationErrorCode(e.errcode)) {
                mDataHandler.onConfigurationError(e.errcode);
            } else {
                try {
                    callback.onMatrixError(e);
                } catch (Exception anException) {
                    Log.e(LOG_TAG, "sendEvent exception " + anException.getMessage(), anException);
                }
            }
        }

        @Override
        public void onUnexpectedError(Exception e) {
            event.unsentException = e;
            mDataHandler.updateEventState(event, Event.SentState.UNDELIVERED);
            try {
                callback.onUnexpectedError(e);
            } catch (Exception anException) {
                Log.e(LOG_TAG, "sendEvent exception " + anException.getMessage(), anException);
            }
        }
    };
    if (isEncrypted() && (null != mDataHandler.getCrypto())) {
        mDataHandler.updateEventState(event, Event.SentState.ENCRYPTING);
        // Store the "m.relates_to" data and remove them from event content before encrypting the event content
        final JsonElement relatesTo;
        JsonObject contentAsJsonObject = event.getContentAsJsonObject();
        if (contentAsJsonObject != null && contentAsJsonObject.has("m.relates_to")) {
            // Get a copy of "m.relates_to" data...
            relatesTo = contentAsJsonObject.get("m.relates_to");
            // ... and remove "m.relates_to" data from the content before encrypting it
            contentAsJsonObject.remove("m.relates_to");
        } else {
            relatesTo = null;
        }
        // Encrypt the content before sending
        mDataHandler.getCrypto().encryptEventContent(contentAsJsonObject, event.getType(), this, new ApiCallback<MXEncryptEventContentResult>() {

            @Override
            public void onSuccess(MXEncryptEventContentResult encryptEventContentResult) {
                // update the event content with the encrypted data
                event.type = encryptEventContentResult.mEventType;
                // Add the "m.relates_to" data to the encrypted event here
                JsonObject encryptedContent = encryptEventContentResult.mEventContent.getAsJsonObject();
                if (relatesTo != null) {
                    encryptedContent.add("m.relates_to", relatesTo);
                }
                event.updateContent(encryptedContent);
                mDataHandler.decryptEvent(event, null);
                // sending in progress
                mDataHandler.updateEventState(event, Event.SentState.SENDING);
                mDataHandler.getDataRetriever().getRoomsRestClient().sendEventToRoom(event.eventId, getRoomId(), encryptEventContentResult.mEventType, encryptEventContentResult.mEventContent.getAsJsonObject(), localCB);
            }

            @Override
            public void onNetworkError(Exception e) {
                event.unsentException = e;
                mDataHandler.updateEventState(event, Event.SentState.UNDELIVERED);
                if (null != callback) {
                    callback.onNetworkError(e);
                }
            }

            @Override
            public void onMatrixError(MatrixError e) {
                // update the sent state if the message encryption failed because there are unknown devices.
                if ((e instanceof MXCryptoError) && TextUtils.equals(((MXCryptoError) e).errcode, MXCryptoError.UNKNOWN_DEVICES_CODE)) {
                    event.mSentState = Event.SentState.FAILED_UNKNOWN_DEVICES;
                } else {
                    event.mSentState = Event.SentState.UNDELIVERED;
                }
                event.unsentMatrixError = e;
                mDataHandler.onEventSentStateUpdated(event);
                if (null != callback) {
                    callback.onMatrixError(e);
                }
            }

            @Override
            public void onUnexpectedError(Exception e) {
                event.unsentException = e;
                mDataHandler.updateEventState(event, Event.SentState.UNDELIVERED);
                if (null != callback) {
                    callback.onUnexpectedError(e);
                }
            }
        });
    } else {
        mDataHandler.updateEventState(event, Event.SentState.SENDING);
        if (Event.EVENT_TYPE_MESSAGE.equals(event.getType())) {
            mDataHandler.getDataRetriever().getRoomsRestClient().sendMessage(event.eventId, getRoomId(), event.getContentAsJsonObject(), localCB);
        } else {
            mDataHandler.getDataRetriever().getRoomsRestClient().sendEventToRoom(event.eventId, getRoomId(), event.getType(), event.getContentAsJsonObject(), localCB);
        }
    }
}
Also used : SimpleApiCallback(org.matrix.androidsdk.core.callback.SimpleApiCallback) ApiCallback(org.matrix.androidsdk.core.callback.ApiCallback) JsonObject(com.google.gson.JsonObject) MXEncryptEventContentResult(org.matrix.androidsdk.crypto.data.MXEncryptEventContentResult) CreatedEvent(org.matrix.androidsdk.rest.model.CreatedEvent) JsonElement(com.google.gson.JsonElement) MatrixError(org.matrix.androidsdk.core.model.MatrixError) MXCryptoError(org.matrix.androidsdk.crypto.MXCryptoError)

Aggregations

JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ApiCallback (org.matrix.androidsdk.core.callback.ApiCallback)1 SimpleApiCallback (org.matrix.androidsdk.core.callback.SimpleApiCallback)1 MatrixError (org.matrix.androidsdk.core.model.MatrixError)1 MXCryptoError (org.matrix.androidsdk.crypto.MXCryptoError)1 MXEncryptEventContentResult (org.matrix.androidsdk.crypto.data.MXEncryptEventContentResult)1 CreatedEvent (org.matrix.androidsdk.rest.model.CreatedEvent)1