Search in sources :

Example 21 with RoomMember

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

the class MXCrypto method getE2eRoomMembers.

/**
 * get the users we share an e2e-enabled room with
 *
 * @return {Object<string>} userid->userid map (should be a Set but argh ES6)
 */
private List<String> getE2eRoomMembers() {
    HashSet<String> list = new HashSet<>();
    List<Room> rooms = getE2eRooms();
    for (Room r : rooms) {
        Collection<RoomMember> activeMembers = r.getActiveMembers();
        for (RoomMember m : activeMembers) {
            // add only the matrix id
            if (MXSession.PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER.matcher(m.getUserId()).matches()) {
                list.add(m.getUserId());
            }
        }
    }
    return new ArrayList<>(list);
}
Also used : RoomMember(org.matrix.androidsdk.rest.model.RoomMember) ArrayList(java.util.ArrayList) Room(org.matrix.androidsdk.data.Room) HashSet(java.util.HashSet)

Example 22 with RoomMember

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

the class MXCrypto method setGlobalBlacklistUnverifiedDevices.

/**
 * Set the global override for whether the client should ever send encrypted
 * messages to unverified devices.
 * If false, it can still be overridden per-room.
 * If true, it overrides the per-room settings.
 *
 * @param block    true to unilaterally blacklist all
 * @param callback the asynchronous callback.
 */
public void setGlobalBlacklistUnverifiedDevices(final boolean block, final ApiCallback<Void> callback) {
    final String userId = mSession.getMyUserId();
    final ArrayList<String> userRoomIds = new ArrayList<>();
    Collection<Room> rooms = mSession.getDataHandler().getStore().getRooms();
    for (Room room : rooms) {
        if (room.isEncrypted()) {
            RoomMember roomMember = room.getMember(userId);
            // test if the user joins the room
            if ((null != roomMember) && TextUtils.equals(roomMember.membership, RoomMember.MEMBERSHIP_JOIN)) {
                userRoomIds.add(room.getRoomId());
            }
        }
    }
    getEncryptingThreadHandler().post(new Runnable() {

        @Override
        public void run() {
            mCryptoStore.setGlobalBlacklistUnverifiedDevices(block);
            getUIHandler().post(new Runnable() {

                @Override
                public void run() {
                    if (null != callback) {
                        callback.onSuccess(null);
                    }
                }
            });
        }
    });
}
Also used : RoomMember(org.matrix.androidsdk.rest.model.RoomMember) ArrayList(java.util.ArrayList) Room(org.matrix.androidsdk.data.Room)

Example 23 with RoomMember

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

the class MXCrypto method encryptEventContent.

/**
 * Encrypt an event content according to the configuration of the room.
 *
 * @param eventContent the content of the event.
 * @param eventType    the type of the event.
 * @param room         the room the event will be sent.
 * @param callback     the asynchronous callback
 */
public void encryptEventContent(final JsonElement eventContent, final String eventType, final Room room, final ApiCallback<MXEncryptEventContentResult> callback) {
    // wait that the crypto is really started
    if (!isStarted()) {
        Log.d(LOG_TAG, "## encryptEventContent() : wait after e2e init");
        start(false, new ApiCallback<Void>() {

            @Override
            public void onSuccess(Void info) {
                encryptEventContent(eventContent, eventType, room, callback);
            }

            @Override
            public void onNetworkError(Exception e) {
                Log.e(LOG_TAG, "## encryptEventContent() : onNetworkError while waiting to start e2e : " + e.getMessage());
                if (null != callback) {
                    callback.onNetworkError(e);
                }
            }

            @Override
            public void onMatrixError(MatrixError e) {
                Log.e(LOG_TAG, "## encryptEventContent() : onMatrixError while waiting to start e2e : " + e.getMessage());
                if (null != callback) {
                    callback.onMatrixError(e);
                }
            }

            @Override
            public void onUnexpectedError(Exception e) {
                Log.e(LOG_TAG, "## encryptEventContent() : onUnexpectedError while waiting to start e2e : " + e.getMessage());
                if (null != callback) {
                    callback.onUnexpectedError(e);
                }
            }
        });
        return;
    }
    // just as you are sending a secret message?
    final ArrayList<String> userdIds = new ArrayList<>();
    Collection<RoomMember> joinedMembers = room.getJoinedMembers();
    for (RoomMember m : joinedMembers) {
        userdIds.add(m.getUserId());
    }
    getEncryptingThreadHandler().post(new Runnable() {

        @Override
        public void run() {
            IMXEncrypting alg;
            synchronized (mRoomEncryptors) {
                alg = mRoomEncryptors.get(room.getRoomId());
            }
            if (null == alg) {
                String algorithm = room.getLiveState().encryptionAlgorithm();
                if (null != algorithm) {
                    if (setEncryptionInRoom(room.getRoomId(), algorithm, false)) {
                        synchronized (mRoomEncryptors) {
                            alg = mRoomEncryptors.get(room.getRoomId());
                        }
                    }
                }
            }
            if (null != alg) {
                final long t0 = System.currentTimeMillis();
                Log.d(LOG_TAG, "## encryptEventContent() starts");
                alg.encryptEventContent(eventContent, eventType, userdIds, new ApiCallback<JsonElement>() {

                    @Override
                    public void onSuccess(final JsonElement encryptedContent) {
                        Log.d(LOG_TAG, "## encryptEventContent() : succeeds after " + (System.currentTimeMillis() - t0) + " ms");
                        if (null != callback) {
                            callback.onSuccess(new MXEncryptEventContentResult(encryptedContent, Event.EVENT_TYPE_MESSAGE_ENCRYPTED));
                        }
                    }

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

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

                    @Override
                    public void onUnexpectedError(final Exception e) {
                        Log.e(LOG_TAG, "## encryptEventContent() : onUnexpectedError " + e.getMessage());
                        if (null != callback) {
                            callback.onUnexpectedError(e);
                        }
                    }
                });
            } else {
                final String algorithm = room.getLiveState().encryptionAlgorithm();
                final String reason = String.format(MXCryptoError.UNABLE_TO_ENCRYPT_REASON, (null == algorithm) ? MXCryptoError.NO_MORE_ALGORITHM_REASON : algorithm);
                Log.e(LOG_TAG, "## encryptEventContent() : " + reason);
                if (null != callback) {
                    getUIHandler().post(new Runnable() {

                        @Override
                        public void run() {
                            callback.onMatrixError(new MXCryptoError(MXCryptoError.UNABLE_TO_ENCRYPT_ERROR_CODE, MXCryptoError.UNABLE_TO_ENCRYPT, reason));
                        }
                    });
                }
            }
        }
    });
}
Also used : IMXEncrypting(org.matrix.androidsdk.crypto.algorithms.IMXEncrypting) ApiCallback(org.matrix.androidsdk.rest.callback.ApiCallback) ArrayList(java.util.ArrayList) MXEncryptEventContentResult(org.matrix.androidsdk.crypto.data.MXEncryptEventContentResult) RoomMember(org.matrix.androidsdk.rest.model.RoomMember) JsonElement(com.google.gson.JsonElement) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Example 24 with RoomMember

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

the class MXCrypto method onRoomMembership.

/**
 * Handle a change in the membership state of a member of a room.
 *
 * @param event the membership event causing the change
 */
private void onRoomMembership(final Event event) {
    final IMXEncrypting alg;
    synchronized (mRoomEncryptors) {
        alg = mRoomEncryptors.get(event.roomId);
    }
    if (null == alg) {
        // No encrypting in this room
        return;
    }
    final String userId = event.stateKey;
    RoomMember roomMember = mSession.getDataHandler().getRoom(event.roomId).getLiveState().getMember(userId);
    if (null != roomMember) {
        final String membership = roomMember.membership;
        getEncryptingThreadHandler().post(new Runnable() {

            @Override
            public void run() {
                if (TextUtils.equals(membership, RoomMember.MEMBERSHIP_JOIN)) {
                    // make sure we are tracking the deviceList for this user
                    getDeviceList().startTrackingDeviceList(Arrays.asList(userId));
                }
            }
        });
    }
}
Also used : IMXEncrypting(org.matrix.androidsdk.crypto.algorithms.IMXEncrypting) RoomMember(org.matrix.androidsdk.rest.model.RoomMember)

Example 25 with RoomMember

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

the class MatrixMessagesFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d(LOG_TAG, "onCreateView");
    View v = super.onCreateView(inflater, container, savedInstanceState);
    // the requests are done in onCreateView  instead of onActivityCreated to speed up in the events request
    // it saves only few ms but it reduces the white screen flash.
    mContext = getActivity().getApplicationContext();
    String roomId = getArguments().getString(ARG_ROOM_ID);
    // so try to find it from the fragments call stack.
    if (null == mSession) {
        List<Fragment> fragments = null;
        FragmentManager fm = getActivity().getSupportFragmentManager();
        if (null != fm) {
            fragments = fm.getFragments();
        }
        if (null != fragments) {
            for (Fragment fragment : fragments) {
                if (fragment instanceof MatrixMessageListFragment) {
                    mMatrixMessagesListener = (MatrixMessageListFragment) fragment;
                    mSession = ((MatrixMessageListFragment) fragment).getSession();
                }
            }
        }
    }
    if (mSession == null) {
        throw new RuntimeException("Must have valid default MXSession.");
    }
    // get the timelime
    if (null == mEventTimeline) {
        mEventTimeline = mMatrixMessagesListener.getEventTimeLine();
    } else {
        mEventTimeline.addEventTimelineListener(mEventTimelineListener);
        // the room has already been initialized
        sendInitialMessagesLoaded();
        return v;
    }
    if (null != mEventTimeline) {
        mEventTimeline.addEventTimelineListener(mEventTimelineListener);
        mRoom = mEventTimeline.getRoom();
    }
    // retrieve the room.
    if (null == mRoom) {
        // check if this room has been joined, if not, join it then get messages.
        mRoom = mSession.getDataHandler().getRoom(roomId);
    }
    // GA reported some weird room content
    // so ensure that the room fields are properly initialized
    mSession.getDataHandler().checkRoom(mRoom);
    // display the message history around a dedicated message
    if ((null != mEventTimeline) && !mEventTimeline.isLiveTimeline() && (null != mEventTimeline.getInitialEventId())) {
        initializeTimeline();
    } else {
        boolean joinedRoom = false;
        // does the room already exist ?
        if ((mRoom != null) && (null != mEventTimeline)) {
            // init the history
            mEventTimeline.initHistory();
            // else, the joining could have been half broken (network error)
            if (null != mRoom.getState().creator) {
                RoomMember self = mRoom.getMember(mSession.getCredentials().userId);
                if (self != null && (RoomMember.MEMBERSHIP_JOIN.equals(self.membership) || RoomMember.MEMBERSHIP_KICK.equals(self.membership) || RoomMember.MEMBERSHIP_BAN.equals(self.membership))) {
                    joinedRoom = true;
                }
            }
            mRoom.addEventListener(mEventListener);
            // i.e display the room messages without joining the room
            if (!mEventTimeline.isLiveTimeline()) {
                previewRoom();
            } else // join the room is not yet joined
            if (!joinedRoom) {
                Log.d(LOG_TAG, "Joining room >> " + roomId);
                joinRoom();
            } else {
                // the room is already joined
                // fill the messages list
                mHasPendingInitialHistory = true;
            }
        } else {
            sendInitialMessagesLoaded();
        }
    }
    return v;
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) RoomMember(org.matrix.androidsdk.rest.model.RoomMember) View(android.view.View) Fragment(android.support.v4.app.Fragment)

Aggregations

RoomMember (org.matrix.androidsdk.rest.model.RoomMember)35 ArrayList (java.util.ArrayList)20 Room (org.matrix.androidsdk.data.Room)12 Event (org.matrix.androidsdk.rest.model.Event)6 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 IMXEncrypting (org.matrix.androidsdk.crypto.algorithms.IMXEncrypting)3 ApiCallback (org.matrix.androidsdk.rest.callback.ApiCallback)3 HandlerThread (android.os.HandlerThread)2 JsonObject (com.google.gson.JsonObject)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 MXDecryptionException (org.matrix.androidsdk.crypto.MXDecryptionException)2 IMXStore (org.matrix.androidsdk.data.store.IMXStore)2 SimpleApiCallback (org.matrix.androidsdk.rest.callback.SimpleApiCallback)2 RoomThirdPartyInvite (org.matrix.androidsdk.rest.model.pid.RoomThirdPartyInvite)2 UnrecognizedCertificateException (org.matrix.androidsdk.ssl.UnrecognizedCertificateException)2