Search in sources :

Example 16 with Promise

use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.

the class UserRouter method onLoadFullUser.

//
// Users changed
//
@Verified
private void onLoadFullUser(int uid) {
    if (requestedFullUsers.contains(uid)) {
        return;
    }
    requestedFullUsers.add(uid);
    freeze();
    users().getValueAsync(uid).flatMap((Function<User, Promise<Tuple2<ResponseLoadFullUsers, User>>>) u -> {
        if (!u.isHaveExtension()) {
            ArrayList<ApiUserOutPeer> users = new ArrayList<>();
            users.add(new ApiUserOutPeer(u.getUid(), u.getAccessHash()));
            return api(new RequestLoadFullUsers(users)).map(responseLoadFullUsers -> new Tuple2<>(responseLoadFullUsers, u));
        } else {
            if (!getUserVM(uid).isInPhoneBook().get()) {
                return checkIsInPhoneBook(u).flatMap(new Function<Void, Promise<Tuple2<ResponseLoadFullUsers, User>>>() {

                    @Override
                    public Promise<Tuple2<ResponseLoadFullUsers, User>> apply(Void aVoid) {
                        return Promise.failure(new RuntimeException("Already loaded"));
                    }
                });
            } else {
                return Promise.failure(new RuntimeException("Already loaded"));
            }
        }
    }).then(r -> {
        User upd = r.getT2().updateExt(r.getT1().getFullUsers().get(0));
        users().addOrUpdateItem(upd);
    }).chain(r -> checkIsInPhoneBook(r.getT2().updateExt(r.getT1().getFullUsers().get(0)))).after((r, e) -> unfreeze());
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ContactRecord(im.actor.core.entity.ContactRecord) PhoneBookContact(im.actor.core.entity.PhoneBookContact) ApiUser(im.actor.core.api.ApiUser) UpdateUserContactsChanged(im.actor.core.api.updates.UpdateUserContactsChanged) ApiMapValue(im.actor.core.api.ApiMapValue) ApiBotCommand(im.actor.core.api.ApiBotCommand) PhoneBookEmail(im.actor.core.entity.PhoneBookEmail) PromisesArray(im.actor.runtime.promise.PromisesArray) RouterLoadFullUser(im.actor.core.modules.users.router.entity.RouterLoadFullUser) UpdateContactRegistered(im.actor.core.api.updates.UpdateContactRegistered) UpdateUserNameChanged(im.actor.core.api.updates.UpdateUserNameChanged) ContactRecordType(im.actor.core.entity.ContactRecordType) Tuple2(im.actor.runtime.function.Tuple2) UpdateUserBlocked(im.actor.core.api.updates.UpdateUserBlocked) Void(im.actor.runtime.actors.messages.Void) Verified(im.actor.runtime.annotations.Verified) UserPhone(im.actor.core.viewmodel.UserPhone) ResponseLoadFullUsers(im.actor.core.api.rpc.ResponseLoadFullUsers) UpdateUserAvatarChanged(im.actor.core.api.updates.UpdateUserAvatarChanged) List(java.util.List) UpdateUserAboutChanged(im.actor.core.api.updates.UpdateUserAboutChanged) ModuleActor(im.actor.core.modules.ModuleActor) ArrayListUserPhone(im.actor.core.viewmodel.generics.ArrayListUserPhone) UpdateUserFullExtChanged(im.actor.core.api.updates.UpdateUserFullExtChanged) UserEmail(im.actor.core.viewmodel.UserEmail) Peer(im.actor.core.entity.Peer) UpdateUserExtChanged(im.actor.core.api.updates.UpdateUserExtChanged) MessageState(im.actor.core.entity.MessageState) Message(im.actor.core.entity.Message) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ApiAvatar(im.actor.core.api.ApiAvatar) ApiContactRecord(im.actor.core.api.ApiContactRecord) UpdateUserTimeZoneChanged(im.actor.core.api.updates.UpdateUserTimeZoneChanged) Function(im.actor.runtime.function.Function) ServiceUserRegistered(im.actor.core.entity.content.ServiceUserRegistered) PhoneBookProvider(im.actor.core.providers.PhoneBookProvider) Promise(im.actor.runtime.promise.Promise) UpdateUserBotCommandsChanged(im.actor.core.api.updates.UpdateUserBotCommandsChanged) UpdateUserNickChanged(im.actor.core.api.updates.UpdateUserNickChanged) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) User(im.actor.core.entity.User) UpdateUserLocalNameChanged(im.actor.core.api.updates.UpdateUserLocalNameChanged) ArrayListUserEmail(im.actor.core.viewmodel.generics.ArrayListUserEmail) JavaUtil.equalsE(im.actor.core.util.JavaUtil.equalsE) UpdateUserUnblocked(im.actor.core.api.updates.UpdateUserUnblocked) RequestLoadFullUsers(im.actor.core.api.rpc.RequestLoadFullUsers) ContactsSyncActor(im.actor.core.modules.contacts.ContactsSyncActor) UserVM(im.actor.core.viewmodel.UserVM) RouterFetchMissingUsers(im.actor.core.modules.users.router.entity.RouterFetchMissingUsers) BookImportStorage(im.actor.core.modules.contacts.entity.BookImportStorage) RouterApplyUsers(im.actor.core.modules.users.router.entity.RouterApplyUsers) UpdateUserPreferredLanguagesChanged(im.actor.core.api.updates.UpdateUserPreferredLanguagesChanged) Log(im.actor.runtime.Log) PhoneBookPhone(im.actor.core.entity.PhoneBookPhone) Update(im.actor.core.network.parser.Update) RouterUserUpdate(im.actor.core.modules.users.router.entity.RouterUserUpdate) RequestLoadFullUsers(im.actor.core.api.rpc.RequestLoadFullUsers) ApiUser(im.actor.core.api.ApiUser) RouterLoadFullUser(im.actor.core.modules.users.router.entity.RouterLoadFullUser) User(im.actor.core.entity.User) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) ArrayList(java.util.ArrayList) Function(im.actor.runtime.function.Function) Promise(im.actor.runtime.promise.Promise) Tuple2(im.actor.runtime.function.Tuple2) Void(im.actor.runtime.actors.messages.Void) ResponseLoadFullUsers(im.actor.core.api.rpc.ResponseLoadFullUsers) Verified(im.actor.runtime.annotations.Verified)

Example 17 with Promise

use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.

the class SequenceHandlerActor method onDifferenceUpdate.

private Promise<Void> onDifferenceUpdate(@NotNull List<ApiUser> users, @NotNull List<ApiGroup> groups, @NotNull List<ApiUserOutPeer> userOutPeers, @NotNull List<ApiGroupOutPeer> groupOutPeers, @NotNull List<Update> updates) {
    if (updates.size() > 0) {
        String command = "Difference updates:";
        for (Update u : updates) {
            command += "\n| " + u;
        }
        Log.d(TAG, command);
    }
    beginUpdates();
    // Related Users
    Promise<Void> currentPromise = updates().applyRelatedData(users, groups);
    // Loading missing peers
    currentPromise = currentPromise.chain(v -> updates().loadRequiredPeers(userOutPeers, groupOutPeers));
    // Apply Diff
    long applyStart = im.actor.runtime.Runtime.getCurrentTime();
    currentPromise = currentPromise.chain(v -> processor.applyDifferenceUpdate(updates)).then(v -> {
        Log.d(TAG, "Difference applied in " + (im.actor.runtime.Runtime.getCurrentTime() - applyStart) + " ms");
        endUpdates();
    });
    return currentPromise;
}
Also used : HandlerDifferenceUpdates(im.actor.core.modules.sequence.internal.HandlerDifferenceUpdates) ModuleContext(im.actor.core.modules.ModuleContext) ActorCreator(im.actor.runtime.actors.ActorCreator) Promise(im.actor.runtime.promise.Promise) ApiUser(im.actor.core.api.ApiUser) PromiseFunc(im.actor.runtime.promise.PromiseFunc) ArrayList(java.util.ArrayList) UpdateProcessor(im.actor.core.modules.sequence.processor.UpdateProcessor) HandlerRelatedResponse(im.actor.core.modules.sequence.internal.HandlerRelatedResponse) PromisesArray(im.actor.runtime.promise.PromisesArray) HandlerWeakUpdate(im.actor.core.modules.sequence.internal.HandlerWeakUpdate) RequestGetReferencedEntitites(im.actor.core.api.rpc.RequestGetReferencedEntitites) ApiGroup(im.actor.core.api.ApiGroup) Void(im.actor.runtime.actors.messages.Void) Runtime(im.actor.runtime.Runtime) Constructor(im.actor.runtime.function.Constructor) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ModuleActor(im.actor.core.modules.ModuleActor) HandlerSeqUpdate(im.actor.core.modules.sequence.internal.HandlerSeqUpdate) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) Log(im.actor.runtime.Log) NotNull(org.jetbrains.annotations.NotNull) Function(im.actor.runtime.function.Function) Update(im.actor.core.network.parser.Update) Promises(im.actor.runtime.promise.Promises) HandlerWeakUpdate(im.actor.core.modules.sequence.internal.HandlerWeakUpdate) HandlerSeqUpdate(im.actor.core.modules.sequence.internal.HandlerSeqUpdate) Update(im.actor.core.network.parser.Update) Void(im.actor.runtime.actors.messages.Void)

Example 18 with Promise

use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.

the class AndroidHttpProvider method getMethod.

@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
    return new Promise<>(resolver -> {
        final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
        Log.d(TAG, "Downloading part: " + request.toString());
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                Log.d(TAG, "Downloading part error: " + request.toString());
                e.printStackTrace();
                resolver.error(new HTTPError(0));
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
                if (response.code() >= 200 && response.code() < 300) {
                    resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
                } else {
                    resolver.error(new HTTPError(response.code()));
                }
            }
        });
    });
}
Also used : Response(com.squareup.okhttp.Response) HTTPResponse(im.actor.runtime.http.HTTPResponse) Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) Callback(com.squareup.okhttp.Callback) HTTPResponse(im.actor.runtime.http.HTTPResponse) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 19 with Promise

use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.

the class AndroidInputFile method read.

@Override
public Promise<FilePart> read(int fileOffset, int len) {
    return new Promise<>(resolver -> {
        executor.execute(() -> {
            try {
                byte[] data = new byte[len];
                randomAccessFile.seek(fileOffset);
                randomAccessFile.read(data, 0, len);
                resolver.result(new FilePart(fileOffset, len, data));
            } catch (Exception e) {
                e.printStackTrace();
                resolver.error(e);
            }
        });
    });
}
Also used : Promise(im.actor.runtime.promise.Promise) FilePart(im.actor.runtime.files.FilePart) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

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