Search in sources :

Example 26 with MessageId

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

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 27 with MessageId

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

the class MessageContentProcessor method handleMessage.

private void handleMessage(@NonNull SignalServiceContent content, long timestamp, @NonNull Recipient senderRecipient, @NonNull Optional<Long> smsMessageId) throws IOException, GroupChangeBusyException {
    try {
        Recipient threadRecipient = getMessageDestination(content);
        if (shouldIgnore(content, senderRecipient, threadRecipient)) {
            log(content.getTimestamp(), "Ignoring message.");
            return;
        }
        PendingRetryReceiptModel pending = ApplicationDependencies.getPendingRetryReceiptCache().get(senderRecipient.getId(), content.getTimestamp());
        long receivedTime = handlePendingRetry(pending, content, threadRecipient);
        log(String.valueOf(content.getTimestamp()), "Beginning message processing. Sender: " + formatSender(senderRecipient, content));
        if (content.getDataMessage().isPresent()) {
            GroupDatabase groupDatabase = SignalDatabase.groups();
            SignalServiceDataMessage message = content.getDataMessage().get();
            boolean isMediaMessage = message.getAttachments().isPresent() || message.getQuote().isPresent() || message.getSharedContacts().isPresent() || message.getPreviews().isPresent() || message.getSticker().isPresent() || message.getMentions().isPresent();
            Optional<GroupId> groupId = GroupUtil.idFromGroupContext(message.getGroupContext());
            boolean isGv2Message = groupId.isPresent() && groupId.get().isV2();
            if (isGv2Message) {
                if (handleGv2PreProcessing(groupId.orNull().requireV2(), content, content.getDataMessage().get().getGroupContext().get().getGroupV2().get(), senderRecipient)) {
                    return;
                }
            }
            MessageId messageId = null;
            if (isInvalidMessage(message))
                handleInvalidMessage(content.getSender(), content.getSenderDevice(), groupId, content.getTimestamp(), smsMessageId);
            else if (message.isEndSession())
                messageId = handleEndSessionMessage(content, smsMessageId, senderRecipient);
            else if (message.isGroupV1Update())
                handleGroupV1Message(content, message, smsMessageId, groupId.get().requireV1(), senderRecipient, threadRecipient, receivedTime);
            else if (message.isExpirationUpdate())
                messageId = handleExpirationUpdate(content, message, smsMessageId, groupId, senderRecipient, threadRecipient, receivedTime);
            else if (message.getReaction().isPresent())
                messageId = handleReaction(content, message, senderRecipient);
            else if (message.getRemoteDelete().isPresent())
                messageId = handleRemoteDelete(content, message, senderRecipient);
            else if (message.getPayment().isPresent())
                handlePayment(content, message, senderRecipient);
            else if (message.getStoryContext().isPresent())
                handleStoryMessage(content);
            else if (isMediaMessage)
                messageId = handleMediaMessage(content, message, smsMessageId, senderRecipient, threadRecipient, receivedTime);
            else if (message.getBody().isPresent())
                messageId = handleTextMessage(content, message, smsMessageId, groupId, senderRecipient, threadRecipient, receivedTime);
            else if (Build.VERSION.SDK_INT > 19 && message.getGroupCallUpdate().isPresent())
                handleGroupCallUpdateMessage(content, message, groupId, senderRecipient);
            if (groupId.isPresent() && groupDatabase.isUnknownGroup(groupId.get())) {
                handleUnknownGroupMessage(content, message.getGroupContext().get(), senderRecipient);
            }
            if (message.getProfileKey().isPresent()) {
                handleProfileKey(content, message.getProfileKey().get(), senderRecipient);
            }
            if (content.isNeedsReceipt() && messageId != null) {
                handleNeedsDeliveryReceipt(content, message, messageId);
            } else if (!content.isNeedsReceipt()) {
                if (RecipientUtil.shouldHaveProfileKey(context, threadRecipient)) {
                    Log.w(TAG, "Received an unsealed sender message from " + senderRecipient.getId() + ", but they should already have our profile key. Correcting.");
                    if (groupId.isPresent() && groupId.get().isV2()) {
                        Log.i(TAG, "Message was to a GV2 group. Ensuring our group profile keys are up to date.");
                        ApplicationDependencies.getJobManager().startChain(new RefreshAttributesJob(false)).then(GroupV2UpdateSelfProfileKeyJob.withQueueLimits(groupId.get().requireV2())).enqueue();
                    } else if (!threadRecipient.isGroup()) {
                        Log.i(TAG, "Message was to a 1:1. Ensuring this user has our profile key.");
                        ApplicationDependencies.getJobManager().startChain(new RefreshAttributesJob(false)).then(ProfileKeySendJob.create(context, SignalDatabase.threads().getOrCreateThreadIdFor(threadRecipient), true)).enqueue();
                    }
                }
            }
        } else if (content.getSyncMessage().isPresent()) {
            TextSecurePreferences.setMultiDevice(context, true);
            SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
            if (syncMessage.getSent().isPresent())
                handleSynchronizeSentMessage(content, syncMessage.getSent().get(), senderRecipient);
            else if (syncMessage.getRequest().isPresent())
                handleSynchronizeRequestMessage(syncMessage.getRequest().get(), content.getTimestamp());
            else if (syncMessage.getRead().isPresent())
                handleSynchronizeReadMessage(syncMessage.getRead().get(), content.getTimestamp(), senderRecipient);
            else if (syncMessage.getViewed().isPresent())
                handleSynchronizeViewedMessage(syncMessage.getViewed().get(), content.getTimestamp());
            else if (syncMessage.getViewOnceOpen().isPresent())
                handleSynchronizeViewOnceOpenMessage(syncMessage.getViewOnceOpen().get(), content.getTimestamp());
            else if (syncMessage.getVerified().isPresent())
                handleSynchronizeVerifiedMessage(syncMessage.getVerified().get());
            else if (syncMessage.getStickerPackOperations().isPresent())
                handleSynchronizeStickerPackOperation(syncMessage.getStickerPackOperations().get(), content.getTimestamp());
            else if (syncMessage.getConfiguration().isPresent())
                handleSynchronizeConfigurationMessage(syncMessage.getConfiguration().get(), content.getTimestamp());
            else if (syncMessage.getBlockedList().isPresent())
                handleSynchronizeBlockedListMessage(syncMessage.getBlockedList().get());
            else if (syncMessage.getFetchType().isPresent())
                handleSynchronizeFetchMessage(syncMessage.getFetchType().get(), content.getTimestamp());
            else if (syncMessage.getMessageRequestResponse().isPresent())
                handleSynchronizeMessageRequestResponse(syncMessage.getMessageRequestResponse().get(), content.getTimestamp());
            else if (syncMessage.getOutgoingPaymentMessage().isPresent())
                handleSynchronizeOutgoingPayment(content, syncMessage.getOutgoingPaymentMessage().get());
            else if (syncMessage.getKeys().isPresent())
                handleSynchronizeKeys(syncMessage.getKeys().get(), content.getTimestamp());
            else if (syncMessage.getContacts().isPresent())
                handleSynchronizeContacts(syncMessage.getContacts().get(), content.getTimestamp());
            else
                warn(String.valueOf(content.getTimestamp()), "Contains no known sync types...");
        } else if (content.getCallMessage().isPresent()) {
            log(String.valueOf(content.getTimestamp()), "Got call message...");
            SignalServiceCallMessage message = content.getCallMessage().get();
            Optional<Integer> destinationDeviceId = message.getDestinationDeviceId();
            if (destinationDeviceId.isPresent() && destinationDeviceId.get() != SignalStore.account().getDeviceId()) {
                log(String.valueOf(content.getTimestamp()), String.format(Locale.US, "Ignoring call message that is not for this device! intended: %d, this: %d", destinationDeviceId.get(), SignalStore.account().getDeviceId()));
                return;
            }
            if (message.getOfferMessage().isPresent())
                handleCallOfferMessage(content, message.getOfferMessage().get(), smsMessageId, senderRecipient);
            else if (message.getAnswerMessage().isPresent())
                handleCallAnswerMessage(content, message.getAnswerMessage().get(), senderRecipient);
            else if (message.getIceUpdateMessages().isPresent())
                handleCallIceUpdateMessage(content, message.getIceUpdateMessages().get(), senderRecipient);
            else if (message.getHangupMessage().isPresent())
                handleCallHangupMessage(content, message.getHangupMessage().get(), smsMessageId, senderRecipient);
            else if (message.getBusyMessage().isPresent())
                handleCallBusyMessage(content, message.getBusyMessage().get(), senderRecipient);
            else if (message.getOpaqueMessage().isPresent())
                handleCallOpaqueMessage(content, message.getOpaqueMessage().get(), senderRecipient);
        } else if (content.getReceiptMessage().isPresent()) {
            SignalServiceReceiptMessage message = content.getReceiptMessage().get();
            if (message.isReadReceipt())
                handleReadReceipt(content, message, senderRecipient);
            else if (message.isDeliveryReceipt())
                handleDeliveryReceipt(content, message, senderRecipient);
            else if (message.isViewedReceipt())
                handleViewedReceipt(content, message, senderRecipient);
        } else if (content.getTypingMessage().isPresent()) {
            handleTypingMessage(content, content.getTypingMessage().get(), senderRecipient);
        } else if (content.getDecryptionErrorMessage().isPresent()) {
            handleRetryReceipt(content, content.getDecryptionErrorMessage().get(), senderRecipient);
        } else if (content.getSenderKeyDistributionMessage().isPresent()) {
        // Already handled, here in order to prevent unrecognized message log
        } else {
            warn(String.valueOf(content.getTimestamp()), "Got unrecognized message!");
        }
        resetRecipientToPush(senderRecipient);
        if (pending != null) {
            warn(content.getTimestamp(), "Pending retry was processed. Deleting.");
            ApplicationDependencies.getPendingRetryReceiptCache().delete(pending);
        }
    } catch (StorageFailedException e) {
        warn(String.valueOf(content.getTimestamp()), e);
        handleCorruptMessage(e.getSender(), e.getSenderDevice(), timestamp, smsMessageId);
    } catch (BadGroupIdException e) {
        warn(String.valueOf(content.getTimestamp()), "Ignoring message with bad group id", e);
    }
}
Also used : PendingRetryReceiptModel(org.thoughtcrime.securesms.database.model.PendingRetryReceiptModel) SignalServiceReceiptMessage(org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) GroupId(org.thoughtcrime.securesms.groups.GroupId) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) RefreshAttributesJob(org.thoughtcrime.securesms.jobs.RefreshAttributesJob) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) BadGroupIdException(org.thoughtcrime.securesms.groups.BadGroupIdException) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Example 28 with MessageId

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

the class ReactWithAnyEmojiViewModel method onEmojiSelected.

void onEmojiSelected(@NonNull String emoji) {
    if (messageId > 0) {
        SignalStore.emojiValues().setPreferredVariation(emoji);
        repository.addEmojiToMessage(emoji, new MessageId(messageId, isMms));
    }
}
Also used : MessageId(org.thoughtcrime.securesms.database.model.MessageId)

Example 29 with MessageId

use of org.thoughtcrime.securesms.database.model.MessageId in project Signal-Android by WhisperSystems.

the class MmsDatabase method markAsPendingInsecureSmsFallback.

@Override
public void markAsPendingInsecureSmsFallback(long messageId) {
    long threadId = getThreadIdForMessage(messageId);
    updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_PENDING_INSECURE_SMS_FALLBACK, Optional.of(threadId));
    ApplicationDependencies.getDatabaseObserver().notifyMessageUpdateObservers(new MessageId(messageId, true));
}
Also used : MessageId(org.thoughtcrime.securesms.database.model.MessageId)

Example 30 with MessageId

use of org.thoughtcrime.securesms.database.model.MessageId in project Signal-Android by WhisperSystems.

the class MmsDatabase method markAsRemoteDelete.

@Override
public void markAsRemoteDelete(long messageId) {
    SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
    long threadId;
    boolean deletedAttachments = false;
    db.beginTransaction();
    try {
        ContentValues values = new ContentValues();
        values.put(REMOTE_DELETED, 1);
        values.putNull(BODY);
        values.putNull(QUOTE_BODY);
        values.putNull(QUOTE_AUTHOR);
        values.putNull(QUOTE_ATTACHMENT);
        values.putNull(QUOTE_ID);
        values.putNull(LINK_PREVIEWS);
        values.putNull(SHARED_CONTACTS);
        db.update(TABLE_NAME, values, ID_WHERE, new String[] { String.valueOf(messageId) });
        deletedAttachments = SignalDatabase.attachments().deleteAttachmentsForMessage(messageId);
        SignalDatabase.mentions().deleteMentionsForMessage(messageId);
        SignalDatabase.messageLog().deleteAllRelatedToMessage(messageId, true);
        SignalDatabase.reactions().deleteReactions(new MessageId(messageId, true));
        threadId = getThreadIdForMessage(messageId);
        SignalDatabase.threads().update(threadId, false);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    ApplicationDependencies.getDatabaseObserver().notifyMessageUpdateObservers(new MessageId(messageId, true));
    ApplicationDependencies.getDatabaseObserver().notifyConversationListListeners();
    if (deletedAttachments) {
        ApplicationDependencies.getDatabaseObserver().notifyAttachmentObservers();
    }
}
Also used : ContentValues(android.content.ContentValues) MessageId(org.thoughtcrime.securesms.database.model.MessageId)

Aggregations

MessageId (org.thoughtcrime.securesms.database.model.MessageId)90 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)40 NonNull (androidx.annotation.NonNull)30 Recipient (org.thoughtcrime.securesms.recipients.Recipient)30 Nullable (androidx.annotation.Nullable)28 LinkedList (java.util.LinkedList)24 List (java.util.List)24 SyncMessageId (org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)24 Context (android.content.Context)22 IOException (java.io.IOException)22 Log (org.signal.core.util.logging.Log)22 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)22 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)22 ContentValues (android.content.ContentValues)20 Collections (java.util.Collections)20 Attachment (org.thoughtcrime.securesms.attachments.Attachment)20 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)20 Cursor (android.database.Cursor)18 ArrayList (java.util.ArrayList)18 TextSecurePreferences (org.thoughtcrime.securesms.util.TextSecurePreferences)18