use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.
the class MultiDeviceContactUpdateJob method generateSingleContactUpdate.
private void generateSingleContactUpdate(@NonNull RecipientId recipientId) throws IOException, UntrustedIdentityException, NetworkException {
WriteDetails writeDetails = createTempFile();
try {
DeviceContactsOutputStream out = new DeviceContactsOutputStream(writeDetails.outputStream);
Recipient recipient = Recipient.resolved(recipientId);
if (recipient.getRegistered() == RecipientDatabase.RegisteredState.NOT_REGISTERED) {
Log.w(TAG, recipientId + " not registered!");
return;
}
Optional<IdentityRecord> identityRecord = ApplicationDependencies.getProtocolStore().aci().identities().getIdentityRecord(recipient.getId());
Optional<VerifiedMessage> verifiedMessage = getVerifiedMessage(recipient, identityRecord);
Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, recipient), Optional.fromNullable(recipient.isGroup() || recipient.isSystemContact() ? recipient.getDisplayName(context) : null), getAvatar(recipient.getId(), recipient.getContactUri()), Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), verifiedMessage, ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey()), recipient.isBlocked(), recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
out.close();
long length = BlobProvider.getInstance().calculateFileSize(context, writeDetails.uri);
sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, writeDetails.uri), length, false);
} catch (InvalidNumberException e) {
Log.w(TAG, e);
} finally {
BlobProvider.getInstance().delete(context, writeDetails.uri);
}
}
use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.
the class GroupCallUpdateSendJob method create.
@WorkerThread
@NonNull
public static GroupCallUpdateSendJob create(@NonNull RecipientId recipientId, @Nullable String eraId) {
Recipient conversationRecipient = Recipient.resolved(recipientId);
if (!conversationRecipient.isPushV2Group()) {
throw new AssertionError("We have a recipient, but it's not a V2 Group");
}
List<RecipientId> recipients = Stream.of(RecipientUtil.getEligibleForSending(conversationRecipient.getParticipants())).filterNot(Recipient::isSelf).filterNot(Recipient::isBlocked).map(Recipient::getId).toList();
return new GroupCallUpdateSendJob(recipientId, eraId, recipients, recipients.size(), new Parameters.Builder().setQueue(conversationRecipient.getId().toQueueKey()).setLifespan(TimeUnit.MINUTES.toMillis(5)).setMaxAttempts(3).build());
}
use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.
the class GroupV1MigrationJob method enqueueRoutineMigrationsIfNecessary.
public static void enqueueRoutineMigrationsIfNecessary(@NonNull Application application) {
if (!SignalStore.registrationValues().isRegistrationComplete() || !SignalStore.account().isRegistered() || SignalStore.account().getAci() == null) {
Log.i(TAG, "Registration not complete. Skipping.");
return;
}
long timeSinceRefresh = System.currentTimeMillis() - SignalStore.misc().getLastGv1RoutineMigrationTime();
if (timeSinceRefresh < REFRESH_INTERVAL) {
Log.i(TAG, "Too soon to refresh. Did the last refresh " + timeSinceRefresh + " ms ago.");
return;
}
SignalStore.misc().setLastGv1RoutineMigrationTime(System.currentTimeMillis());
SignalExecutors.BOUNDED.execute(() -> {
JobManager jobManager = ApplicationDependencies.getJobManager();
List<ThreadRecord> threads = SignalDatabase.threads().getRecentV1Groups(ROUTINE_LIMIT);
Set<RecipientId> needsRefresh = new HashSet<>();
if (threads.size() > 0) {
Log.d(TAG, "About to enqueue refreshes for " + threads.size() + " groups.");
}
for (ThreadRecord thread : threads) {
jobManager.add(new GroupV1MigrationJob(thread.getRecipient().getId()));
needsRefresh.addAll(Stream.of(thread.getRecipient().getParticipants()).filter(r -> r.getGroupsV2Capability() != Recipient.Capability.SUPPORTED || r.getGroupsV1MigrationCapability() != Recipient.Capability.SUPPORTED).map(Recipient::getId).toList());
}
if (needsRefresh.size() > 0) {
Log.w(TAG, "Enqueuing profile refreshes for " + needsRefresh.size() + " GV1 participants.");
RetrieveProfileJob.enqueue(needsRefresh);
}
});
}
use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.
the class PaymentSendJob method onRun.
@Override
protected void onRun() throws Exception {
if (!Recipient.self().isRegistered()) {
throw new NotPushRegisteredException();
}
if (!SignalStore.paymentsValues().mobileCoinPaymentsEnabled()) {
Log.w(TAG, "Payments are not enabled");
return;
}
Stopwatch stopwatch = new Stopwatch("Payment submission");
Wallet wallet = ApplicationDependencies.getPayments().getWallet();
PaymentDatabase paymentDatabase = SignalDatabase.payments();
paymentDatabase.createOutgoingPayment(uuid, recipientId, publicAddress, timestamp, note, amount);
Log.i(TAG, "Payment record created " + uuid);
stopwatch.split("Record created");
try {
PaymentSubmissionResult paymentSubmissionResult = wallet.sendPayment(publicAddress, amount.requireMobileCoin(), totalFee.requireMobileCoin());
stopwatch.split("Payment submitted");
if (paymentSubmissionResult.containsDefrags()) {
Log.i(TAG, "Payment contains " + paymentSubmissionResult.defrags().size() + " defrags, main payment" + uuid);
RecipientId self = Recipient.self().getId();
MobileCoinPublicAddress selfAddress = wallet.getMobileCoinPublicAddress();
for (TransactionSubmissionResult defrag : paymentSubmissionResult.defrags()) {
UUID defragUuid = UUID.randomUUID();
PaymentTransactionId.MobileCoin mobileCoinTransaction = (PaymentTransactionId.MobileCoin) defrag.getTransactionId();
paymentDatabase.createDefrag(defragUuid, self, selfAddress, timestamp - 1, mobileCoinTransaction.getFee(), mobileCoinTransaction.getTransaction(), mobileCoinTransaction.getReceipt());
Log.i(TAG, "Defrag entered with id " + defragUuid);
ApplicationDependencies.getJobManager().startChain(new PaymentTransactionCheckJob(defragUuid, QUEUE)).then(new MultiDeviceOutgoingPaymentSyncJob(defragUuid)).enqueue();
}
stopwatch.split("Defrag");
}
TransactionSubmissionResult.ErrorCode errorCode = paymentSubmissionResult.getErrorCode();
switch(errorCode) {
case INSUFFICIENT_FUNDS:
paymentDatabase.markPaymentFailed(uuid, FailureReason.INSUFFICIENT_FUNDS);
throw new PaymentException("Payment failed due to " + errorCode);
case GENERIC_FAILURE:
paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
throw new PaymentException("Payment failed due to " + errorCode);
case NETWORK_FAILURE:
paymentDatabase.markPaymentFailed(uuid, FailureReason.NETWORK);
throw new PaymentException("Payment failed due to " + errorCode);
case NONE:
Log.i(TAG, "Payment submission complete");
TransactionSubmissionResult transactionSubmissionResult = Objects.requireNonNull(paymentSubmissionResult.getNonDefrag());
PaymentTransactionId.MobileCoin mobileCoinTransaction = (PaymentTransactionId.MobileCoin) transactionSubmissionResult.getTransactionId();
paymentDatabase.markPaymentSubmitted(uuid, mobileCoinTransaction.getTransaction(), mobileCoinTransaction.getReceipt(), mobileCoinTransaction.getFee());
Log.i(TAG, "Payment record updated " + uuid);
break;
}
} catch (Exception e) {
Log.w(TAG, "Unknown payment failure", e);
paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
throw e;
}
stopwatch.split("Update database record");
stopwatch.stop(TAG);
}
use of org.thoughtcrime.securesms.recipients.RecipientId in project Signal-Android by WhisperSystems.
the class MultiDeviceGroupUpdateJob method onRun.
@Override
public void onRun() throws Exception {
if (!Recipient.self().isRegistered()) {
throw new NotPushRegisteredException();
}
if (!TextSecurePreferences.isMultiDevice(context)) {
Log.i(TAG, "Not multi device, aborting...");
return;
}
if (SignalStore.account().isLinkedDevice()) {
Log.i(TAG, "Not primary device, aborting...");
return;
}
ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);
Uri uri = BlobProvider.getInstance().forData(inputStream, 0).withFileName("multidevice-group-update").createForSingleSessionOnDiskAsync(context, () -> Log.i(TAG, "Write successful."), e -> Log.w(TAG, "Error during write.", e));
try (GroupDatabase.Reader reader = SignalDatabase.groups().getGroups()) {
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]));
boolean hasData = false;
GroupDatabase.GroupRecord record;
while ((record = reader.getNext()) != null) {
if (record.isV1Group()) {
List<SignalServiceAddress> members = new LinkedList<>();
List<Recipient> registeredMembers = RecipientUtil.getEligibleForSending(Recipient.resolvedList(record.getMembers()));
for (Recipient member : registeredMembers) {
members.add(RecipientUtil.toSignalServiceAddress(context, member));
}
RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(record.getId());
Recipient recipient = Recipient.resolved(recipientId);
Optional<Integer> expirationTimer = recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent();
Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
out.write(new DeviceGroup(record.getId().getDecodedId(), Optional.fromNullable(record.getTitle()), members, getAvatar(record.getRecipientId()), record.isActive(), expirationTimer, Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), recipient.isBlocked(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
hasData = true;
}
}
out.close();
if (hasData) {
long length = BlobProvider.getInstance().calculateFileSize(context, uri);
sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, uri), length);
} else {
Log.w(TAG, "No groups present for sync message. Sending an empty update.");
sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), null, 0);
}
} finally {
BlobProvider.getInstance().delete(context, uri);
}
}
Aggregations