Search in sources :

Example 36 with NotPushRegisteredException

use of org.thoughtcrime.securesms.net.NotPushRegisteredException 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);
}
Also used : PaymentTransactionId(org.thoughtcrime.securesms.payments.PaymentTransactionId) PaymentSubmissionResult(org.thoughtcrime.securesms.payments.PaymentSubmissionResult) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Wallet(org.thoughtcrime.securesms.payments.Wallet) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) TransactionSubmissionResult(org.thoughtcrime.securesms.payments.TransactionSubmissionResult) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase) UUID(java.util.UUID) MobileCoinPublicAddress(org.thoughtcrime.securesms.payments.MobileCoinPublicAddress)

Example 37 with NotPushRegisteredException

use of org.thoughtcrime.securesms.net.NotPushRegisteredException 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);
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) InputStream(java.io.InputStream) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) DeviceGroup(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Uri(android.net.Uri) LinkedList(java.util.LinkedList) ParcelFileDescriptor(android.os.ParcelFileDescriptor) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) DeviceGroupsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)

Example 38 with NotPushRegisteredException

use of org.thoughtcrime.securesms.net.NotPushRegisteredException in project Signal-Android by WhisperSystems.

the class MultiDeviceMessageRequestResponseJob method onRun.

@Override
public void onRun() throws IOException, UntrustedIdentityException {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!TextSecurePreferences.isMultiDevice(context)) {
        Log.i(TAG, "Not multi device, aborting...");
        return;
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    Recipient recipient = Recipient.resolved(threadRecipient);
    if (!recipient.hasServiceId()) {
        Log.i(TAG, "Queued for recipient without ServiceId");
        return;
    }
    MessageRequestResponseMessage response;
    if (recipient.isGroup()) {
        response = MessageRequestResponseMessage.forGroup(recipient.getGroupId().get().getDecodedId(), localToRemoteType(type));
    } else if (recipient.isMaybeRegistered()) {
        response = MessageRequestResponseMessage.forIndividual(RecipientUtil.toSignalServiceAddress(context, recipient), localToRemoteType(type));
    } else {
        response = null;
    }
    if (response != null) {
        messageSender.sendSyncMessage(SignalServiceSyncMessage.forMessageRequestResponse(response), UnidentifiedAccessUtil.getAccessForSync(context));
    } else {
        Log.w(TAG, recipient.getId() + " not registered!");
    }
}
Also used : MessageRequestResponseMessage(org.whispersystems.signalservice.api.messages.multidevice.MessageRequestResponseMessage) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Example 39 with NotPushRegisteredException

use of org.thoughtcrime.securesms.net.NotPushRegisteredException in project Signal-Android by WhisperSystems.

the class MultiDeviceProfileKeyUpdateJob method onRun.

@Override
public void onRun() throws IOException, UntrustedIdentityException {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!TextSecurePreferences.isMultiDevice(context)) {
        Log.i(TAG, "Not multi device...");
        return;
    }
    Optional<ProfileKey> profileKey = Optional.of(ProfileKeyUtil.getSelfProfileKey());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeviceContactsOutputStream out = new DeviceContactsOutputStream(baos);
    out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, Recipient.self()), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), profileKey, false, Optional.absent(), Optional.absent(), false));
    out.close();
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder().withStream(new ByteArrayInputStream(baos.toByteArray())).withContentType("application/octet-stream").withLength(baos.toByteArray().length).build();
    SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, false));
    messageSender.sendSyncMessage(syncMessage, UnidentifiedAccessUtil.getAccessForSync(context));
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) ByteArrayInputStream(java.io.ByteArrayInputStream) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) ContactsMessage(org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) ProfileKey(org.signal.zkgroup.profiles.ProfileKey) SignalServiceAttachmentStream(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream)

Example 40 with NotPushRegisteredException

use of org.thoughtcrime.securesms.net.NotPushRegisteredException in project Signal-Android by signalapp.

the class GroupCallUpdateSendJob method onRun.

@Override
protected void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    Recipient conversationRecipient = Recipient.resolved(recipientId);
    if (!conversationRecipient.isPushV2Group()) {
        throw new AssertionError("We have a recipient, but it's not a V2 Group");
    }
    List<Recipient> destinations = Stream.of(recipients).map(Recipient::resolved).toList();
    List<Recipient> completions = deliver(conversationRecipient, destinations);
    for (Recipient completion : completions) {
        recipients.remove(completion.getId());
    }
    Log.i(TAG, "Completed now: " + completions.size() + ", Remaining: " + recipients.size());
    if (!recipients.isEmpty()) {
        Log.w(TAG, "Still need to send to " + recipients.size() + " recipients. Retrying.");
        throw new RetryLaterException();
    }
}
Also used : NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException)

Aggregations

NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)58 Recipient (org.thoughtcrime.securesms.recipients.Recipient)38 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)38 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)18 LinkedList (java.util.LinkedList)12 RetryLaterException (org.thoughtcrime.securesms.transport.RetryLaterException)12 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)8 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)7 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)6 PaymentDatabase (org.thoughtcrime.securesms.database.PaymentDatabase)6 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)6 SignalServiceReceiptMessage (org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage)6 MessageRecord (org.thoughtcrime.securesms.database.model.MessageRecord)4 GroupId (org.thoughtcrime.securesms.groups.GroupId)4 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)4 Data (org.thoughtcrime.securesms.jobmanager.Data)3 SignalServiceGroup (org.whispersystems.signalservice.api.messages.SignalServiceGroup)3 SignalServiceSyncMessage (org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage)3 Uri (android.net.Uri)2 ParcelFileDescriptor (android.os.ParcelFileDescriptor)2