Search in sources :

Example 21 with InsertResult

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

the class MmsDownloadJob method storeRetrievedMms.

private void storeRetrievedMms(String contentLocation, long messageId, long threadId, RetrieveConf retrieved, int subscriptionId, @Nullable RecipientId notificationFrom) throws MmsException {
    MessageDatabase database = SignalDatabase.mms();
    Optional<GroupId> group = Optional.absent();
    Set<RecipientId> members = new HashSet<>();
    String body = null;
    List<Attachment> attachments = new LinkedList<>();
    List<Contact> sharedContacts = new LinkedList<>();
    RecipientId from = null;
    if (retrieved.getFrom() != null) {
        from = Recipient.external(context, Util.toIsoString(retrieved.getFrom().getTextString())).getId();
    } else if (notificationFrom != null) {
        from = notificationFrom;
    }
    if (retrieved.getTo() != null) {
        for (EncodedStringValue toValue : retrieved.getTo()) {
            members.add(Recipient.external(context, Util.toIsoString(toValue.getTextString())).getId());
        }
    }
    if (retrieved.getCc() != null) {
        for (EncodedStringValue ccValue : retrieved.getCc()) {
            members.add(Recipient.external(context, Util.toIsoString(ccValue.getTextString())).getId());
        }
    }
    if (from != null) {
        members.add(from);
    }
    members.add(Recipient.self().getId());
    if (retrieved.getBody() != null) {
        body = PartParser.getMessageText(retrieved.getBody());
        PduBody media = PartParser.getSupportedMediaParts(retrieved.getBody());
        for (int i = 0; i < media.getPartsNum(); i++) {
            PduPart part = media.getPart(i);
            if (part.getData() != null) {
                if (Util.toIsoString(part.getContentType()).toLowerCase().equals(MediaUtil.VCARD)) {
                    sharedContacts.addAll(VCardUtil.parseContacts(new String(part.getData())));
                } else {
                    Uri uri = BlobProvider.getInstance().forData(part.getData()).createForSingleUseInMemory();
                    String name = null;
                    if (part.getName() != null)
                        name = Util.toIsoString(part.getName());
                    attachments.add(new UriAttachment(uri, Util.toIsoString(part.getContentType()), AttachmentDatabase.TRANSFER_PROGRESS_DONE, part.getData().length, name, false, false, false, false, null, null, null, null, null));
                }
            }
        }
    }
    if (members.size() > 2) {
        List<RecipientId> recipients = new ArrayList<>(members);
        group = Optional.of(SignalDatabase.groups().getOrCreateMmsGroupForMembers(recipients));
    }
    IncomingMediaMessage message = new IncomingMediaMessage(from, group, body, TimeUnit.SECONDS.toMillis(retrieved.getDate()), -1, System.currentTimeMillis(), attachments, subscriptionId, 0, false, false, false, Optional.of(sharedContacts));
    Optional<InsertResult> insertResult = database.insertMessageInbox(message, contentLocation, threadId);
    if (insertResult.isPresent()) {
        database.deleteMessage(messageId);
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) PduBody(com.google.android.mms.pdu_alt.PduBody) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) ArrayList(java.util.ArrayList) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Uri(android.net.Uri) LinkedList(java.util.LinkedList) GroupId(org.thoughtcrime.securesms.groups.GroupId) Contact(org.thoughtcrime.securesms.contactshare.Contact) PduPart(com.google.android.mms.pdu_alt.PduPart) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) HashSet(java.util.HashSet)

Example 22 with InsertResult

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

the class MessageContentProcessor method handleExpirationUpdate.

@Nullable
private MessageId handleExpirationUpdate(@NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message, @NonNull Optional<Long> smsMessageId, @NonNull Optional<GroupId> groupId, @NonNull Recipient senderRecipient, @NonNull Recipient threadRecipient, long receivedTime) throws StorageFailedException {
    log(content.getTimestamp(), "Expiration update.");
    if (groupId.isPresent() && groupId.get().isV2()) {
        warn(String.valueOf(content.getTimestamp()), "Expiration update received for GV2. Ignoring.");
        return null;
    }
    int expiresInSeconds = message.getExpiresInSeconds();
    Optional<SignalServiceGroupContext> groupContext = message.getGroupContext();
    if (threadRecipient.getExpiresInSeconds() == expiresInSeconds) {
        log(String.valueOf(content.getTimestamp()), "No change in message expiry for group. Ignoring.");
        return null;
    }
    try {
        MessageDatabase database = SignalDatabase.mms();
        IncomingMediaMessage mediaMessage = new IncomingMediaMessage(senderRecipient.getId(), content.getTimestamp(), content.getServerReceivedTimestamp(), receivedTime, -1, expiresInSeconds * 1000L, true, false, content.isNeedsReceipt(), Optional.absent(), groupContext, Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), content.getServerUuid());
        Optional<InsertResult> insertResult = database.insertSecureDecryptedMessageInbox(mediaMessage, -1);
        SignalDatabase.recipients().setExpireMessages(threadRecipient.getId(), expiresInSeconds);
        if (smsMessageId.isPresent()) {
            SignalDatabase.sms().deleteMessage(smsMessageId.get());
        }
        if (insertResult.isPresent()) {
            return new MessageId(insertResult.get().getMessageId(), true);
        }
    } catch (MmsException e) {
        throw new StorageFailedException(e, content.getSender().getIdentifier(), content.getSenderDevice());
    }
    return null;
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) SuppressLint(android.annotation.SuppressLint) SignalServiceGroupContext(org.whispersystems.signalservice.api.messages.SignalServiceGroupContext) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId) Nullable(androidx.annotation.Nullable)

Example 23 with InsertResult

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

the class MessageContentProcessor method handleTextMessage.

@Nullable
private MessageId handleTextMessage(@NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message, @NonNull Optional<Long> smsMessageId, @NonNull Optional<GroupId> groupId, @NonNull Recipient senderRecipient, @NonNull Recipient threadRecipient, long receivedTime) throws StorageFailedException {
    log(message.getTimestamp(), "Text message.");
    MessageDatabase database = SignalDatabase.sms();
    String body = message.getBody().isPresent() ? message.getBody().get() : "";
    if (message.getExpiresInSeconds() != threadRecipient.getExpiresInSeconds()) {
        handleExpirationUpdate(content, message, Optional.absent(), groupId, senderRecipient, threadRecipient, receivedTime);
    }
    Optional<InsertResult> insertResult;
    if (smsMessageId.isPresent() && !message.getGroupContext().isPresent()) {
        insertResult = Optional.of(database.updateBundleMessageBody(smsMessageId.get(), body));
    } else {
        notifyTypingStoppedFromIncomingMessage(senderRecipient, threadRecipient, content.getSenderDevice());
        IncomingTextMessage textMessage = new IncomingTextMessage(senderRecipient.getId(), content.getSenderDevice(), message.getTimestamp(), content.getServerReceivedTimestamp(), receivedTime, body, groupId, TimeUnit.SECONDS.toMillis(message.getExpiresInSeconds()), content.isNeedsReceipt(), content.getServerUuid());
        textMessage = new IncomingEncryptedMessage(textMessage, body);
        insertResult = database.insertMessageInbox(textMessage);
        if (smsMessageId.isPresent())
            database.deleteMessage(smsMessageId.get());
    }
    if (insertResult.isPresent()) {
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        return new MessageId(insertResult.get().getMessageId(), false);
    } else {
        return null;
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) IncomingEncryptedMessage(org.thoughtcrime.securesms.sms.IncomingEncryptedMessage) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId) Nullable(androidx.annotation.Nullable)

Example 24 with InsertResult

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

the class MessageContentProcessor method handleLegacyMessage.

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

Example 25 with InsertResult

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

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)

Aggregations

InsertResult (org.thoughtcrime.securesms.database.MessageDatabase.InsertResult)30 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)26 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)12 Nullable (androidx.annotation.Nullable)10 SyncMessageId (org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)8 MessageId (org.thoughtcrime.securesms.database.model.MessageId)8 IncomingMediaMessage (org.thoughtcrime.securesms.mms.IncomingMediaMessage)6 MmsException (org.thoughtcrime.securesms.mms.MmsException)6 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)6 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)4 SuppressLint (android.annotation.SuppressLint)2 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 Intent (android.content.Intent)2 Uri (android.net.Uri)2 EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)2