use of im.actor.core.api.base.FatSeqUpdate 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();
}
});
}
use of im.actor.core.api.base.FatSeqUpdate in project actor-platform by actorapp.
the class SequenceActor method onReceive.
//
// Messages
//
@Override
public void onReceive(Object message) {
if (message instanceof Invalidate || message instanceof SeqUpdateTooLong || message instanceof ForceInvalidate) {
if (!isValidated) {
return;
}
invalidate();
} else if (message instanceof SeqUpdate || message instanceof FatSeqUpdate) {
if (!isValidated) {
stash();
return;
}
int seq;
byte[] state;
int type;
byte[] body;
List<ApiUser> users = null;
List<ApiGroup> groups = null;
if (message instanceof SeqUpdate) {
seq = ((SeqUpdate) message).getSeq();
state = ((SeqUpdate) message).getState();
type = ((SeqUpdate) message).getUpdateHeader();
body = ((SeqUpdate) message).getUpdate();
} else {
seq = ((FatSeqUpdate) message).getSeq();
state = ((FatSeqUpdate) message).getState();
type = ((FatSeqUpdate) message).getUpdateHeader();
body = ((FatSeqUpdate) message).getUpdate();
users = ((FatSeqUpdate) message).getUsers();
groups = ((FatSeqUpdate) message).getGroups();
}
onUpdateReceived(seq, state, type, body, users, groups);
} else if (message instanceof ExecuteAfter) {
if (!isValidated) {
stash();
return;
}
onExecuteAfter((ExecuteAfter) message);
} else if (message instanceof PushSeq) {
if (!isValidated) {
stash();
return;
}
onPushSeqReceived(((PushSeq) message).seq, ((PushSeq) message).authId);
} else if (message instanceof WeakUpdate) {
WeakUpdate weakUpdate = (WeakUpdate) message;
onWeakUpdateReceived(weakUpdate.getUpdateHeader(), weakUpdate.getUpdate(), weakUpdate.getDate());
} else {
super.onReceive(message);
}
}
Aggregations