Search in sources :

Example 6 with Function

use of im.actor.runtime.function.Function 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 7 with Function

use of im.actor.runtime.function.Function 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 8 with Function

use of im.actor.runtime.function.Function in project actor-platform by actorapp.

the class UserRouter method onLoadFullUser.

//
// Users changed
//
@Verified
private void onLoadFullUser(int uid) {
    if (requestedFullUsers.contains(uid)) {
        return;
    }
    requestedFullUsers.add(uid);
    freeze();
    users().getValueAsync(uid).flatMap((Function<User, Promise<Tuple2<ResponseLoadFullUsers, User>>>) u -> {
        if (!u.isHaveExtension()) {
            ArrayList<ApiUserOutPeer> users = new ArrayList<>();
            users.add(new ApiUserOutPeer(u.getUid(), u.getAccessHash()));
            return api(new RequestLoadFullUsers(users)).map(responseLoadFullUsers -> new Tuple2<>(responseLoadFullUsers, u));
        } else {
            if (!getUserVM(uid).isInPhoneBook().get()) {
                return checkIsInPhoneBook(u).flatMap(new Function<Void, Promise<Tuple2<ResponseLoadFullUsers, User>>>() {

                    @Override
                    public Promise<Tuple2<ResponseLoadFullUsers, User>> apply(Void aVoid) {
                        return Promise.failure(new RuntimeException("Already loaded"));
                    }
                });
            } else {
                return Promise.failure(new RuntimeException("Already loaded"));
            }
        }
    }).then(r -> {
        User upd = r.getT2().updateExt(r.getT1().getFullUsers().get(0));
        users().addOrUpdateItem(upd);
    }).chain(r -> checkIsInPhoneBook(r.getT2().updateExt(r.getT1().getFullUsers().get(0)))).after((r, e) -> unfreeze());
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ContactRecord(im.actor.core.entity.ContactRecord) PhoneBookContact(im.actor.core.entity.PhoneBookContact) ApiUser(im.actor.core.api.ApiUser) UpdateUserContactsChanged(im.actor.core.api.updates.UpdateUserContactsChanged) ApiMapValue(im.actor.core.api.ApiMapValue) ApiBotCommand(im.actor.core.api.ApiBotCommand) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) PromisesArray(im.actor.runtime.promise.PromisesArray) RouterLoadFullUser(im.actor.core.modules.users.router.entity.RouterLoadFullUser) UpdateContactRegistered(im.actor.core.api.updates.UpdateContactRegistered) UpdateUserNameChanged(im.actor.core.api.updates.UpdateUserNameChanged) ContactRecordType(im.actor.core.entity.ContactRecordType) Tuple2(im.actor.runtime.function.Tuple2) UpdateUserBlocked(im.actor.core.api.updates.UpdateUserBlocked) Void(im.actor.runtime.actors.messages.Void) Verified(im.actor.runtime.annotations.Verified) UserPhone(im.actor.core.viewmodel.UserPhone) ResponseLoadFullUsers(im.actor.core.api.rpc.ResponseLoadFullUsers) UpdateUserAvatarChanged(im.actor.core.api.updates.UpdateUserAvatarChanged) List(java.util.List) UpdateUserAboutChanged(im.actor.core.api.updates.UpdateUserAboutChanged) ModuleActor(im.actor.core.modules.ModuleActor) ArrayListUserPhone(im.actor.core.viewmodel.generics.ArrayListUserPhone) UpdateUserFullExtChanged(im.actor.core.api.updates.UpdateUserFullExtChanged) UserEmail(im.actor.core.viewmodel.UserEmail) Peer(im.actor.core.entity.Peer) UpdateUserExtChanged(im.actor.core.api.updates.UpdateUserExtChanged) MessageState(im.actor.core.entity.MessageState) Message(im.actor.core.entity.Message) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ApiAvatar(im.actor.core.api.ApiAvatar) ApiContactRecord(im.actor.core.api.ApiContactRecord) UpdateUserTimeZoneChanged(im.actor.core.api.updates.UpdateUserTimeZoneChanged) Function(im.actor.runtime.function.Function) ServiceUserRegistered(im.actor.core.entity.content.ServiceUserRegistered) PhoneBookProvider(im.actor.core.providers.PhoneBookProvider) Promise(im.actor.runtime.promise.Promise) UpdateUserBotCommandsChanged(im.actor.core.api.updates.UpdateUserBotCommandsChanged) UpdateUserNickChanged(im.actor.core.api.updates.UpdateUserNickChanged) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) User(im.actor.core.entity.User) UpdateUserLocalNameChanged(im.actor.core.api.updates.UpdateUserLocalNameChanged) ArrayListUserEmail(im.actor.core.viewmodel.generics.ArrayListUserEmail) JavaUtil.equalsE(im.actor.core.util.JavaUtil.equalsE) UpdateUserUnblocked(im.actor.core.api.updates.UpdateUserUnblocked) RequestLoadFullUsers(im.actor.core.api.rpc.RequestLoadFullUsers) ContactsSyncActor(im.actor.core.modules.contacts.ContactsSyncActor) UserVM(im.actor.core.viewmodel.UserVM) RouterFetchMissingUsers(im.actor.core.modules.users.router.entity.RouterFetchMissingUsers) BookImportStorage(im.actor.core.modules.contacts.entity.BookImportStorage) RouterApplyUsers(im.actor.core.modules.users.router.entity.RouterApplyUsers) UpdateUserPreferredLanguagesChanged(im.actor.core.api.updates.UpdateUserPreferredLanguagesChanged) Log(im.actor.runtime.Log) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone) Update(im.actor.core.network.parser.Update) RouterUserUpdate(im.actor.core.modules.users.router.entity.RouterUserUpdate) RequestLoadFullUsers(im.actor.core.api.rpc.RequestLoadFullUsers) ApiUser(im.actor.core.api.ApiUser) RouterLoadFullUser(im.actor.core.modules.users.router.entity.RouterLoadFullUser) User(im.actor.core.entity.User) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ArrayList(java.util.ArrayList) Function(im.actor.runtime.function.Function) Promise(im.actor.runtime.promise.Promise) Tuple2(im.actor.runtime.function.Tuple2) Void(im.actor.runtime.actors.messages.Void) ResponseLoadFullUsers(im.actor.core.api.rpc.ResponseLoadFullUsers) Verified(im.actor.runtime.annotations.Verified)

Aggregations

Function (im.actor.runtime.function.Function)8 Tuple2 (im.actor.runtime.function.Tuple2)6 Promise (im.actor.runtime.promise.Promise)6 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)5 ArrayList (java.util.ArrayList)5 User (im.actor.core.entity.User)4 UserKeysGroup (im.actor.core.modules.encryption.entity.UserKeysGroup)4 ApiAvatar (im.actor.core.api.ApiAvatar)3 ApiMapValue (im.actor.core.api.ApiMapValue)3 ModuleActor (im.actor.core.modules.ModuleActor)3 ModuleContext (im.actor.core.modules.ModuleContext)3 UserKeys (im.actor.core.modules.encryption.entity.UserKeys)3 List (java.util.List)3 ApiBotCommand (im.actor.core.api.ApiBotCommand)2 ApiContactRecord (im.actor.core.api.ApiContactRecord)2 ApiEncryptionKey (im.actor.core.api.ApiEncryptionKey)2 ApiEncryptionKeySignature (im.actor.core.api.ApiEncryptionKeySignature)2 ApiUser (im.actor.core.api.ApiUser)2 RequestLoadFullUsers (im.actor.core.api.rpc.RequestLoadFullUsers)2 RequestLoadPublicKey (im.actor.core.api.rpc.RequestLoadPublicKey)2