use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class EncryptedPeerActor method doDecrypt.
private Promise<DecryptBoxResponse> doDecrypt(final EncryptedBox data) {
if (!isReady) {
stash();
return null;
}
final int senderKeyGroup = ByteStrings.bytesToInt(ByteStrings.substring(data.getEncryptedPackage(), 0, 4));
final byte[] encPackage = ByteStrings.substring(data.getEncryptedPackage(), 4, data.getEncryptedPackage().length - 4);
if (ignoredKeyGroups.contains(senderKeyGroup)) {
throw new RuntimeException("This key group is ignored");
}
return PromisesArray.of(data.getKeys()).filter(EncryptedBoxKey.FILTER(myUid(), ownKeyGroupId)).first().flatMap(new Function<EncryptedBoxKey, Promise<Tuple2<SessionActor, EncryptedBoxKey>>>() {
@Override
public Promise<Tuple2<SessionActor, EncryptedBoxKey>> apply(final EncryptedBoxKey boxKey) {
final long senderPreKeyId = ByteStrings.bytesToLong(boxKey.getEncryptedKey(), 4);
final long receiverPreKeyId = ByteStrings.bytesToLong(boxKey.getEncryptedKey(), 12);
if (activeSessions.containsKey(boxKey.getKeyGroupId())) {
for (SessionActor s : activeSessions.get(senderKeyGroup).getSessions()) {
if (s.getSession().getOwnPreKeyId() == receiverPreKeyId && s.getSession().getTheirPreKeyId() == senderPreKeyId) {
return success(new Tuple2<>(s, boxKey));
}
}
}
return context().getEncryption().getSessionManagerInt().pickSession(uid, senderKeyGroup, receiverPreKeyId, senderPreKeyId).map(new Function<PeerSession, Tuple2<SessionActor, EncryptedBoxKey>>() {
@Override
public Tuple2<SessionActor, EncryptedBoxKey> apply(PeerSession src) {
return new Tuple2<>(spawnSession(src), boxKey);
}
});
}
}).flatMap(new Function<Tuple2<SessionActor, EncryptedBoxKey>, Promise<EncryptedSessionActor.DecryptedPackage>>() {
@Override
public Promise<EncryptedSessionActor.DecryptedPackage> apply(Tuple2<SessionActor, EncryptedBoxKey> src) {
Log.d(TAG, "Key size:" + src.getT2().getEncryptedKey().length);
// TODO: Implement
return null;
}
}).map(new Function<EncryptedSessionActor.DecryptedPackage, DecryptBoxResponse>() {
@Override
public DecryptBoxResponse apply(EncryptedSessionActor.DecryptedPackage decryptedPackage) {
byte[] encData;
try {
byte[] encKeyExtended = decryptedPackage.getData().length >= 128 ? decryptedPackage.getData() : keyPrf.calculate(decryptedPackage.getData(), "ActorPackage", 128);
encData = ActorBox.openBox(ByteStrings.intToBytes(senderKeyGroup), encPackage, new ActorBoxKey(encKeyExtended));
Log.d(TAG, "Box size: " + encData.length);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return new DecryptBoxResponse(encData);
}
});
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class UserRouter method checkIsInPhoneBook.
@Verified
protected Promise<Void> checkIsInPhoneBook(User user) {
if (!config().isEnableOnClientPrivacy()) {
return Promise.success(null);
}
Log.d("ON_CLIENT_PRIVACY", "checking " + user.getName() + " is in phone book");
return getPhoneBook().flatMap(new Function<List<PhoneBookContact>, Promise<Void>>() {
@Override
public Promise<Void> apply(List<PhoneBookContact> phoneBookContacts) {
return new Promise<Void>(resolver -> {
List<ContactRecord> userRecords = user.getRecords();
Log.d("ON_CLIENT_PRIVACY", "phonebook have " + phoneBookContacts.size() + " records");
Log.d("ON_CLIENT_PRIVACY", "user have " + userRecords.size() + " records");
outer: for (ContactRecord record : userRecords) {
for (PhoneBookContact phoneBookContact : phoneBookContacts) {
for (PhoneBookPhone phone1 : phoneBookContact.getPhones()) {
if (record.getRecordType() == ContactRecordType.PHONE) {
if (record.getRecordData().equals(phone1.getNumber() + "")) {
context().getContactsModule().markInPhoneBook(user.getUid());
getUserVM(user.getUid()).isInPhoneBook().change(true);
Log.d("ON_CLIENT_PRIVACY", "in record book!");
break outer;
}
}
}
for (PhoneBookEmail email : phoneBookContact.getEmails()) {
if (record.getRecordType() == ContactRecordType.EMAIL) {
if (record.getRecordData().equals(email.getEmail())) {
context().getContactsModule().markInPhoneBook(user.getUid());
getUserVM(user.getUid()).isInPhoneBook().change(true);
Log.d("ON_CLIENT_PRIVACY", "in record book!");
break outer;
}
}
}
}
}
Log.d("ON_CLIENT_PRIVACY", "finish check");
resolver.result(null);
});
}
});
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class RouterActor method onPeersChanged.
//
// Peer Changed
//
private Promise<Void> onPeersChanged(List<User> users, List<Group> groups) {
Promise<Void> res = Promise.success(null);
boolean isActiveNeedUpdate = false;
for (User u : users) {
if (!isActiveNeedUpdate) {
for (ActiveDialogGroup g : activeDialogStorage.getGroups()) {
if (g.getPeers().contains(u.peer())) {
isActiveNeedUpdate = true;
break;
}
}
}
res = res.chain(v -> getDialogsRouter().onUserChanged(u));
}
for (Group group : groups) {
if (!isActiveNeedUpdate) {
for (ActiveDialogGroup g : activeDialogStorage.getGroups()) {
if (g.getPeers().contains(group.peer())) {
isActiveNeedUpdate = true;
break;
}
}
}
res = res.chain(v -> getDialogsRouter().onGroupChanged(group));
}
if (isActiveNeedUpdate) {
notifyActiveDialogsVM();
}
return res;
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class AndroidHttpProvider method putMethod.
@Override
public Promise<HTTPResponse> putMethod(String url, byte[] contents) {
return new Promise<>(resolver -> {
final Request request = new Request.Builder().url(url).method("PUT", RequestBody.create(MEDIA_TYPE, contents)).build();
Log.d(TAG, "Uploading part: " + request.toString());
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "Uploading part error: " + request.toString());
e.printStackTrace();
resolver.error(new HTTPError(0));
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG, "Upload part response: " + request.toString() + " -> " + response.toString());
if (response.code() >= 200 && response.code() < 300) {
resolver.result(new HTTPResponse(response.code(), null));
} else {
resolver.error(new HTTPError(response.code()));
}
}
});
});
}
use of im.actor.runtime.promise.Promise in project actor-platform by actorapp.
the class JsHttpProvider method putMethod.
@Override
public Promise<HTTPResponse> putMethod(String url, byte[] contents) {
return new Promise<>(resolver -> {
JsHttpRequest request = JsHttpRequest.create();
request.open("PUT", url);
request.setRequestHeader("Content-Type", "application/octet-stream");
request.setOnLoadHandler(request1 -> {
if (request1.getReadyState() == 4) {
if (request1.getStatus() >= 200 && request1.getStatus() < 300) {
resolver.result(new HTTPResponse(request1.getStatus(), null));
} else {
resolver.error(new HTTPError(request1.getStatus()));
}
}
});
Uint8Array push = TypedArrays.createUint8Array(contents.length);
for (int i = 0; i < contents.length; i++) {
push.set(i, contents[i]);
}
request.send(push.buffer());
});
}
Aggregations