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;
}
}
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;
}
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);
}
}
}
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();
}
}
Aggregations