use of org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId in project Signal-Android by signalapp.
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.database.MessageDatabase.SyncMessageId in project Signal-Android by signalapp.
the class MessageContentProcessor method handleViewedReceipt.
private void handleViewedReceipt(@NonNull SignalServiceContent content, @NonNull SignalServiceReceiptMessage message, @NonNull Recipient senderRecipient) {
if (!TextSecurePreferences.isReadReceiptsEnabled(context)) {
log("Ignoring viewed receipts for IDs: " + Util.join(message.getTimestamps(), ", "));
return;
}
log(TAG, "Processing viewed receipts. Sender: " + senderRecipient.getId() + ", Device: " + content.getSenderDevice() + ", Timestamps: " + Util.join(message.getTimestamps(), ", "));
List<SyncMessageId> ids = Stream.of(message.getTimestamps()).map(t -> new SyncMessageId(senderRecipient.getId(), t)).toList();
Collection<SyncMessageId> unhandled = SignalDatabase.mmsSms().incrementViewedReceiptCounts(ids, content.getTimestamp());
for (SyncMessageId id : unhandled) {
warn(String.valueOf(content.getTimestamp()), "[handleViewedReceipt] Could not find matching message! timestamp: " + id.getTimetamp() + " author: " + senderRecipient.getId());
ApplicationDependencies.getEarlyMessageCache().store(senderRecipient.getId(), id.getTimetamp(), content);
}
}
use of org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId in project Signal-Android by signalapp.
the class MessageContentProcessor method handleSynchronizeSentTextMessage.
private long handleSynchronizeSentTextMessage(@NonNull SentTranscriptMessage message, long envelopeTimestamp) throws MmsException, BadGroupIdException {
log(envelopeTimestamp, "Synchronize sent text message for " + message.getTimestamp());
Recipient recipient = getSyncMessageDestination(message);
String body = message.getMessage().getBody().or("");
long expiresInMillis = TimeUnit.SECONDS.toMillis(message.getMessage().getExpiresInSeconds());
if (recipient.getExpiresInSeconds() != message.getMessage().getExpiresInSeconds()) {
handleSynchronizeSentExpirationUpdate(message);
}
long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
boolean isGroup = recipient.isGroup();
MessageDatabase database;
long messageId;
if (isGroup) {
OutgoingMediaMessage outgoingMediaMessage = new OutgoingMediaMessage(recipient, new SlideDeck(), body, message.getTimestamp(), -1, expiresInMillis, false, ThreadDatabase.DistributionTypes.DEFAULT, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
outgoingMediaMessage = new OutgoingSecureMediaMessage(outgoingMediaMessage);
messageId = SignalDatabase.mms().insertMessageOutbox(outgoingMediaMessage, threadId, false, GroupReceiptDatabase.STATUS_UNKNOWN, null);
database = SignalDatabase.mms();
updateGroupReceiptStatus(message, messageId, recipient.requireGroupId());
} else {
OutgoingTextMessage outgoingTextMessage = new OutgoingEncryptedMessage(recipient, body, expiresInMillis);
messageId = SignalDatabase.sms().insertMessageOutbox(threadId, outgoingTextMessage, false, message.getTimestamp(), null);
database = SignalDatabase.sms();
database.markUnidentified(messageId, isUnidentified(message, recipient));
SignalDatabase.threads().update(threadId, true);
}
database.markAsSent(messageId, true);
if (expiresInMillis > 0) {
database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
ApplicationDependencies.getExpiringMessageManager().scheduleDeletion(messageId, isGroup, message.getExpirationStartTimestamp(), expiresInMillis);
}
if (recipient.isSelf()) {
SyncMessageId id = new SyncMessageId(recipient.getId(), message.getTimestamp());
SignalDatabase.mmsSms().incrementDeliveryReceiptCount(id, System.currentTimeMillis());
SignalDatabase.mmsSms().incrementReadReceiptCount(id, System.currentTimeMillis());
}
return threadId;
}
use of org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId in project Signal-Android by WhisperSystems.
the class MmsSmsDatabase method incrementReceiptCounts.
/**
* Wraps multiple receipt updates in a transaction and triggers the proper updates.
*
* @return All of the messages that didn't result in updates.
*/
@NonNull
private Collection<SyncMessageId> incrementReceiptCounts(@NonNull List<SyncMessageId> syncMessageIds, long timestamp, @NonNull MessageDatabase.ReceiptType receiptType) {
SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
ThreadDatabase threadDatabase = SignalDatabase.threads();
Set<MessageUpdate> messageUpdates = new HashSet<>();
Collection<SyncMessageId> unhandled = new HashSet<>();
db.beginTransaction();
try {
for (SyncMessageId id : syncMessageIds) {
Set<MessageUpdate> updates = incrementReceiptCountInternal(id, timestamp, receiptType);
if (updates.size() > 0) {
messageUpdates.addAll(updates);
} else {
unhandled.add(id);
}
}
for (MessageUpdate update : messageUpdates) {
threadDatabase.updateSilently(update.getThreadId(), false);
ApplicationDependencies.getDatabaseObserver().notifyMessageUpdateObservers(update.getMessageId());
ApplicationDependencies.getDatabaseObserver().notifyVerboseConversationListeners(Collections.singleton(update.getThreadId()));
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
if (messageUpdates.size() > 0) {
notifyConversationListListeners();
}
}
return unhandled;
}
use of org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId in project Signal-Android by WhisperSystems.
the class MmsSmsDatabase method setTimestampRead.
public void setTimestampRead(@NonNull Recipient senderRecipient, @NonNull List<ReadMessage> readMessages, long proposedExpireStarted, @NonNull Map<Long, Long> threadToLatestRead) {
SQLiteDatabase db = getWritableDatabase();
List<Pair<Long, Long>> expiringText = new LinkedList<>();
List<Pair<Long, Long>> expiringMedia = new LinkedList<>();
Set<Long> updatedThreads = new HashSet<>();
db.beginTransaction();
try {
for (ReadMessage readMessage : readMessages) {
TimestampReadResult textResult = SignalDatabase.sms().setTimestampRead(new SyncMessageId(senderRecipient.getId(), readMessage.getTimestamp()), proposedExpireStarted, threadToLatestRead);
TimestampReadResult mediaResult = SignalDatabase.mms().setTimestampRead(new SyncMessageId(senderRecipient.getId(), readMessage.getTimestamp()), proposedExpireStarted, threadToLatestRead);
expiringText.addAll(textResult.expiring);
expiringMedia.addAll(mediaResult.expiring);
updatedThreads.addAll(textResult.threads);
updatedThreads.addAll(mediaResult.threads);
}
for (long threadId : updatedThreads) {
SignalDatabase.threads().updateReadState(threadId);
SignalDatabase.threads().setLastSeen(threadId);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
for (Pair<Long, Long> expiringMessage : expiringText) {
ApplicationDependencies.getExpiringMessageManager().scheduleDeletion(expiringMessage.first(), false, proposedExpireStarted, expiringMessage.second());
}
for (Pair<Long, Long> expiringMessage : expiringMedia) {
ApplicationDependencies.getExpiringMessageManager().scheduleDeletion(expiringMessage.first(), true, proposedExpireStarted, expiringMessage.second());
}
for (long threadId : updatedThreads) {
notifyConversationListeners(threadId);
}
}
Aggregations