Search in sources :

Example 11 with Promise

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());
    });
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ApiGroupType(im.actor.core.api.ApiGroupType) RequestKickUser(im.actor.core.api.rpc.RequestKickUser) RequestLoadAdminSettings(im.actor.core.api.rpc.RequestLoadAdminSettings) ResponseInviteUrl(im.actor.core.api.rpc.ResponseInviteUrl) RandomUtils(im.actor.core.util.RandomUtils) RequestEditGroupShortName(im.actor.core.api.rpc.RequestEditGroupShortName) RequestInviteUser(im.actor.core.api.rpc.RequestInviteUser) RequestTransferOwnership(im.actor.core.api.rpc.RequestTransferOwnership) RequestEditGroupTitle(im.actor.core.api.rpc.RequestEditGroupTitle) RequestJoinGroupByPeer(im.actor.core.api.rpc.RequestJoinGroupByPeer) ApiSupportConfiguration(im.actor.core.modules.api.ApiSupportConfiguration) ApiAdminSettings(im.actor.core.api.ApiAdminSettings) RequestGetIntegrationToken(im.actor.core.api.rpc.RequestGetIntegrationToken) Void(im.actor.runtime.actors.messages.Void) RequestRevokeInviteUrl(im.actor.core.api.rpc.RequestRevokeInviteUrl) GroupMember(im.actor.core.entity.GroupMember) ApiOutPeer(im.actor.core.api.ApiOutPeer) RequestCreateGroup(im.actor.core.api.rpc.RequestCreateGroup) MVVMCollection(im.actor.runtime.mvvm.MVVMCollection) ActorSystem.system(im.actor.runtime.actors.ActorSystem.system) RequestLeaveAndDelete(im.actor.core.api.rpc.RequestLeaveAndDelete) GroupAvatarVM(im.actor.core.viewmodel.GroupAvatarVM) List(java.util.List) RequestEditGroupAbout(im.actor.core.api.rpc.RequestEditGroupAbout) Peer(im.actor.core.entity.Peer) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) Function(im.actor.runtime.function.Function) ApiPeerType(im.actor.core.api.ApiPeerType) RequestMakeUserAdminObsolete(im.actor.core.api.rpc.RequestMakeUserAdminObsolete) Promise(im.actor.runtime.promise.Promise) HashMap(java.util.HashMap) GroupVM(im.actor.core.viewmodel.GroupVM) ArrayList(java.util.ArrayList) AbsModule(im.actor.core.modules.AbsModule) User(im.actor.core.entity.User) BusSubscriber(im.actor.runtime.eventbus.BusSubscriber) GroupAvatarChangeActor(im.actor.core.modules.profile.avatar.GroupAvatarChangeActor) RequestSaveAdminSettings(im.actor.core.api.rpc.RequestSaveAdminSettings) RequestRevokeIntegrationToken(im.actor.core.api.rpc.RequestRevokeIntegrationToken) GroupMembersSlice(im.actor.core.entity.GroupMembersSlice) PeerChatOpened(im.actor.core.events.PeerChatOpened) ActorRef(im.actor.runtime.actors.ActorRef) RequestDismissUserAdmin(im.actor.core.api.rpc.RequestDismissUserAdmin) PeerType(im.actor.core.entity.PeerType) RequestLeaveGroup(im.actor.core.api.rpc.RequestLeaveGroup) RequestLoadMembers(im.actor.core.api.rpc.RequestLoadMembers) RequestShareHistory(im.actor.core.api.rpc.RequestShareHistory) RequestJoinGroup(im.actor.core.api.rpc.RequestJoinGroup) ApiMember(im.actor.core.api.ApiMember) Group(im.actor.core.entity.Group) PeerInfoOpened(im.actor.core.events.PeerInfoOpened) GroupRouterInt(im.actor.core.modules.groups.router.GroupRouterInt) RequestDeleteGroup(im.actor.core.api.rpc.RequestDeleteGroup) Storage(im.actor.runtime.Storage) ResponseIntegrationToken(im.actor.core.api.rpc.ResponseIntegrationToken) RequestGetGroupInviteUrl(im.actor.core.api.rpc.RequestGetGroupInviteUrl) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ResponseVoid(im.actor.core.api.rpc.ResponseVoid) GroupPermissions(im.actor.core.entity.GroupPermissions) KeyValueEngine(im.actor.runtime.storage.KeyValueEngine) Event(im.actor.runtime.eventbus.Event) RequestEditGroupTopic(im.actor.core.api.rpc.RequestEditGroupTopic) Promises(im.actor.runtime.promise.Promises) GroupMember(im.actor.core.entity.GroupMember) RequestLoadMembers(im.actor.core.api.rpc.RequestLoadMembers) ArrayList(java.util.ArrayList) GroupMembersSlice(im.actor.core.entity.GroupMembersSlice) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ApiMember(im.actor.core.api.ApiMember)

Example 12 with Promise

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());
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) RouterApplyGroups(im.actor.core.modules.groups.router.entity.RouterApplyGroups) Promise(im.actor.runtime.promise.Promise) UpdateGroupAvatarChanged(im.actor.core.api.updates.UpdateGroupAvatarChanged) RouterLoadFullGroup(im.actor.core.modules.groups.router.entity.RouterLoadFullGroup) UpdateGroupFullPermissionsChanged(im.actor.core.api.updates.UpdateGroupFullPermissionsChanged) RouterFetchMissingGroups(im.actor.core.modules.groups.router.entity.RouterFetchMissingGroups) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateGroupMemberChanged(im.actor.core.api.updates.UpdateGroupMemberChanged) ApiMapValue(im.actor.core.api.ApiMapValue) PromisesArray(im.actor.runtime.promise.PromisesArray) UpdateGroupMemberAdminChanged(im.actor.core.api.updates.UpdateGroupMemberAdminChanged) UpdateGroupTitleChanged(im.actor.core.api.updates.UpdateGroupTitleChanged) UpdateGroupTopicChanged(im.actor.core.api.updates.UpdateGroupTopicChanged) RouterInt(im.actor.core.modules.messaging.router.RouterInt) RouterGroupUpdate(im.actor.core.modules.groups.router.entity.RouterGroupUpdate) UpdateGroupMemberDiff(im.actor.core.api.updates.UpdateGroupMemberDiff) Tuple2(im.actor.runtime.function.Tuple2) ApiGroup(im.actor.core.api.ApiGroup) UpdateGroupDeleted(im.actor.core.api.updates.UpdateGroupDeleted) Void(im.actor.runtime.actors.messages.Void) UpdateGroupOwnerChanged(im.actor.core.api.updates.UpdateGroupOwnerChanged) Verified(im.actor.runtime.annotations.Verified) ApiMember(im.actor.core.api.ApiMember) Group(im.actor.core.entity.Group) UpdateGroupAboutChanged(im.actor.core.api.updates.UpdateGroupAboutChanged) UpdateGroupFullExtChanged(im.actor.core.api.updates.UpdateGroupFullExtChanged) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ModuleActor(im.actor.core.modules.ModuleActor) UpdateGroupPermissionsChanged(im.actor.core.api.updates.UpdateGroupPermissionsChanged) UpdateGroupMembersCountChanged(im.actor.core.api.updates.UpdateGroupMembersCountChanged) UpdateGroupMembersBecameAsync(im.actor.core.api.updates.UpdateGroupMembersBecameAsync) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ApiAvatar(im.actor.core.api.ApiAvatar) UpdateGroupHistoryShared(im.actor.core.api.updates.UpdateGroupHistoryShared) UpdateGroupShortNameChanged(im.actor.core.api.updates.UpdateGroupShortNameChanged) Function(im.actor.runtime.function.Function) RequestLoadFullGroups(im.actor.core.api.rpc.RequestLoadFullGroups) UpdateGroupExtChanged(im.actor.core.api.updates.UpdateGroupExtChanged) Update(im.actor.core.network.parser.Update) UpdateGroupMembersUpdated(im.actor.core.api.updates.UpdateGroupMembersUpdated) RouterLoadFullGroup(im.actor.core.modules.groups.router.entity.RouterLoadFullGroup) ApiGroup(im.actor.core.api.ApiGroup) Group(im.actor.core.entity.Group) Promise(im.actor.runtime.promise.Promise) RequestLoadFullGroups(im.actor.core.api.rpc.RequestLoadFullGroups) ArrayList(java.util.ArrayList) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer)

Example 13 with Promise

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());
                }
            });
        }
    });
}
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 14 with Promise

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;
                }
            });
        }
    });
}
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 15 with Promise

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

Promise (im.actor.runtime.promise.Promise)19 Function (im.actor.runtime.function.Function)10 ArrayList (java.util.ArrayList)9 ModuleContext (im.actor.core.modules.ModuleContext)8 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)7 User (im.actor.core.entity.User)7 Void (im.actor.runtime.actors.messages.Void)7 List (java.util.List)7 Peer (im.actor.core.entity.Peer)6 ModuleActor (im.actor.core.modules.ModuleActor)6 Tuple2 (im.actor.runtime.function.Tuple2)6 Update (im.actor.core.network.parser.Update)5 Log (im.actor.runtime.Log)5 HashSet (java.util.HashSet)5 ApiGroupOutPeer (im.actor.core.api.ApiGroupOutPeer)4 Group (im.actor.core.entity.Group)4 PromisesArray (im.actor.runtime.promise.PromisesArray)4 ApiAvatar (im.actor.core.api.ApiAvatar)3 ApiMapValue (im.actor.core.api.ApiMapValue)3 ApiUser (im.actor.core.api.ApiUser)3