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