Search in sources :

Example 1 with ApiUserOutPeer

use of im.actor.core.api.ApiUserOutPeer in project actor-platform by actorapp.

the class BookImportActor method performImportIfRequired.

private void performImportIfRequired() {
    if (ENABLE_LOG) {
        Log.d(TAG, "performImportIfRequired called");
    }
    if (isUploadingContacts) {
        if (ENABLE_LOG) {
            Log.d(TAG, "performImportIfRequired:exiting:already importing");
        }
        return;
    }
    if (importQueue.size() == 0) {
        if (ENABLE_LOG) {
            Log.d(TAG, "performImportIfRequired:exiting:nothing to import");
        }
        // Marking as everything is imported
        context().getConductor().getConductor().onPhoneBookImported();
        return;
    }
    //
    // Performing import
    //
    isUploadingContacts = true;
    final ArrayList<ApiPhoneToImport> phoneToImports = new ArrayList<>();
    final ArrayList<ApiEmailToImport> emailToImports = new ArrayList<>();
    for (int i = 0; i < MAX_IMPORT_SIZE && importQueue.size() > 0; i++) {
        ImportQueueItem importQueueItem = importQueue.remove(0);
        if (importQueueItem instanceof ImportEmailQueueItem) {
            emailToImports.add(new ApiEmailToImport(((ImportEmailQueueItem) importQueueItem).getEmail(), ((ImportEmailQueueItem) importQueueItem).getName()));
        } else if (importQueueItem instanceof ImportPhoneQueueItem) {
            phoneToImports.add(new ApiPhoneToImport(((ImportPhoneQueueItem) importQueueItem).getPhoneNumber(), ((ImportPhoneQueueItem) importQueueItem).getName()));
        } else {
            throw new RuntimeException();
        }
    }
    request(new RequestImportContacts(phoneToImports, emailToImports, ApiSupportConfiguration.OPTIMIZATIONS), new RpcCallback<ResponseImportContacts>() {

        @Override
        public void onResult(ResponseImportContacts response) {
            for (ApiPhoneToImport phoneToImport : phoneToImports) {
                storage.markAsImported(phoneToImport.getPhoneNumber());
                importingPhones.remove(phoneToImport.getPhoneNumber());
            }
            for (ApiEmailToImport emailToImport : emailToImports) {
                storage.markAsImported(emailToImport.getEmail());
                importingEmails.remove(emailToImport.getEmail());
            }
            context().getContactsModule().getBookImportState().put(0, storage.toByteArray());
            //
            if (response.getUsers().size() != 0 || response.getUserPeers().size() != 0) {
                if (ENABLE_LOG) {
                    Log.d(TAG, "Import success with " + (response.getUsers().size() + response.getUserPeers().size()) + " new contacts");
                }
                if (response.getUserPeers().size() != 0) {
                    // Optimized version
                    ArrayList<Integer> uids = new ArrayList<>();
                    for (ApiUserOutPeer u : response.getUserPeers()) {
                        uids.add(u.getUid());
                    }
                    updates().loadRequiredPeers(response.getUserPeers(), new ArrayList<>()).flatMap(v -> updates().applyUpdate(response.getSeq(), response.getState(), new UpdateContactsAdded(uids)));
                } else {
                    // Old version
                    ArrayList<Integer> uids = new ArrayList<>();
                    for (ApiUser u : response.getUsers()) {
                        uids.add(u.getId());
                    }
                    updates().onUpdateReceived(new FatSeqUpdate(response.getSeq(), response.getState(), UpdateContactsAdded.HEADER, new UpdateContactsAdded(uids).toByteArray(), response.getUsers(), new ArrayList<>()));
                }
            } else {
                if (ENABLE_LOG) {
                    Log.d(TAG, "Import success, but no new contacts found");
                }
            }
            //
            // Launching next iteration
            //
            isUploadingContacts = false;
            performImportIfRequired();
        }

        @Override
        public void onError(RpcException e) {
            // TODO: Better error handling
            if (ENABLE_LOG) {
                Log.d(TAG, "Import failure");
            }
            e.printStackTrace();
            //
            // Launching next iteration
            //
            isUploadingContacts = false;
            performImportIfRequired();
        }
    });
}
Also used : ApiSupportConfiguration(im.actor.core.modules.api.ApiSupportConfiguration) ApiEmailToImport(im.actor.core.api.ApiEmailToImport) ModuleContext(im.actor.core.modules.ModuleContext) UpdateContactsAdded(im.actor.core.api.updates.UpdateContactsAdded) Void(im.actor.runtime.actors.messages.Void) Bser(im.actor.runtime.bser.Bser) ApiPhoneToImport(im.actor.core.api.ApiPhoneToImport) PhoneBookContact(im.actor.core.entity.PhoneBookContact) PhoneBookIds(im.actor.core.entity.PhoneBookIds) ApiUser(im.actor.core.api.ApiUser) FatSeqUpdate(im.actor.core.api.base.FatSeqUpdate) ResponseImportContacts(im.actor.core.api.rpc.ResponseImportContacts) RpcException(im.actor.core.network.RpcException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestImportContacts(im.actor.core.api.rpc.RequestImportContacts) List(java.util.List) ModuleActor(im.actor.core.modules.ModuleActor) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) BookImportStorage(im.actor.core.modules.contacts.entity.BookImportStorage) RpcCallback(im.actor.core.network.RpcCallback) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) Log(im.actor.runtime.Log) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone) UpdateContactsAdded(im.actor.core.api.updates.UpdateContactsAdded) FatSeqUpdate(im.actor.core.api.base.FatSeqUpdate) ApiPhoneToImport(im.actor.core.api.ApiPhoneToImport) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ApiUser(im.actor.core.api.ApiUser) ArrayList(java.util.ArrayList) ResponseImportContacts(im.actor.core.api.rpc.ResponseImportContacts) ApiEmailToImport(im.actor.core.api.ApiEmailToImport) RpcException(im.actor.core.network.RpcException) RequestImportContacts(im.actor.core.api.rpc.RequestImportContacts)

Example 2 with ApiUserOutPeer

use of im.actor.core.api.ApiUserOutPeer in project actor-platform by actorapp.

the class KeyManagerActor method fetchUserPreKey.

/**
     * Fetching user's random pre key
     *
     * @param uid        User's id
     * @param keyGroupId User's key group id
     */
private Promise<PublicKey> fetchUserPreKey(final int uid, final int keyGroupId) {
    return pickUserGroup(uid, keyGroupId).flatMap(new Function<Tuple2<UserKeysGroup, UserKeys>, Promise<PublicKey>>() {

        @Override
        public Promise<PublicKey> apply(final Tuple2<UserKeysGroup, UserKeys> keyGroups) {
            return api(new RequestLoadPrePublicKeys(new ApiUserOutPeer(uid, getUser(uid).getAccessHash()), keyGroupId)).map(new Function<ResponsePublicKeys, PublicKey>() {

                @Override
                public PublicKey apply(ResponsePublicKeys response) {
                    if (response.getPublicKey().size() == 0) {
                        throw new RuntimeException("User doesn't have pre keys");
                    }
                    ApiEncryptionKey key = response.getPublicKey().get(0);
                    ApiEncryptionKeySignature sig = null;
                    for (ApiEncryptionKeySignature s : response.getSignatures()) {
                        if (s.getKeyId() == key.getKeyId() && "Ed25519".equals(s.getSignatureAlg())) {
                            sig = s;
                            break;
                        }
                    }
                    if (sig == null) {
                        throw new RuntimeException("Unable to find public key on server");
                    }
                    byte[] keyHash = RatchetKeySignature.hashForSignature(key.getKeyId(), key.getKeyAlg(), key.getKeyMaterial());
                    if (!Curve25519.verifySignature(keyGroups.getT1().getIdentityKey().getPublicKey(), keyHash, sig.getSignature())) {
                        throw new RuntimeException("Key signature does not isMatch");
                    }
                    return new PublicKey(key.getKeyId(), key.getKeyAlg(), key.getKeyMaterial());
                }
            });
        }
    });
}
Also used : RequestLoadPrePublicKeys(im.actor.core.api.rpc.RequestLoadPrePublicKeys) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) RequestLoadPublicKey(im.actor.core.api.rpc.RequestLoadPublicKey) PublicKey(im.actor.core.modules.encryption.entity.PublicKey) UserKeys(im.actor.core.modules.encryption.entity.UserKeys) ResponsePublicKeys(im.actor.core.api.rpc.ResponsePublicKeys) UserKeysGroup(im.actor.core.modules.encryption.entity.UserKeysGroup) Promise(im.actor.runtime.promise.Promise) Function(im.actor.runtime.function.Function) ApiEncryptionKeySignature(im.actor.core.api.ApiEncryptionKeySignature) Tuple2(im.actor.runtime.function.Tuple2) ApiEncryptionKey(im.actor.core.api.ApiEncryptionKey)

Example 3 with ApiUserOutPeer

use of im.actor.core.api.ApiUserOutPeer in project actor-platform by actorapp.

the class KeyManagerActor method fetchUserPreKey.

/**
     * Fetching user's pre key by key id
     *
     * @param uid        User's id
     * @param keyGroupId User's key group id
     * @param keyId      Key id
     */
private Promise<PublicKey> fetchUserPreKey(final int uid, final int keyGroupId, final long keyId) {
    User user = users().getValue(uid);
    if (user == null) {
        throw new RuntimeException("Unable to find user #" + uid);
    }
    return pickUserGroup(uid, keyGroupId).flatMap(new Function<Tuple2<UserKeysGroup, UserKeys>, Promise<PublicKey>>() {

        @Override
        public Promise<PublicKey> apply(final Tuple2<UserKeysGroup, UserKeys> keysGroup) {
            for (PublicKey p : keysGroup.getT1().getEphemeralKeys()) {
                if (p.getKeyId() == keyId) {
                    return Promise.success(p);
                }
            }
            //
            // Downloading pre key
            //
            ArrayList<Long> ids = new ArrayList<Long>();
            ids.add(keyId);
            final UserKeysGroup finalKeysGroup = keysGroup.getT1();
            return api(new RequestLoadPublicKey(new ApiUserOutPeer(uid, getUser(uid).getAccessHash()), keyGroupId, ids)).map(new Function<ResponsePublicKeys, PublicKey>() {

                @Override
                public PublicKey apply(ResponsePublicKeys responsePublicKeys) {
                    if (responsePublicKeys.getPublicKey().size() == 0) {
                        throw new RuntimeException("Unable to find public key on server");
                    }
                    ApiEncryptionKeySignature sig = null;
                    for (ApiEncryptionKeySignature s : responsePublicKeys.getSignatures()) {
                        if (s.getKeyId() == keyId && "Ed25519".equals(s.getSignatureAlg())) {
                            sig = s;
                            break;
                        }
                    }
                    if (sig == null) {
                        throw new RuntimeException("Unable to find public key on server");
                    }
                    ApiEncryptionKey key = responsePublicKeys.getPublicKey().get(0);
                    byte[] keyHash = RatchetKeySignature.hashForSignature(key.getKeyId(), key.getKeyAlg(), key.getKeyMaterial());
                    if (!Curve25519.verifySignature(keysGroup.getT1().getIdentityKey().getPublicKey(), keyHash, sig.getSignature())) {
                        throw new RuntimeException("Key signature does not isMatch");
                    }
                    PublicKey pkey = new PublicKey(keyId, key.getKeyAlg(), key.getKeyMaterial());
                    UserKeysGroup userKeysGroup = finalKeysGroup.addPublicKey(pkey);
                    cacheUserKeys(keysGroup.getT2().removeUserKeyGroup(userKeysGroup.getKeyGroupId()).addUserKeyGroup(userKeysGroup));
                    return pkey;
                }
            });
        }
    });
}
Also used : User(im.actor.core.entity.User) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) RequestLoadPublicKey(im.actor.core.api.rpc.RequestLoadPublicKey) PublicKey(im.actor.core.modules.encryption.entity.PublicKey) RequestLoadPublicKey(im.actor.core.api.rpc.RequestLoadPublicKey) ArrayList(java.util.ArrayList) UserKeys(im.actor.core.modules.encryption.entity.UserKeys) ResponsePublicKeys(im.actor.core.api.rpc.ResponsePublicKeys) UserKeysGroup(im.actor.core.modules.encryption.entity.UserKeysGroup) Promise(im.actor.runtime.promise.Promise) Function(im.actor.runtime.function.Function) ApiEncryptionKeySignature(im.actor.core.api.ApiEncryptionKeySignature) Tuple2(im.actor.runtime.function.Tuple2) ApiEncryptionKey(im.actor.core.api.ApiEncryptionKey)

Example 4 with ApiUserOutPeer

use of im.actor.core.api.ApiUserOutPeer in project actor-platform by actorapp.

the class KeyManagerActor method fetchUserGroups.

//
// User keys fetching
//
/**
     * Fetching all user's key groups
     *
     * @param uid User's id
     */
private Promise<UserKeys> fetchUserGroups(final int uid) {
    User user = users().getValue(uid);
    if (user == null) {
        throw new RuntimeException("Unable to find user #" + uid);
    }
    final UserKeys userKeys = getCachedUserKeys(uid);
    if (userKeys != null) {
        return Promise.success(userKeys);
    }
    return api(new RequestLoadPublicKeyGroups(new ApiUserOutPeer(uid, user.getAccessHash()))).map(new Function<ResponsePublicKeyGroups, ArrayList<UserKeysGroup>>() {

        @Override
        public ArrayList<UserKeysGroup> apply(ResponsePublicKeyGroups response) {
            ArrayList<UserKeysGroup> keysGroups = new ArrayList<>();
            for (ApiEncryptionKeyGroup keyGroup : response.getPublicKeyGroups()) {
                UserKeysGroup validatedKeysGroup = validateUserKeysGroup(uid, keyGroup);
                if (validatedKeysGroup != null) {
                    keysGroups.add(validatedKeysGroup);
                }
            }
            return keysGroups;
        }
    }).map(new Function<ArrayList<UserKeysGroup>, UserKeys>() {

        @Override
        public UserKeys apply(ArrayList<UserKeysGroup> userKeysGroups) {
            UserKeys userKeys = new UserKeys(uid, userKeysGroups.toArray(new UserKeysGroup[userKeysGroups.size()]));
            cacheUserKeys(userKeys);
            return userKeys;
        }
    });
}
Also used : ResponsePublicKeyGroups(im.actor.core.api.rpc.ResponsePublicKeyGroups) User(im.actor.core.entity.User) RequestLoadPublicKeyGroups(im.actor.core.api.rpc.RequestLoadPublicKeyGroups) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ArrayList(java.util.ArrayList) UserKeys(im.actor.core.modules.encryption.entity.UserKeys) UserKeysGroup(im.actor.core.modules.encryption.entity.UserKeysGroup) Function(im.actor.runtime.function.Function) ApiEncryptionKeyGroup(im.actor.core.api.ApiEncryptionKeyGroup)

Example 5 with ApiUserOutPeer

use of im.actor.core.api.ApiUserOutPeer in project actor-platform by actorapp.

the class PresenceActor method onCheckQueue.

private void onCheckQueue() {
    if (isRequesting) {
        return;
    }
    if (pendingPeers.size() == 0) {
        return;
    }
    ArrayList<Peer> destPeers = new ArrayList<>(pendingPeers);
    pendingPeers.clear();
    ArrayList<ApiUserOutPeer> outUserPeers = new ArrayList<>();
    ArrayList<ApiGroupOutPeer> outGroupPeers = new ArrayList<>();
    for (Peer p : destPeers) {
        if (p.getPeerType() == PeerType.GROUP) {
            Group g = getGroup(p.getPeerId());
            if (g != null) {
                outGroupPeers.add(new ApiGroupOutPeer(p.getPeerId(), g.getAccessHash()));
            }
        } else if (p.getPeerType() == PeerType.PRIVATE) {
            User u = getUser(p.getPeerId());
            if (u != null) {
                outUserPeers.add(new ApiUserOutPeer(p.getPeerId(), u.getAccessHash()));
            }
        }
    }
    ArrayList<Promise<ResponseVoid>> requests = new ArrayList<>();
    if (outUserPeers.size() > 0) {
        requests.add(api(new RequestSubscribeToOnline(outUserPeers)));
    }
    if (outGroupPeers.size() > 0) {
        requests.add(api(new RequestSubscribeToGroupOnline(outGroupPeers)));
    }
    if (requests.size() > 0) {
        isRequesting = true;
        PromisesArray.ofPromises(requests).zip().then(responseVoids -> {
            isRequesting = false;
            onCheckQueue();
        }).failure(e -> {
            isRequesting = false;
            onCheckQueue();
        });
    }
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ActorCancellable(im.actor.runtime.actors.ActorCancellable) ActorCreator(im.actor.runtime.actors.ActorCreator) Promise(im.actor.runtime.promise.Promise) UserPresence(im.actor.core.viewmodel.UserPresence) HashMap(java.util.HashMap) GroupVM(im.actor.core.viewmodel.GroupVM) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestSubscribeToOnline(im.actor.core.api.rpc.RequestSubscribeToOnline) User(im.actor.core.entity.User) BusSubscriber(im.actor.runtime.eventbus.BusSubscriber) PromisesArray(im.actor.runtime.promise.PromisesArray) ActorSystem(im.actor.runtime.actors.ActorSystem) PeerChatOpened(im.actor.core.events.PeerChatOpened) ActorRef(im.actor.runtime.actors.ActorRef) GroupType(im.actor.core.entity.GroupType) PeerType(im.actor.core.entity.PeerType) UserVisible(im.actor.core.events.UserVisible) Verified(im.actor.runtime.annotations.Verified) Group(im.actor.core.entity.Group) PeerInfoOpened(im.actor.core.events.PeerInfoOpened) RequestSubscribeToGroupOnline(im.actor.core.api.rpc.RequestSubscribeToGroupOnline) List(java.util.List) ModuleActor(im.actor.core.modules.ModuleActor) UserVM(im.actor.core.viewmodel.UserVM) Consumer(im.actor.runtime.function.Consumer) Peer(im.actor.core.entity.Peer) Props(im.actor.runtime.actors.Props) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ResponseVoid(im.actor.core.api.rpc.ResponseVoid) Log(im.actor.runtime.Log) Event(im.actor.runtime.eventbus.Event) NewSessionCreated(im.actor.core.events.NewSessionCreated) Group(im.actor.core.entity.Group) User(im.actor.core.entity.User) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) Peer(im.actor.core.entity.Peer) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ArrayList(java.util.ArrayList) Promise(im.actor.runtime.promise.Promise) RequestSubscribeToGroupOnline(im.actor.core.api.rpc.RequestSubscribeToGroupOnline) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) RequestSubscribeToOnline(im.actor.core.api.rpc.RequestSubscribeToOnline)

Aggregations

ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)8 ArrayList (java.util.ArrayList)6 Function (im.actor.runtime.function.Function)5 Promise (im.actor.runtime.promise.Promise)5 ApiUser (im.actor.core.api.ApiUser)4 User (im.actor.core.entity.User)4 ModuleActor (im.actor.core.modules.ModuleActor)4 ModuleContext (im.actor.core.modules.ModuleContext)4 Log (im.actor.runtime.Log)4 HashSet (java.util.HashSet)4 List (java.util.List)4 UserKeys (im.actor.core.modules.encryption.entity.UserKeys)3 UserKeysGroup (im.actor.core.modules.encryption.entity.UserKeysGroup)3 Void (im.actor.runtime.actors.messages.Void)3 Tuple2 (im.actor.runtime.function.Tuple2)3 PromisesArray (im.actor.runtime.promise.PromisesArray)3 ApiEncryptionKey (im.actor.core.api.ApiEncryptionKey)2 ApiEncryptionKeySignature (im.actor.core.api.ApiEncryptionKeySignature)2 ApiGroupOutPeer (im.actor.core.api.ApiGroupOutPeer)2 RequestLoadPublicKey (im.actor.core.api.rpc.RequestLoadPublicKey)2