Search in sources :

Example 1 with UpdateContactsAdded

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

the class UpdateValidator method isCausesInvalidation.

public boolean isCausesInvalidation(Update update) {
    HashSet<Integer> users = new HashSet<>();
    HashSet<Integer> groups = new HashSet<>();
    if (update instanceof UpdateMessage) {
        UpdateMessage updateMessage = (UpdateMessage) update;
        users.add(updateMessage.getSenderUid());
        if (updateMessage.getPeer().getType() == ApiPeerType.GROUP) {
            groups.add(updateMessage.getPeer().getId());
        }
        if (updateMessage.getPeer().getType() == ApiPeerType.PRIVATE) {
            users.add(updateMessage.getPeer().getId());
        }
    } else if (update instanceof UpdateContactRegistered) {
        UpdateContactRegistered contactRegistered = (UpdateContactRegistered) update;
        users.add(contactRegistered.getUid());
    } else if (update instanceof UpdateContactsAdded) {
        users.addAll(((UpdateContactsAdded) update).getUids());
    } else if (update instanceof UpdateContactsRemoved) {
        users.addAll(((UpdateContactsRemoved) update).getUids());
    } else if (update instanceof UpdateUserLocalNameChanged) {
        UpdateUserLocalNameChanged localNameChanged = (UpdateUserLocalNameChanged) update;
        users.add(localNameChanged.getUid());
    } else if (update instanceof UpdateChatGroupsChanged) {
        UpdateChatGroupsChanged changed = (UpdateChatGroupsChanged) update;
        for (ApiDialogGroup group : changed.getDialogs()) {
            for (ApiDialogShort dialog : group.getDialogs()) {
                if (dialog.getPeer().getType() == ApiPeerType.PRIVATE) {
                    users.add(dialog.getPeer().getId());
                } else if (dialog.getPeer().getType() == ApiPeerType.GROUP) {
                    groups.add(dialog.getPeer().getId());
                }
            }
        }
    } else if (update instanceof UpdateGroupMemberChanged) {
        UpdateGroupMemberChanged memberChanged = (UpdateGroupMemberChanged) update;
        groups.add(memberChanged.getGroupId());
    } else if (update instanceof UpdateGroupMemberDiff) {
        UpdateGroupMemberDiff diff = (UpdateGroupMemberDiff) update;
        groups.add(diff.getGroupId());
        for (Integer u : diff.getRemovedUsers()) {
            users.add(u);
        }
        for (ApiMember m : diff.getAddedMembers()) {
            users.add(m.getInviterUid());
            users.add(m.getUid());
        }
    } else if (update instanceof UpdateGroupMembersUpdated) {
        UpdateGroupMembersUpdated u = (UpdateGroupMembersUpdated) update;
        groups.add(u.getGroupId());
        for (ApiMember m : u.getMembers()) {
            users.add(m.getInviterUid());
            users.add(m.getUid());
        }
    } else if (update instanceof UpdateGroupMemberAdminChanged) {
        UpdateGroupMemberAdminChanged u = (UpdateGroupMemberAdminChanged) update;
        users.add(u.getUserId());
        groups.add(u.getGroupId());
    } else if (update instanceof UpdateGroupMembersCountChanged) {
        UpdateGroupMembersCountChanged countChanged = (UpdateGroupMembersCountChanged) update;
        groups.add(countChanged.getGroupId());
    } else if (update instanceof UpdateGroupOwnerChanged) {
        UpdateGroupOwnerChanged ownerChanged = (UpdateGroupOwnerChanged) update;
        groups.add(ownerChanged.getGroupId());
        users.add(ownerChanged.getUserId());
    } else if (update instanceof UpdateGroupFullExtChanged) {
        UpdateGroupFullExtChanged fullExtChanged = (UpdateGroupFullExtChanged) update;
        groups.add(fullExtChanged.getGroupId());
    } else if (update instanceof UpdateGroupExtChanged) {
        UpdateGroupExtChanged extChanged = (UpdateGroupExtChanged) update;
        groups.add(extChanged.getGroupId());
    }
    if (!hasUsers(users)) {
        return true;
    }
    if (!hasGroups(groups)) {
        return true;
    }
    return false;
}
Also used : UpdateContactsAdded(im.actor.core.api.updates.UpdateContactsAdded) UpdateGroupExtChanged(im.actor.core.api.updates.UpdateGroupExtChanged) UpdateMessage(im.actor.core.api.updates.UpdateMessage) UpdateGroupMemberChanged(im.actor.core.api.updates.UpdateGroupMemberChanged) UpdateContactRegistered(im.actor.core.api.updates.UpdateContactRegistered) UpdateGroupFullExtChanged(im.actor.core.api.updates.UpdateGroupFullExtChanged) UpdateGroupMemberDiff(im.actor.core.api.updates.UpdateGroupMemberDiff) ApiMember(im.actor.core.api.ApiMember) UpdateChatGroupsChanged(im.actor.core.api.updates.UpdateChatGroupsChanged) UpdateGroupMemberAdminChanged(im.actor.core.api.updates.UpdateGroupMemberAdminChanged) UpdateGroupMembersCountChanged(im.actor.core.api.updates.UpdateGroupMembersCountChanged) UpdateUserLocalNameChanged(im.actor.core.api.updates.UpdateUserLocalNameChanged) UpdateGroupOwnerChanged(im.actor.core.api.updates.UpdateGroupOwnerChanged) UpdateContactsRemoved(im.actor.core.api.updates.UpdateContactsRemoved) ApiDialogGroup(im.actor.core.api.ApiDialogGroup) UpdateGroupMembersUpdated(im.actor.core.api.updates.UpdateGroupMembersUpdated) HashSet(java.util.HashSet) ApiDialogShort(im.actor.core.api.ApiDialogShort)

Example 2 with UpdateContactsAdded

use of im.actor.core.api.updates.UpdateContactsAdded 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 3 with UpdateContactsAdded

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

the class ContactsModule method addContact.

public Command<Boolean> addContact(final int uid) {
    return callback -> {
        User user = users().getValue(uid);
        if (user == null) {
            runOnUiThread(() -> callback.onError(new RpcInternalException()));
            return;
        }
        request(new RequestAddContact(uid, user.getAccessHash()), new RpcCallback<ResponseSeq>() {

            @Override
            public void onResult(ResponseSeq response) {
                ArrayList<Integer> uids = new ArrayList<>();
                uids.add(uid);
                SeqUpdate update = new SeqUpdate(response.getSeq(), response.getState(), UpdateContactsAdded.HEADER, new UpdateContactsAdded(uids).toByteArray());
                updates().onUpdateReceived(update);
                runOnUiThread(() -> callback.onResult(true));
            }

            @Override
            public void onError(RpcException e) {
                runOnUiThread(() -> callback.onError(new RpcInternalException()));
            }
        });
    };
}
Also used : ResponseSeq(im.actor.core.api.rpc.ResponseSeq) RequestRemoveContact(im.actor.core.api.rpc.RequestRemoveContact) Promise(im.actor.runtime.promise.Promise) ApiUser(im.actor.core.api.ApiUser) ArrayList(java.util.ArrayList) AbsModule(im.actor.core.modules.AbsModule) User(im.actor.core.entity.User) SyncKeyValue(im.actor.runtime.storage.SyncKeyValue) Modules(im.actor.core.modules.Modules) RpcCallback(im.actor.core.network.RpcCallback) ActorRef(im.actor.runtime.actors.ActorRef) ListEngine(im.actor.runtime.storage.ListEngine) Contact(im.actor.core.entity.Contact) ApiSupportConfiguration(im.actor.core.modules.api.ApiSupportConfiguration) Command(im.actor.core.viewmodel.Command) RpcInternalException(im.actor.core.network.RpcInternalException) RequestSearchContacts(im.actor.core.api.rpc.RequestSearchContacts) UpdateContactsAdded(im.actor.core.api.updates.UpdateContactsAdded) UpdateContactsRemoved(im.actor.core.api.updates.UpdateContactsRemoved) ApiOutPeer(im.actor.core.api.ApiOutPeer) ActorSystem.system(im.actor.runtime.actors.ActorSystem.system) RpcException(im.actor.core.network.RpcException) Storage(im.actor.runtime.Storage) UserVM(im.actor.core.viewmodel.UserVM) Props(im.actor.runtime.actors.Props) RequestAddContact(im.actor.core.api.rpc.RequestAddContact) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) SeqUpdate(im.actor.core.api.base.SeqUpdate) UpdateContactsAdded(im.actor.core.api.updates.UpdateContactsAdded) ApiUser(im.actor.core.api.ApiUser) User(im.actor.core.entity.User) SeqUpdate(im.actor.core.api.base.SeqUpdate) RpcInternalException(im.actor.core.network.RpcInternalException) ResponseSeq(im.actor.core.api.rpc.ResponseSeq) RpcException(im.actor.core.network.RpcException) ArrayList(java.util.ArrayList) RpcCallback(im.actor.core.network.RpcCallback) RequestAddContact(im.actor.core.api.rpc.RequestAddContact)

Example 4 with UpdateContactsAdded

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

the class ContactsProcessor method process.

@Override
public Promise<Void> process(Update update) {
    if (update instanceof UpdateContactsAdded) {
        UpdateContactsAdded contactsAdded = (UpdateContactsAdded) update;
        int[] uids = new int[contactsAdded.getUids().size()];
        for (int i = 0; i < uids.length; i++) {
            uids[i] = contactsAdded.getUids().get(i);
        }
        context.getContactsModule().getContactSyncActor().send(new ContactsSyncActor.ContactsAdded(uids));
        return Promise.success(null);
    } else if (update instanceof UpdateContactsRemoved) {
        UpdateContactsRemoved contactsRemoved = (UpdateContactsRemoved) update;
        int[] uids = new int[contactsRemoved.getUids().size()];
        for (int i = 0; i < uids.length; i++) {
            uids[i] = contactsRemoved.getUids().get(i);
        }
        context.getContactsModule().getContactSyncActor().send(new ContactsSyncActor.ContactsRemoved(uids));
        return Promise.success(null);
    }
    return null;
}
Also used : UpdateContactsAdded(im.actor.core.api.updates.UpdateContactsAdded) UpdateContactsRemoved(im.actor.core.api.updates.UpdateContactsRemoved) UpdateContactsRemoved(im.actor.core.api.updates.UpdateContactsRemoved)

Aggregations

UpdateContactsAdded (im.actor.core.api.updates.UpdateContactsAdded)4 UpdateContactsRemoved (im.actor.core.api.updates.UpdateContactsRemoved)3 ApiUser (im.actor.core.api.ApiUser)2 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)2 ApiSupportConfiguration (im.actor.core.modules.api.ApiSupportConfiguration)2 RpcCallback (im.actor.core.network.RpcCallback)2 RpcException (im.actor.core.network.RpcException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ApiDialogGroup (im.actor.core.api.ApiDialogGroup)1 ApiDialogShort (im.actor.core.api.ApiDialogShort)1 ApiEmailToImport (im.actor.core.api.ApiEmailToImport)1 ApiMember (im.actor.core.api.ApiMember)1 ApiOutPeer (im.actor.core.api.ApiOutPeer)1 ApiPhoneToImport (im.actor.core.api.ApiPhoneToImport)1 FatSeqUpdate (im.actor.core.api.base.FatSeqUpdate)1 SeqUpdate (im.actor.core.api.base.SeqUpdate)1 RequestAddContact (im.actor.core.api.rpc.RequestAddContact)1 RequestImportContacts (im.actor.core.api.rpc.RequestImportContacts)1 RequestRemoveContact (im.actor.core.api.rpc.RequestRemoveContact)1