Search in sources :

Example 36 with MessageDatabase

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

the class MessageSender method send.

public static long send(final Context context, final OutgoingMediaMessage message, final long threadId, final boolean forceSms, @Nullable final String metricId, final SmsDatabase.InsertListener insertListener) {
    Log.i(TAG, "Sending media message to " + message.getRecipient().getId() + ", thread: " + threadId);
    try {
        ThreadDatabase threadDatabase = SignalDatabase.threads();
        MessageDatabase database = SignalDatabase.mms();
        long allocatedThreadId = threadDatabase.getOrCreateValidThreadId(message.getRecipient(), threadId, message.getDistributionType());
        Recipient recipient = message.getRecipient();
        long messageId = database.insertMessageOutbox(applyUniversalExpireTimerIfNecessary(context, recipient, message, allocatedThreadId), allocatedThreadId, forceSms, insertListener);
        if (message.getRecipient().isGroup() && message.getAttachments().isEmpty() && message.getLinkPreviews().isEmpty() && message.getSharedContacts().isEmpty()) {
            SignalLocalMetrics.GroupMessageSend.onInsertedIntoDatabase(messageId, metricId);
        } else {
            SignalLocalMetrics.GroupMessageSend.cancel(metricId);
        }
        sendMediaMessage(context, recipient, forceSms, messageId, Collections.emptyList());
        onMessageSent();
        threadDatabase.update(threadId, true);
        return allocatedThreadId;
    } catch (MmsException e) {
        Log.w(TAG, e);
        return threadId;
    }
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase)

Example 37 with MessageDatabase

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

the class IdentityUtil method markIdentityVerified.

public static void markIdentityVerified(Context context, Recipient recipient, boolean verified, boolean remote) {
    long time = System.currentTimeMillis();
    MessageDatabase smsDatabase = SignalDatabase.sms();
    GroupDatabase groupDatabase = SignalDatabase.groups();
    try (GroupDatabase.Reader reader = groupDatabase.getGroups()) {
        GroupDatabase.GroupRecord groupRecord;
        while ((groupRecord = reader.getNext()) != null) {
            if (groupRecord.getMembers().contains(recipient.getId()) && groupRecord.isActive() && !groupRecord.isMms()) {
                if (remote) {
                    IncomingTextMessage incoming = new IncomingTextMessage(recipient.getId(), 1, time, -1, time, null, Optional.of(groupRecord.getId()), 0, false, null);
                    if (verified)
                        incoming = new IncomingIdentityVerifiedMessage(incoming);
                    else
                        incoming = new IncomingIdentityDefaultMessage(incoming);
                    smsDatabase.insertMessageInbox(incoming);
                } else {
                    RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromGroupId(groupRecord.getId());
                    Recipient groupRecipient = Recipient.resolved(recipientId);
                    long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient);
                    OutgoingTextMessage outgoing;
                    if (verified)
                        outgoing = new OutgoingIdentityVerifiedMessage(recipient);
                    else
                        outgoing = new OutgoingIdentityDefaultMessage(recipient);
                    SignalDatabase.sms().insertMessageOutbox(threadId, outgoing, false, time, null);
                    SignalDatabase.threads().update(threadId, true);
                }
            }
        }
    }
    if (remote) {
        IncomingTextMessage incoming = new IncomingTextMessage(recipient.getId(), 1, time, -1, time, null, Optional.absent(), 0, false, null);
        if (verified)
            incoming = new IncomingIdentityVerifiedMessage(incoming);
        else
            incoming = new IncomingIdentityDefaultMessage(incoming);
        smsDatabase.insertMessageInbox(incoming);
    } else {
        OutgoingTextMessage outgoing;
        if (verified)
            outgoing = new OutgoingIdentityVerifiedMessage(recipient);
        else
            outgoing = new OutgoingIdentityDefaultMessage(recipient);
        long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
        Log.i(TAG, "Inserting verified outbox...");
        SignalDatabase.sms().insertMessageOutbox(threadId, outgoing, false, time, null);
        SignalDatabase.threads().update(threadId, true);
    }
}
Also used : OutgoingTextMessage(org.thoughtcrime.securesms.sms.OutgoingTextMessage) OutgoingIdentityVerifiedMessage(org.thoughtcrime.securesms.sms.OutgoingIdentityVerifiedMessage) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) IncomingIdentityVerifiedMessage(org.thoughtcrime.securesms.sms.IncomingIdentityVerifiedMessage) OutgoingIdentityDefaultMessage(org.thoughtcrime.securesms.sms.OutgoingIdentityDefaultMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) IncomingIdentityDefaultMessage(org.thoughtcrime.securesms.sms.IncomingIdentityDefaultMessage)

Example 38 with MessageDatabase

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

the class IdentityUtil method markIdentityUpdate.

public static void markIdentityUpdate(@NonNull Context context, @NonNull RecipientId recipientId) {
    long time = System.currentTimeMillis();
    MessageDatabase smsDatabase = SignalDatabase.sms();
    GroupDatabase groupDatabase = SignalDatabase.groups();
    try (GroupDatabase.Reader reader = groupDatabase.getGroups()) {
        GroupDatabase.GroupRecord groupRecord;
        while ((groupRecord = reader.getNext()) != null) {
            if (groupRecord.getMembers().contains(recipientId) && groupRecord.isActive()) {
                IncomingTextMessage incoming = new IncomingTextMessage(recipientId, 1, time, time, time, null, Optional.of(groupRecord.getId()), 0, false, null);
                IncomingIdentityUpdateMessage groupUpdate = new IncomingIdentityUpdateMessage(incoming);
                smsDatabase.insertMessageInbox(groupUpdate);
            }
        }
    }
    IncomingTextMessage incoming = new IncomingTextMessage(recipientId, 1, time, -1, time, null, Optional.absent(), 0, false, null);
    IncomingIdentityUpdateMessage individualUpdate = new IncomingIdentityUpdateMessage(incoming);
    Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(individualUpdate);
    if (insertResult.isPresent()) {
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
    }
}
Also used : IncomingIdentityUpdateMessage(org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage) InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase)

Example 39 with MessageDatabase

use of org.thoughtcrime.securesms.database.MessageDatabase 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;
}
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 40 with MessageDatabase

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

the class MessageContentProcessor method handleUnsupportedDataMessage.

private void handleUnsupportedDataMessage(@NonNull String sender, int senderDevice, @NonNull Optional<GroupId> groupId, long timestamp, @NonNull Optional<Long> smsMessageId) {
    log(timestamp, "Unsupported data message.");
    MessageDatabase smsDatabase = SignalDatabase.sms();
    if (!smsMessageId.isPresent()) {
        Optional<InsertResult> insertResult = insertPlaceholder(sender, senderDevice, timestamp, groupId);
        if (insertResult.isPresent()) {
            smsDatabase.markAsUnsupportedProtocolVersion(insertResult.get().getMessageId());
            ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        }
    } else {
        smsDatabase.markAsNoSession(smsMessageId.get());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase)

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