use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class GroupsModule method loadMembers.
public Promise<GroupMembersSlice> loadMembers(int gid, int limit, byte[] next) {
return getGroups().getValueAsync(gid).flatMap(group -> api(new RequestLoadMembers(new ApiGroupOutPeer(group.getGroupId(), group.getAccessHash()), limit, next))).chain(r -> updates().loadRequiredPeers(r.getUsers(), new ArrayList<>())).map(r -> {
ArrayList<GroupMember> members = new ArrayList<>();
for (ApiMember p : r.getMembers()) {
boolean isAdmin = p.isAdmin() != null ? p.isAdmin() : false;
members.add(new GroupMember(p.getUid(), p.getInviterUid(), p.getInviterUid(), isAdmin));
}
return new GroupMembersSlice(members, r.getNext());
});
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class GroupRouter method onRequestLoadFullGroup.
private void onRequestLoadFullGroup(int gid) {
if (requestedFullGroups.contains(gid)) {
return;
}
requestedFullGroups.add(gid);
freeze();
groups().getValueAsync(gid).flatMap(new Function<Group, Promise<Group>>() {
@Override
public Promise<Group> apply(Group group) {
if (!group.isHaveExtension()) {
ArrayList<ApiGroupOutPeer> groups = new ArrayList<>();
groups.add(new ApiGroupOutPeer(gid, group.getAccessHash()));
return api(new RequestLoadFullGroups(groups)).map(r -> group.updateExt(r.getGroups().get(0)));
} else {
return Promise.failure(new RuntimeException("Already loaded"));
}
}
}).then(r -> groups().addOrUpdateItem(r)).after((r, e) -> unfreeze());
}
use of im.actor.runtime.promise.Promise 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());
}
});
}
});
}
use of im.actor.runtime.promise.Promise 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.promise.Promise 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();
});
}
}
Aggregations