Search in sources :

Example 1 with ResponseGetContacts

use of im.actor.core.api.rpc.ResponseGetContacts in project actor-platform by actorapp.

the class ContactsSyncActor method performSync.

public void performSync() {
    if (ENABLE_LOG) {
        Log.d(TAG, "Checking sync");
    }
    if (isInProgress) {
        if (ENABLE_LOG) {
            Log.d(TAG, "Sync in progress, invalidating current sync");
        }
        isInvalidated = true;
        return;
    }
    isInProgress = true;
    isInvalidated = false;
    if (ENABLE_LOG) {
        Log.d(TAG, "Starting sync");
    }
    Integer[] uids = contacts.toArray(new Integer[contacts.size()]);
    Arrays.sort(uids);
    String hash = "";
    for (long u : uids) {
        if (hash.length() != 0) {
            hash += ",";
        }
        hash += u;
    }
    byte[] hashData;
    try {
        hashData = hash.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return;
    }
    String hashValue = Crypto.hex(Crypto.SHA256(hashData));
    if (ENABLE_LOG) {
        Log.d(TAG, "Performing sync with hash: " + hashValue);
        Log.d(TAG, "Performing sync with uids: " + hash);
    }
    request(new RequestGetContacts(hashValue, ApiSupportConfiguration.OPTIMIZATIONS), new RpcCallback<ResponseGetContacts>() {

        @Override
        public void onResult(ResponseGetContacts response) {
            if (ENABLE_LOG) {
                Log.d(TAG, "Sync received " + (response.getUsers().size() + response.getUserPeers().size()) + " contacts");
            }
            if (response.getUserPeers().size() > 0) {
                updates().loadRequiredPeers(response.getUserPeers(), new ArrayList<>()).then(v -> onContactsLoaded(response));
            } else {
                updates().applyRelatedData(response.getUsers()).then(v -> onContactsLoaded(response));
            }
        }

        @Override
        public void onError(RpcException e) {
            isInProgress = false;
            e.printStackTrace();
        }
    });
}
Also used : ApiSupportConfiguration(im.actor.core.modules.api.ApiSupportConfiguration) ModuleContext(im.actor.core.modules.ModuleContext) Arrays(java.util.Arrays) Void(im.actor.runtime.actors.messages.Void) ResponseGetContacts(im.actor.core.api.rpc.ResponseGetContacts) IOException(java.io.IOException) ApiUser(im.actor.core.api.ApiUser) RpcException(im.actor.core.network.RpcException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DataInput(im.actor.runtime.bser.DataInput) List(java.util.List) ModuleActor(im.actor.core.modules.ModuleActor) User(im.actor.core.entity.User) RequestGetContacts(im.actor.core.api.rpc.RequestGetContacts) DataOutput(im.actor.runtime.bser.DataOutput) RpcCallback(im.actor.core.network.RpcCallback) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) Log(im.actor.runtime.Log) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Contact(im.actor.core.entity.Contact) Crypto(im.actor.runtime.Crypto) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestGetContacts(im.actor.core.api.rpc.RequestGetContacts) RpcException(im.actor.core.network.RpcException) ResponseGetContacts(im.actor.core.api.rpc.ResponseGetContacts)

Aggregations

ApiUser (im.actor.core.api.ApiUser)1 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)1 RequestGetContacts (im.actor.core.api.rpc.RequestGetContacts)1 ResponseGetContacts (im.actor.core.api.rpc.ResponseGetContacts)1 Contact (im.actor.core.entity.Contact)1 User (im.actor.core.entity.User)1 ModuleActor (im.actor.core.modules.ModuleActor)1 ModuleContext (im.actor.core.modules.ModuleContext)1 ApiSupportConfiguration (im.actor.core.modules.api.ApiSupportConfiguration)1 RpcCallback (im.actor.core.network.RpcCallback)1 RpcException (im.actor.core.network.RpcException)1 Crypto (im.actor.runtime.Crypto)1 Log (im.actor.runtime.Log)1 Void (im.actor.runtime.actors.messages.Void)1 DataInput (im.actor.runtime.bser.DataInput)1 DataOutput (im.actor.runtime.bser.DataOutput)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1