Search in sources :

Example 1 with InsertResult

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

the class DirectoryHelper method notifyNewUsers.

private static void notifyNewUsers(@NonNull Context context, @NonNull Collection<RecipientId> newUsers) {
    if (!SignalStore.settings().isNotifyWhenContactJoinsSignal())
        return;
    for (RecipientId newUser : newUsers) {
        Recipient recipient = Recipient.resolved(newUser);
        if (!recipient.isSelf() && recipient.hasAUserSetDisplayName(context) && !hasSession(recipient.getId())) {
            IncomingJoinedMessage message = new IncomingJoinedMessage(recipient.getId());
            Optional<InsertResult> insertResult = SignalDatabase.sms().insertMessageInbox(message);
            if (insertResult.isPresent()) {
                int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                if (hour >= 9 && hour < 23) {
                    ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId(), true);
                } else {
                    Log.i(TAG, "Not notifying of a new user due to the time of day. (Hour: " + hour + ")");
                }
            }
        }
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IncomingJoinedMessage(org.thoughtcrime.securesms.sms.IncomingJoinedMessage)

Example 2 with InsertResult

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

the class SmsReceiveJob method onRun.

@Override
public void onRun() throws MigrationPendingException, RetryLaterException {
    Optional<IncomingTextMessage> message = assembleMessageFragments(pdus, subscriptionId);
    if (SignalStore.account().getE164() == null) {
        Log.i(TAG, "Received an SMS before we're registered...");
        if (message.isPresent()) {
            Optional<String> token = VerificationCodeParser.parse(message.get().getMessageBody());
            if (token.isPresent()) {
                Log.i(TAG, "Received something that looks like a registration SMS. Posting a notification and broadcast.");
                NotificationManager manager = ServiceUtil.getNotificationManager(context);
                Notification notification = buildPreRegistrationNotification(context, message.get());
                manager.notify(NotificationIds.PRE_REGISTRATION_SMS, notification);
                Intent smsRetrieverIntent = buildSmsRetrieverIntent(message.get());
                context.sendBroadcast(smsRetrieverIntent);
                return;
            } else {
                Log.w(TAG, "Received an SMS before registration is complete. We'll try again later.");
                throw new RetryLaterException();
            }
        } else {
            Log.w(TAG, "Received an SMS before registration is complete, but couldn't assemble the message anyway. Ignoring.");
            return;
        }
    }
    if (message.isPresent() && !isBlocked(message.get())) {
        Optional<InsertResult> insertResult = storeMessage(message.get());
        if (insertResult.isPresent()) {
            ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        }
    } else if (message.isPresent()) {
        Log.w(TAG, "*** Received blocked SMS, ignoring...");
    } else {
        Log.w(TAG, "*** Failed to assemble message fragments!");
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) NotificationManager(android.app.NotificationManager) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) Intent(android.content.Intent) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) Notification(android.app.Notification)

Example 3 with InsertResult

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

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

the class MessageContentProcessor method handleEndSessionMessage.

@Nullable
private MessageId handleEndSessionMessage(@NonNull SignalServiceContent content, @NonNull Optional<Long> smsMessageId, @NonNull Recipient senderRecipient) {
    log(content.getTimestamp(), "End session message.");
    MessageDatabase smsDatabase = SignalDatabase.sms();
    IncomingTextMessage incomingTextMessage = new IncomingTextMessage(senderRecipient.getId(), content.getSenderDevice(), content.getTimestamp(), content.getServerReceivedTimestamp(), System.currentTimeMillis(), "", Optional.absent(), 0, content.isNeedsReceipt(), content.getServerUuid());
    Optional<InsertResult> insertResult;
    if (!smsMessageId.isPresent()) {
        IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage);
        insertResult = smsDatabase.insertMessageInbox(incomingEndSessionMessage);
    } else {
        smsDatabase.markAsEndSession(smsMessageId.get());
        insertResult = Optional.of(new InsertResult(smsMessageId.get(), smsDatabase.getThreadIdForMessage(smsMessageId.get())));
    }
    if (insertResult.isPresent()) {
        ApplicationDependencies.getProtocolStore().aci().deleteAllSessions(content.getSender().getIdentifier());
        SecurityEvent.broadcastSecurityUpdateEvent(context);
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        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) IncomingEndSessionMessage(org.thoughtcrime.securesms.sms.IncomingEndSessionMessage) 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 5 with InsertResult

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

the class MessageContentProcessor method handleCorruptMessage.

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

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