Search in sources :

Example 1 with RpcCallback

use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.

the class Authentication method requestStartEmailAuth.

@Deprecated
public Command<AuthState> requestStartEmailAuth(final String email) {
    return callback -> {
        ArrayList<String> langs1 = new ArrayList<>();
        for (String s : modules.getConfiguration().getPreferredLanguages()) {
            langs1.add(s);
        }
        request(new RequestStartEmailAuth(email, apiConfiguration.getAppId(), apiConfiguration.getAppKey(), deviceHash, apiConfiguration.getDeviceTitle(), modules.getConfiguration().getTimeZone(), langs1), new RpcCallback<ResponseStartEmailAuth>() {

            @Override
            public void onResult(ResponseStartEmailAuth response) {
                modules.getPreferences().putString(KEY_EMAIL, email);
                modules.getPreferences().putString(KEY_TRANSACTION_HASH, response.getTransactionHash());
                ApiEmailActivationType emailActivationType = response.getActivationType();
                if (emailActivationType.equals(ApiEmailActivationType.OAUTH2)) {
                    state = AuthState.GET_OAUTH_PARAMS;
                } else if (emailActivationType.equals(ApiEmailActivationType.CODE)) {
                    state = AuthState.CODE_VALIDATION_EMAIL;
                } else if (emailActivationType.equals(ApiEmailActivationType.PASSWORD)) {
                    state = AuthState.PASSWORD_VALIDATION;
                } else {
                    state = AuthState.CODE_VALIDATION_EMAIL;
                }
                Runtime.postToMainThread(() -> callback.onResult(state));
            }

            @Override
            public void onError(final RpcException e) {
                Runtime.postToMainThread(() -> {
                    Log.e(TAG, e);
                    callback.onError(e);
                });
            }
        });
    };
}
Also used : ApiPhoneActivationType(im.actor.core.api.ApiPhoneActivationType) ResponseStartEmailAuth(im.actor.core.api.rpc.ResponseStartEmailAuth) RequestSendCodeByPhoneCall(im.actor.core.api.rpc.RequestSendCodeByPhoneCall) RequestSignUp(im.actor.core.api.rpc.RequestSignUp) Promise(im.actor.runtime.promise.Promise) Request(im.actor.core.network.parser.Request) PromiseFunc(im.actor.runtime.promise.PromiseFunc) ResponseStartPhoneAuth(im.actor.core.api.rpc.ResponseStartPhoneAuth) ArrayList(java.util.ArrayList) AbsModule(im.actor.core.modules.AbsModule) AuthCodeRes(im.actor.core.entity.AuthCodeRes) User(im.actor.core.entity.User) RequestGetOAuth2Params(im.actor.core.api.rpc.RequestGetOAuth2Params) Response(im.actor.core.network.parser.Response) Modules(im.actor.core.modules.Modules) im.actor.runtime(im.actor.runtime) RequestValidateCode(im.actor.core.api.rpc.RequestValidateCode) RpcCallback(im.actor.core.network.RpcCallback) ApiEmailActivationType(im.actor.core.api.ApiEmailActivationType) CommandCallback(im.actor.core.viewmodel.CommandCallback) Sex(im.actor.core.entity.Sex) RequestStartEmailAuth(im.actor.core.api.rpc.RequestStartEmailAuth) Command(im.actor.core.viewmodel.Command) AuthRes(im.actor.core.entity.AuthRes) ResponseStartUsernameAuth(im.actor.core.api.rpc.ResponseStartUsernameAuth) ApiConfiguration(im.actor.core.ApiConfiguration) ResponseGetOAuth2Params(im.actor.core.api.rpc.ResponseGetOAuth2Params) AuthStartRes(im.actor.core.entity.AuthStartRes) ResponseAuth(im.actor.core.api.rpc.ResponseAuth) IOException(java.io.IOException) AuthState(im.actor.core.AuthState) Runtime(im.actor.runtime.Runtime) RequestStartUsernameAuth(im.actor.core.api.rpc.RequestStartUsernameAuth) RpcException(im.actor.core.network.RpcException) ConnectionEndpoint(im.actor.runtime.mtproto.ConnectionEndpoint) TrustedKey(im.actor.core.network.TrustedKey) RequestStartPhoneAuth(im.actor.core.api.rpc.RequestStartPhoneAuth) ApiSex(im.actor.core.api.ApiSex) Endpoints(im.actor.core.network.Endpoints) ConnectionEndpointArray(im.actor.runtime.mtproto.ConnectionEndpointArray) ResponseVoid(im.actor.core.api.rpc.ResponseVoid) RequestCompleteOAuth2(im.actor.core.api.rpc.RequestCompleteOAuth2) RequestValidatePassword(im.actor.core.api.rpc.RequestValidatePassword) RequestStartAnonymousAuth(im.actor.core.api.rpc.RequestStartAnonymousAuth) AuthMode(im.actor.core.entity.AuthMode) ApiEmailActivationType(im.actor.core.api.ApiEmailActivationType) RequestStartEmailAuth(im.actor.core.api.rpc.RequestStartEmailAuth) ResponseStartEmailAuth(im.actor.core.api.rpc.ResponseStartEmailAuth) RpcException(im.actor.core.network.RpcException) ArrayList(java.util.ArrayList) RpcCallback(im.actor.core.network.RpcCallback)

Example 2 with RpcCallback

use of im.actor.core.network.RpcCallback 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 RpcCallback

use of im.actor.core.network.RpcCallback 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)

Example 4 with RpcCallback

use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.

the class ArchivedDialogsActor method onLoadMore.

private void onLoadMore(boolean init, RpcCallback<ResponseLoadArchived> callback) {
    if (init || isLoading) {
        //
        if (lastCallback != null) {
            lastCallback.onError(new RpcException(TAG, 0, "callback replaced", false, null));
        }
    }
    lastCallback = callback;
    if (isLoading && !init) {
        return;
    }
    if (init) {
        if (lastRequest != -1) {
            cancelRequest(lastRequest);
        }
        nextOffset = null;
    }
    isLoading = true;
    Log.d(TAG, "Loading archived dialogs");
    api(new RequestLoadArchived(nextOffset, LIMIT, ApiSupportConfiguration.OPTIMIZATIONS)).chain(r -> updates().applyRelatedData(r.getUsers(), r.getGroups())).chain(r -> updates().loadRequiredPeers(r.getUserPeers(), r.getGroupPeers())).then(r -> onLoadedMore(r)).failure(e -> lastCallback.onError((RpcException) e));
}
Also used : RequestLoadArchived(im.actor.core.api.rpc.RequestLoadArchived) ApiSupportConfiguration(im.actor.core.modules.api.ApiSupportConfiguration) ModuleContext(im.actor.core.modules.ModuleContext) ModuleActor(im.actor.core.modules.ModuleActor) Consumer(im.actor.runtime.function.Consumer) Void(im.actor.runtime.actors.messages.Void) RpcCallback(im.actor.core.network.RpcCallback) Log(im.actor.runtime.Log) ResponseLoadArchived(im.actor.core.api.rpc.ResponseLoadArchived) RpcException(im.actor.core.network.RpcException) RpcException(im.actor.core.network.RpcException) RequestLoadArchived(im.actor.core.api.rpc.RequestLoadArchived)

Example 5 with RpcCallback

use of im.actor.core.network.RpcCallback in project actor-platform by actorapp.

the class Authentication method requestCompleteOauth.

@Deprecated
public Command<AuthState> requestCompleteOauth(final String code) {
    return callback -> request(new RequestCompleteOAuth2(modules.getPreferences().getString(KEY_TRANSACTION_HASH), code), new RpcCallback<ResponseAuth>() {

        @Override
        public void onResult(ResponseAuth response) {
            onLoggedIn(callback, response);
        }

        @Override
        public void onError(final RpcException e) {
            if ("EMAIL_EXPIRED".equals(e.getTag())) {
                resetAuth();
            } else if ("EMAIL_UNOCCUPIED".equals(e.getTag())) {
                state = AuthState.SIGN_UP;
                callback.onResult(AuthState.SIGN_UP);
                return;
            }
            Runtime.postToMainThread(() -> {
                Log.e(TAG, e);
                callback.onError(e);
            });
        }
    });
}
Also used : ApiPhoneActivationType(im.actor.core.api.ApiPhoneActivationType) ResponseStartEmailAuth(im.actor.core.api.rpc.ResponseStartEmailAuth) RequestSendCodeByPhoneCall(im.actor.core.api.rpc.RequestSendCodeByPhoneCall) RequestSignUp(im.actor.core.api.rpc.RequestSignUp) Promise(im.actor.runtime.promise.Promise) Request(im.actor.core.network.parser.Request) PromiseFunc(im.actor.runtime.promise.PromiseFunc) ResponseStartPhoneAuth(im.actor.core.api.rpc.ResponseStartPhoneAuth) ArrayList(java.util.ArrayList) AbsModule(im.actor.core.modules.AbsModule) AuthCodeRes(im.actor.core.entity.AuthCodeRes) User(im.actor.core.entity.User) RequestGetOAuth2Params(im.actor.core.api.rpc.RequestGetOAuth2Params) Response(im.actor.core.network.parser.Response) Modules(im.actor.core.modules.Modules) im.actor.runtime(im.actor.runtime) RequestValidateCode(im.actor.core.api.rpc.RequestValidateCode) RpcCallback(im.actor.core.network.RpcCallback) ApiEmailActivationType(im.actor.core.api.ApiEmailActivationType) CommandCallback(im.actor.core.viewmodel.CommandCallback) Sex(im.actor.core.entity.Sex) RequestStartEmailAuth(im.actor.core.api.rpc.RequestStartEmailAuth) Command(im.actor.core.viewmodel.Command) AuthRes(im.actor.core.entity.AuthRes) ResponseStartUsernameAuth(im.actor.core.api.rpc.ResponseStartUsernameAuth) ApiConfiguration(im.actor.core.ApiConfiguration) ResponseGetOAuth2Params(im.actor.core.api.rpc.ResponseGetOAuth2Params) AuthStartRes(im.actor.core.entity.AuthStartRes) ResponseAuth(im.actor.core.api.rpc.ResponseAuth) IOException(java.io.IOException) AuthState(im.actor.core.AuthState) Runtime(im.actor.runtime.Runtime) RequestStartUsernameAuth(im.actor.core.api.rpc.RequestStartUsernameAuth) RpcException(im.actor.core.network.RpcException) ConnectionEndpoint(im.actor.runtime.mtproto.ConnectionEndpoint) TrustedKey(im.actor.core.network.TrustedKey) RequestStartPhoneAuth(im.actor.core.api.rpc.RequestStartPhoneAuth) ApiSex(im.actor.core.api.ApiSex) Endpoints(im.actor.core.network.Endpoints) ConnectionEndpointArray(im.actor.runtime.mtproto.ConnectionEndpointArray) ResponseVoid(im.actor.core.api.rpc.ResponseVoid) RequestCompleteOAuth2(im.actor.core.api.rpc.RequestCompleteOAuth2) RequestValidatePassword(im.actor.core.api.rpc.RequestValidatePassword) RequestStartAnonymousAuth(im.actor.core.api.rpc.RequestStartAnonymousAuth) AuthMode(im.actor.core.entity.AuthMode) RequestCompleteOAuth2(im.actor.core.api.rpc.RequestCompleteOAuth2) ResponseAuth(im.actor.core.api.rpc.ResponseAuth) RpcException(im.actor.core.network.RpcException)

Aggregations

RpcCallback (im.actor.core.network.RpcCallback)8 RpcException (im.actor.core.network.RpcException)8 ArrayList (java.util.ArrayList)6 User (im.actor.core.entity.User)5 ApiSupportConfiguration (im.actor.core.modules.api.ApiSupportConfiguration)5 ApiUser (im.actor.core.api.ApiUser)4 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)4 AbsModule (im.actor.core.modules.AbsModule)3 ModuleActor (im.actor.core.modules.ModuleActor)3 ModuleContext (im.actor.core.modules.ModuleContext)3 Modules (im.actor.core.modules.Modules)3 Command (im.actor.core.viewmodel.Command)3 Log (im.actor.runtime.Log)3 Void (im.actor.runtime.actors.messages.Void)3 Promise (im.actor.runtime.promise.Promise)3 ApiConfiguration (im.actor.core.ApiConfiguration)2 AuthState (im.actor.core.AuthState)2 ApiEmailActivationType (im.actor.core.api.ApiEmailActivationType)2 ApiOutPeer (im.actor.core.api.ApiOutPeer)2 ApiPhoneActivationType (im.actor.core.api.ApiPhoneActivationType)2