Search in sources :

Example 1 with AttachmentDownloadJob

use of org.thoughtcrime.securesms.jobs.AttachmentDownloadJob in project Signal-Android by WhisperSystems.

the class MessageContentProcessor method handleMediaMessage.

@Nullable
private MessageId handleMediaMessage(@NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message, @NonNull Optional<Long> smsMessageId, @NonNull Recipient senderRecipient, @NonNull Recipient threadRecipient, long receivedTime) throws StorageFailedException {
    log(message.getTimestamp(), "Media message.");
    notifyTypingStoppedFromIncomingMessage(senderRecipient, threadRecipient, content.getSenderDevice());
    Optional<InsertResult> insertResult;
    MessageDatabase database = SignalDatabase.mms();
    database.beginTransaction();
    try {
        Optional<QuoteModel> quote = getValidatedQuote(message.getQuote());
        Optional<List<Contact>> sharedContacts = getContacts(message.getSharedContacts());
        Optional<List<LinkPreview>> linkPreviews = getLinkPreviews(message.getPreviews(), message.getBody().or(""));
        Optional<List<Mention>> mentions = getMentions(message.getMentions());
        Optional<Attachment> sticker = getStickerAttachment(message.getSticker());
        IncomingMediaMessage mediaMessage = new IncomingMediaMessage(senderRecipient.getId(), message.getTimestamp(), content.getServerReceivedTimestamp(), receivedTime, -1, TimeUnit.SECONDS.toMillis(message.getExpiresInSeconds()), false, message.isViewOnce(), content.isNeedsReceipt(), message.getBody(), message.getGroupContext(), message.getAttachments(), quote, sharedContacts, linkPreviews, mentions, sticker, content.getServerUuid());
        insertResult = database.insertSecureDecryptedMessageInbox(mediaMessage, -1);
        if (insertResult.isPresent()) {
            if (smsMessageId.isPresent()) {
                SignalDatabase.sms().deleteMessage(smsMessageId.get());
            }
            database.setTransactionSuccessful();
        }
    } catch (MmsException e) {
        throw new StorageFailedException(e, content.getSender().getIdentifier(), content.getSenderDevice());
    } finally {
        database.endTransaction();
    }
    if (insertResult.isPresent()) {
        List<DatabaseAttachment> allAttachments = SignalDatabase.attachments().getAttachmentsForMessage(insertResult.get().getMessageId());
        List<DatabaseAttachment> stickerAttachments = Stream.of(allAttachments).filter(Attachment::isSticker).toList();
        List<DatabaseAttachment> attachments = Stream.of(allAttachments).filterNot(Attachment::isSticker).toList();
        forceStickerDownloadIfNecessary(insertResult.get().getMessageId(), stickerAttachments);
        for (DatabaseAttachment attachment : attachments) {
            ApplicationDependencies.getJobManager().add(new AttachmentDownloadJob(insertResult.get().getMessageId(), attachment.getAttachmentId(), false));
        }
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        TrimThreadJob.enqueueAsync(insertResult.get().getThreadId());
        if (message.isViewOnce()) {
            ApplicationDependencies.getViewOnceMessageManager().scheduleIfNecessary();
        }
        return new MessageId(insertResult.get().getMessageId(), true);
    } else {
        return null;
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) PointerAttachment(org.thoughtcrime.securesms.attachments.PointerAttachment) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) TombstoneAttachment(org.thoughtcrime.securesms.attachments.TombstoneAttachment) QuoteModel(org.thoughtcrime.securesms.mms.QuoteModel) MmsException(org.thoughtcrime.securesms.mms.MmsException) AttachmentDownloadJob(org.thoughtcrime.securesms.jobs.AttachmentDownloadJob) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId) Nullable(androidx.annotation.Nullable)

Example 2 with AttachmentDownloadJob

use of org.thoughtcrime.securesms.jobs.AttachmentDownloadJob in project Signal-Android by WhisperSystems.

the class MessageContentProcessor method handleSynchronizeSentMediaMessage.

private long handleSynchronizeSentMediaMessage(@NonNull SentTranscriptMessage message, long envelopeTimestamp) throws MmsException, BadGroupIdException {
    log(envelopeTimestamp, "Synchronize sent media message for " + message.getTimestamp());
    MessageDatabase database = SignalDatabase.mms();
    Recipient recipients = getSyncMessageDestination(message);
    Optional<QuoteModel> quote = getValidatedQuote(message.getMessage().getQuote());
    Optional<Attachment> sticker = getStickerAttachment(message.getMessage().getSticker());
    Optional<List<Contact>> sharedContacts = getContacts(message.getMessage().getSharedContacts());
    Optional<List<LinkPreview>> previews = getLinkPreviews(message.getMessage().getPreviews(), message.getMessage().getBody().or(""));
    Optional<List<Mention>> mentions = getMentions(message.getMessage().getMentions());
    boolean viewOnce = message.getMessage().isViewOnce();
    List<Attachment> syncAttachments = viewOnce ? Collections.singletonList(new TombstoneAttachment(MediaUtil.VIEW_ONCE, false)) : PointerAttachment.forPointers(message.getMessage().getAttachments());
    if (sticker.isPresent()) {
        syncAttachments.add(sticker.get());
    }
    OutgoingMediaMessage mediaMessage = new OutgoingMediaMessage(recipients, message.getMessage().getBody().orNull(), syncAttachments, message.getTimestamp(), -1, TimeUnit.SECONDS.toMillis(message.getMessage().getExpiresInSeconds()), viewOnce, ThreadDatabase.DistributionTypes.DEFAULT, quote.orNull(), sharedContacts.or(Collections.emptyList()), previews.or(Collections.emptyList()), mentions.or(Collections.emptyList()), Collections.emptySet(), Collections.emptySet());
    mediaMessage = new OutgoingSecureMediaMessage(mediaMessage);
    if (recipients.getExpiresInSeconds() != message.getMessage().getExpiresInSeconds()) {
        handleSynchronizeSentExpirationUpdate(message);
    }
    long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipients);
    long messageId;
    List<DatabaseAttachment> attachments;
    List<DatabaseAttachment> stickerAttachments;
    database.beginTransaction();
    try {
        messageId = database.insertMessageOutbox(mediaMessage, threadId, false, GroupReceiptDatabase.STATUS_UNKNOWN, null);
        if (recipients.isGroup()) {
            updateGroupReceiptStatus(message, messageId, recipients.requireGroupId());
        } else {
            database.markUnidentified(messageId, isUnidentified(message, recipients));
        }
        database.markAsSent(messageId, true);
        List<DatabaseAttachment> allAttachments = SignalDatabase.attachments().getAttachmentsForMessage(messageId);
        stickerAttachments = Stream.of(allAttachments).filter(Attachment::isSticker).toList();
        attachments = Stream.of(allAttachments).filterNot(Attachment::isSticker).toList();
        if (message.getMessage().getExpiresInSeconds() > 0) {
            database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
            ApplicationDependencies.getExpiringMessageManager().scheduleDeletion(messageId, true, message.getExpirationStartTimestamp(), TimeUnit.SECONDS.toMillis(message.getMessage().getExpiresInSeconds()));
        }
        if (recipients.isSelf()) {
            SyncMessageId id = new SyncMessageId(recipients.getId(), message.getTimestamp());
            SignalDatabase.mmsSms().incrementDeliveryReceiptCount(id, System.currentTimeMillis());
            SignalDatabase.mmsSms().incrementReadReceiptCount(id, System.currentTimeMillis());
        }
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
    for (DatabaseAttachment attachment : attachments) {
        ApplicationDependencies.getJobManager().add(new AttachmentDownloadJob(messageId, attachment.getAttachmentId(), false));
    }
    forceStickerDownloadIfNecessary(messageId, stickerAttachments);
    return threadId;
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) PointerAttachment(org.thoughtcrime.securesms.attachments.PointerAttachment) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) TombstoneAttachment(org.thoughtcrime.securesms.attachments.TombstoneAttachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) QuoteModel(org.thoughtcrime.securesms.mms.QuoteModel) OutgoingSecureMediaMessage(org.thoughtcrime.securesms.mms.OutgoingSecureMediaMessage) TombstoneAttachment(org.thoughtcrime.securesms.attachments.TombstoneAttachment) AttachmentDownloadJob(org.thoughtcrime.securesms.jobs.AttachmentDownloadJob) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Example 3 with AttachmentDownloadJob

use of org.thoughtcrime.securesms.jobs.AttachmentDownloadJob in project Signal-Android by WhisperSystems.

the class MessageContentProcessor method forceStickerDownloadIfNecessary.

private void forceStickerDownloadIfNecessary(long messageId, List<DatabaseAttachment> stickerAttachments) {
    if (stickerAttachments.isEmpty())
        return;
    DatabaseAttachment stickerAttachment = stickerAttachments.get(0);
    if (stickerAttachment.getTransferState() != AttachmentDatabase.TRANSFER_PROGRESS_DONE) {
        AttachmentDownloadJob downloadJob = new AttachmentDownloadJob(messageId, stickerAttachment.getAttachmentId(), true);
        try {
            downloadJob.setContext(context);
            downloadJob.doWork();
        } catch (Exception e) {
            warn("Failed to download sticker inline. Scheduling.");
            ApplicationDependencies.getJobManager().add(downloadJob);
        }
    }
}
Also used : DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) AttachmentDownloadJob(org.thoughtcrime.securesms.jobs.AttachmentDownloadJob) GroupChangeBusyException(org.thoughtcrime.securesms.groups.GroupChangeBusyException) IOException(java.io.IOException) SerializationException(com.mobilecoin.lib.exceptions.SerializationException) GroupNotAMemberException(org.thoughtcrime.securesms.groups.GroupNotAMemberException) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException) MmsException(org.thoughtcrime.securesms.mms.MmsException)

Example 4 with AttachmentDownloadJob

use of org.thoughtcrime.securesms.jobs.AttachmentDownloadJob in project Signal-Android by WhisperSystems.

the class LegacyMigrationJob method schedulePendingIncomingParts.

private void schedulePendingIncomingParts(Context context) {
    final AttachmentDatabase attachmentDb = SignalDatabase.attachments();
    final MessageDatabase mmsDb = SignalDatabase.mms();
    final List<DatabaseAttachment> pendingAttachments = SignalDatabase.attachments().getPendingAttachments();
    Log.i(TAG, pendingAttachments.size() + " pending parts.");
    for (DatabaseAttachment attachment : pendingAttachments) {
        final Reader reader = MmsDatabase.readerFor(mmsDb.getMessageCursor(attachment.getMmsId()));
        final MessageRecord record = reader.getNext();
        if (attachment.hasData()) {
            Log.i(TAG, "corrected a pending media part " + attachment.getAttachmentId() + "that already had data.");
            attachmentDb.setTransferState(attachment.getMmsId(), attachment.getAttachmentId(), AttachmentDatabase.TRANSFER_PROGRESS_DONE);
        } else if (record != null && !record.isOutgoing() && record.isPush()) {
            Log.i(TAG, "queuing new attachment download job for incoming push part " + attachment.getAttachmentId() + ".");
            ApplicationDependencies.getJobManager().add(new AttachmentDownloadJob(attachment.getMmsId(), attachment.getAttachmentId(), false));
        }
        reader.close();
    }
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) AttachmentDownloadJob(org.thoughtcrime.securesms.jobs.AttachmentDownloadJob) Reader(org.thoughtcrime.securesms.database.MmsDatabase.Reader) MessageRecord(org.thoughtcrime.securesms.database.model.MessageRecord) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase)

Aggregations

DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)4 AttachmentDownloadJob (org.thoughtcrime.securesms.jobs.AttachmentDownloadJob)4 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Attachment (org.thoughtcrime.securesms.attachments.Attachment)2 PointerAttachment (org.thoughtcrime.securesms.attachments.PointerAttachment)2 TombstoneAttachment (org.thoughtcrime.securesms.attachments.TombstoneAttachment)2 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)2 SyncMessageId (org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)2 MmsException (org.thoughtcrime.securesms.mms.MmsException)2 QuoteModel (org.thoughtcrime.securesms.mms.QuoteModel)2 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)2 Nullable (androidx.annotation.Nullable)1 SerializationException (com.mobilecoin.lib.exceptions.SerializationException)1 IOException (java.io.IOException)1 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)1 InsertResult (org.thoughtcrime.securesms.database.MessageDatabase.InsertResult)1 Reader (org.thoughtcrime.securesms.database.MmsDatabase.Reader)1