Search in sources :

Example 86 with MessageDatabase

use of org.thoughtcrime.securesms.database.MessageDatabase in project Signal-Android by signalapp.

the class PushGroupSendJob method enqueue.

@WorkerThread
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long messageId, @NonNull RecipientId destination, @Nullable RecipientId filterAddress) {
    try {
        Recipient group = Recipient.resolved(destination);
        if (!group.isPushGroup()) {
            throw new AssertionError("Not a group!");
        }
        MessageDatabase database = SignalDatabase.mms();
        OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
        Set<String> attachmentUploadIds = enqueueCompressingAndUploadAttachmentsChains(jobManager, message);
        if (!SignalDatabase.groups().isActive(group.requireGroupId()) && !isGv2UpdateMessage(message)) {
            throw new MmsException("Inactive group!");
        }
        jobManager.add(new PushGroupSendJob(messageId, destination, filterAddress, !attachmentUploadIds.isEmpty()), attachmentUploadIds, attachmentUploadIds.isEmpty() ? null : destination.toQueueKey());
    } catch (NoSuchMessageException | MmsException e) {
        Log.w(TAG, "Failed to enqueue message.", e);
        SignalDatabase.mms().markAsSentFailed(messageId);
        notifyMediaMessageDeliveryFailed(context, messageId);
    }
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) NoSuchMessageException(org.thoughtcrime.securesms.database.NoSuchMessageException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) ByteString(com.google.protobuf.ByteString) WorkerThread(androidx.annotation.WorkerThread)

Example 87 with MessageDatabase

use of org.thoughtcrime.securesms.database.MessageDatabase in project Signal-Android by signalapp.

the class MmsSendJob method enqueue.

/**
 * Enqueues compression jobs for attachments and finally the MMS send job.
 */
@WorkerThread
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long messageId) {
    MessageDatabase database = SignalDatabase.mms();
    OutgoingMediaMessage message;
    try {
        message = database.getOutgoingMessage(messageId);
    } catch (MmsException | NoSuchMessageException e) {
        throw new AssertionError(e);
    }
    List<Job> compressionJobs = Stream.of(message.getAttachments()).map(a -> (Job) AttachmentCompressionJob.fromAttachment((DatabaseAttachment) a, true, message.getSubscriptionId())).toList();
    MmsSendJob sendJob = new MmsSendJob(messageId);
    jobManager.startChain(compressionJobs).then(sendJob).enqueue();
}
Also used : SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) NumberUtil(org.thoughtcrime.securesms.phonenumbers.NumberUtil) Arrays(java.util.Arrays) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) PduPart(com.google.android.mms.pdu_alt.PduPart) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) Utils(com.klinker.android.send_message.Utils) SecureRandom(java.security.SecureRandom) SmilXmlSerializer(com.android.mms.dom.smil.parser.SmilXmlSerializer) Recipient(org.thoughtcrime.securesms.recipients.Recipient) PartAuthority(org.thoughtcrime.securesms.mms.PartAuthority) SendReq(com.google.android.mms.pdu_alt.SendReq) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Log(org.signal.core.util.logging.Log) List(java.util.List) ContentType(com.google.android.mms.ContentType) SmilHelper(com.google.android.mms.smil.SmilHelper) Job(org.thoughtcrime.securesms.jobmanager.Job) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) NoSuchMessageException(org.thoughtcrime.securesms.database.NoSuchMessageException) Attachment(org.thoughtcrime.securesms.attachments.Attachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) Context(android.content.Context) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) Stream(com.annimon.stream.Stream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Util(org.thoughtcrime.securesms.util.Util) JobLogger(org.thoughtcrime.securesms.jobmanager.JobLogger) WorkerThread(androidx.annotation.WorkerThread) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) InvalidHeaderValueException(com.google.android.mms.InvalidHeaderValueException) PduHeaders(com.google.android.mms.pdu_alt.PduHeaders) StreamUtil(org.signal.core.util.StreamUtil) MmsException(org.thoughtcrime.securesms.mms.MmsException) EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) TextUtils(android.text.TextUtils) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) Hex(org.thoughtcrime.securesms.util.Hex) IOException(java.io.IOException) SendConf(com.google.android.mms.pdu_alt.SendConf) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) CharacterSets(com.google.android.mms.pdu_alt.CharacterSets) PduComposer(com.google.android.mms.pdu_alt.PduComposer) CompatMmsConnection(org.thoughtcrime.securesms.mms.CompatMmsConnection) MmsSendResult(org.thoughtcrime.securesms.mms.MmsSendResult) MimeTypeMap(android.webkit.MimeTypeMap) PduBody(com.google.android.mms.pdu_alt.PduBody) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) NoSuchMessageException(org.thoughtcrime.securesms.database.NoSuchMessageException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) Job(org.thoughtcrime.securesms.jobmanager.Job) WorkerThread(androidx.annotation.WorkerThread)

Example 88 with MessageDatabase

use of org.thoughtcrime.securesms.database.MessageDatabase in project Signal-Android by signalapp.

the class MmsSendJob method onSend.

@Override
public void onSend() throws MmsException, NoSuchMessageException, IOException {
    MessageDatabase database = SignalDatabase.mms();
    OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
    if (database.isSent(messageId)) {
        Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
        return;
    }
    try {
        Log.i(TAG, "Sending message: " + messageId);
        SendReq pdu = constructSendPdu(message);
        validateDestinations(message, pdu);
        final byte[] pduBytes = getPduBytes(pdu);
        final SendConf sendConf = new CompatMmsConnection(context).send(pduBytes, message.getSubscriptionId());
        final MmsSendResult result = getSendResult(sendConf, pdu);
        database.markAsSent(messageId, false);
        markAttachmentsUploaded(messageId, message);
        Log.i(TAG, "Sent message: " + messageId);
    } catch (UndeliverableMessageException | IOException e) {
        Log.w(TAG, e);
        database.markAsSentFailed(messageId);
        notifyMediaMessageDeliveryFailed(context, messageId);
    } catch (InsecureFallbackApprovalException e) {
        Log.w(TAG, e);
        database.markAsPendingInsecureSmsFallback(messageId);
        notifyMediaMessageDeliveryFailed(context, messageId);
    }
}
Also used : SendConf(com.google.android.mms.pdu_alt.SendConf) CompatMmsConnection(org.thoughtcrime.securesms.mms.CompatMmsConnection) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsSendResult(org.thoughtcrime.securesms.mms.MmsSendResult) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) IOException(java.io.IOException) SendReq(com.google.android.mms.pdu_alt.SendReq) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException)

Example 89 with MessageDatabase

use of org.thoughtcrime.securesms.database.MessageDatabase in project Signal-Android by signalapp.

the class PushMediaSendJob method onPushSend.

@Override
public void onPushSend() throws IOException, MmsException, NoSuchMessageException, UndeliverableMessageException, RetryLaterException {
    ExpiringMessageManager expirationManager = ApplicationDependencies.getExpiringMessageManager();
    MessageDatabase database = SignalDatabase.mms();
    OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
    long threadId = database.getMessageRecord(messageId).getThreadId();
    if (database.isSent(messageId)) {
        warn(TAG, String.valueOf(message.getSentTimeMillis()), "Message " + messageId + " was already sent. Ignoring.");
        return;
    }
    try {
        log(TAG, String.valueOf(message.getSentTimeMillis()), "Sending message: " + messageId + ", Recipient: " + message.getRecipient().getId() + ", Thread: " + threadId + ", Attachments: " + buildAttachmentString(message.getAttachments()));
        RecipientUtil.shareProfileIfFirstSecureMessage(context, message.getRecipient());
        Recipient recipient = message.getRecipient().fresh();
        byte[] profileKey = recipient.getProfileKey();
        UnidentifiedAccessMode accessMode = recipient.getUnidentifiedAccessMode();
        boolean unidentified = deliver(message);
        database.markAsSent(messageId, true);
        markAttachmentsUploaded(messageId, message);
        database.markUnidentified(messageId, unidentified);
        if (recipient.isSelf()) {
            SyncMessageId id = new SyncMessageId(recipient.getId(), message.getSentTimeMillis());
            SignalDatabase.mmsSms().incrementDeliveryReceiptCount(id, System.currentTimeMillis());
            SignalDatabase.mmsSms().incrementReadReceiptCount(id, System.currentTimeMillis());
            SignalDatabase.mmsSms().incrementViewedReceiptCount(id, System.currentTimeMillis());
        }
        if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN && profileKey == null) {
            log(TAG, String.valueOf(message.getSentTimeMillis()), "Marking recipient as UD-unrestricted following a UD send.");
            SignalDatabase.recipients().setUnidentifiedAccessMode(recipient.getId(), UnidentifiedAccessMode.UNRESTRICTED);
        } else if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN) {
            log(TAG, String.valueOf(message.getSentTimeMillis()), "Marking recipient as UD-enabled following a UD send.");
            SignalDatabase.recipients().setUnidentifiedAccessMode(recipient.getId(), UnidentifiedAccessMode.ENABLED);
        } else if (!unidentified && accessMode != UnidentifiedAccessMode.DISABLED) {
            log(TAG, String.valueOf(message.getSentTimeMillis()), "Marking recipient as UD-disabled following a non-UD send.");
            SignalDatabase.recipients().setUnidentifiedAccessMode(recipient.getId(), UnidentifiedAccessMode.DISABLED);
        }
        if (message.getExpiresIn() > 0 && !message.isExpirationUpdate()) {
            database.markExpireStarted(messageId);
            expirationManager.scheduleDeletion(messageId, true, message.getExpiresIn());
        }
        if (message.isViewOnce()) {
            SignalDatabase.attachments().deleteAttachmentFilesForViewOnceMessage(messageId);
        }
        log(TAG, String.valueOf(message.getSentTimeMillis()), "Sent message: " + messageId);
    } catch (InsecureFallbackApprovalException ifae) {
        warn(TAG, "Failure", ifae);
        database.markAsPendingInsecureSmsFallback(messageId);
        notifyMediaMessageDeliveryFailed(context, messageId);
        ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(false));
    } catch (UntrustedIdentityException uie) {
        warn(TAG, "Failure", uie);
        RecipientId recipientId = Recipient.external(context, uie.getIdentifier()).getId();
        database.addMismatchedIdentity(messageId, recipientId, uie.getIdentityKey());
        database.markAsSentFailed(messageId);
        RetrieveProfileJob.enqueue(recipientId);
    } catch (ProofRequiredException e) {
        handleProofRequiredException(e, SignalDatabase.threads().getRecipientForThreadId(threadId), threadId, messageId, true);
    }
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ExpiringMessageManager(org.thoughtcrime.securesms.service.ExpiringMessageManager) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) UnidentifiedAccessMode(org.thoughtcrime.securesms.database.RecipientDatabase.UnidentifiedAccessMode) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Example 90 with MessageDatabase

use of org.thoughtcrime.securesms.database.MessageDatabase in project Signal-Android by signalapp.

the class PushTextSendJob method onPushSend.

@Override
public void onPushSend() throws IOException, NoSuchMessageException, UndeliverableMessageException, RetryLaterException {
    SignalLocalMetrics.IndividualMessageSend.onJobStarted(messageId);
    ExpiringMessageManager expirationManager = ApplicationDependencies.getExpiringMessageManager();
    MessageDatabase database = SignalDatabase.sms();
    SmsMessageRecord record = database.getSmsMessage(messageId);
    if (!record.isPending() && !record.isFailed()) {
        warn(TAG, String.valueOf(record.getDateSent()), "Message " + messageId + " was already sent. Ignoring.");
        return;
    }
    try {
        log(TAG, String.valueOf(record.getDateSent()), "Sending message: " + messageId + ",  Recipient: " + record.getRecipient().getId() + ", Thread: " + record.getThreadId());
        RecipientUtil.shareProfileIfFirstSecureMessage(context, record.getRecipient());
        Recipient recipient = record.getRecipient().resolve();
        byte[] profileKey = recipient.getProfileKey();
        UnidentifiedAccessMode accessMode = recipient.getUnidentifiedAccessMode();
        boolean unidentified = deliver(record);
        database.markAsSent(messageId, true);
        database.markUnidentified(messageId, unidentified);
        if (recipient.isSelf()) {
            SyncMessageId id = new SyncMessageId(recipient.getId(), record.getDateSent());
            SignalDatabase.mmsSms().incrementDeliveryReceiptCount(id, System.currentTimeMillis());
            SignalDatabase.mmsSms().incrementReadReceiptCount(id, System.currentTimeMillis());
        }
        if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN && profileKey == null) {
            log(TAG, String.valueOf(record.getDateSent()), "Marking recipient as UD-unrestricted following a UD send.");
            SignalDatabase.recipients().setUnidentifiedAccessMode(recipient.getId(), UnidentifiedAccessMode.UNRESTRICTED);
        } else if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN) {
            log(TAG, String.valueOf(record.getDateSent()), "Marking recipient as UD-enabled following a UD send.");
            SignalDatabase.recipients().setUnidentifiedAccessMode(recipient.getId(), UnidentifiedAccessMode.ENABLED);
        } else if (!unidentified && accessMode != UnidentifiedAccessMode.DISABLED) {
            log(TAG, String.valueOf(record.getDateSent()), "Marking recipient as UD-disabled following a non-UD send.");
            SignalDatabase.recipients().setUnidentifiedAccessMode(recipient.getId(), UnidentifiedAccessMode.DISABLED);
        }
        if (record.getExpiresIn() > 0) {
            database.markExpireStarted(messageId);
            expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
        }
        log(TAG, String.valueOf(record.getDateSent()), "Sent message: " + messageId);
    } catch (InsecureFallbackApprovalException e) {
        warn(TAG, String.valueOf(record.getDateSent()), "Failure", e);
        database.markAsPendingInsecureSmsFallback(record.getId());
        ApplicationDependencies.getMessageNotifier().notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
        ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(false));
    } catch (UntrustedIdentityException e) {
        warn(TAG, String.valueOf(record.getDateSent()), "Failure", e);
        RecipientId recipientId = Recipient.external(context, e.getIdentifier()).getId();
        database.addMismatchedIdentity(record.getId(), recipientId, e.getIdentityKey());
        database.markAsSentFailed(record.getId());
        database.markAsPush(record.getId());
        RetrieveProfileJob.enqueue(recipientId);
    } catch (ProofRequiredException e) {
        handleProofRequiredException(e, record.getRecipient(), record.getThreadId(), messageId, false);
    }
    SignalLocalMetrics.IndividualMessageSend.onJobFinished(messageId);
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) SmsMessageRecord(org.thoughtcrime.securesms.database.model.SmsMessageRecord) ExpiringMessageManager(org.thoughtcrime.securesms.service.ExpiringMessageManager) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) UnidentifiedAccessMode(org.thoughtcrime.securesms.database.RecipientDatabase.UnidentifiedAccessMode) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Aggregations

MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)94 Recipient (org.thoughtcrime.securesms.recipients.Recipient)36 InsertResult (org.thoughtcrime.securesms.database.MessageDatabase.InsertResult)28 SyncMessageId (org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)26 MmsException (org.thoughtcrime.securesms.mms.MmsException)26 Nullable (androidx.annotation.Nullable)22 OutgoingMediaMessage (org.thoughtcrime.securesms.mms.OutgoingMediaMessage)22 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)22 MessageId (org.thoughtcrime.securesms.database.model.MessageId)20 NoSuchMessageException (org.thoughtcrime.securesms.database.NoSuchMessageException)18 Attachment (org.thoughtcrime.securesms.attachments.Attachment)16 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)16 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)16 WorkerThread (androidx.annotation.WorkerThread)14 IOException (java.io.IOException)14 List (java.util.List)14 MessageRecord (org.thoughtcrime.securesms.database.model.MessageRecord)14 SmsMessageRecord (org.thoughtcrime.securesms.database.model.SmsMessageRecord)14 ArrayList (java.util.ArrayList)12 LinkedList (java.util.LinkedList)12