Search in sources :

Example 1 with IncomingGroupUpdateMessage

use of org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage in project Signal-Android by signalapp.

the class GroupV1MessageProcessor method storeMessage.

@Nullable
private static Long storeMessage(@NonNull Context context, @NonNull SignalServiceContent content, @NonNull SignalServiceGroup group, @NonNull GroupContext storage, boolean outgoing) {
    if (group.getAvatar().isPresent()) {
        ApplicationDependencies.getJobManager().add(new AvatarGroupsV1DownloadJob(GroupId.v1orThrow(group.getGroupId())));
    }
    try {
        if (outgoing) {
            MessageDatabase mmsDatabase = SignalDatabase.mms();
            RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromGroupId(GroupId.v1orThrow(group.getGroupId()));
            Recipient recipient = Recipient.resolved(recipientId);
            OutgoingGroupUpdateMessage outgoingMessage = new OutgoingGroupUpdateMessage(recipient, storage, null, content.getTimestamp(), 0, false, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
            long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
            long messageId = mmsDatabase.insertMessageOutbox(outgoingMessage, threadId, false, null);
            mmsDatabase.markAsSent(messageId, true);
            return threadId;
        } else {
            MessageDatabase smsDatabase = SignalDatabase.sms();
            String body = Base64.encodeBytes(storage.toByteArray());
            IncomingTextMessage incoming = new IncomingTextMessage(Recipient.externalHighTrustPush(context, content.getSender()).getId(), content.getSenderDevice(), content.getTimestamp(), content.getServerReceivedTimestamp(), System.currentTimeMillis(), body, Optional.of(GroupId.v1orThrow(group.getGroupId())), 0, content.isNeedsReceipt(), content.getServerUuid());
            IncomingGroupUpdateMessage groupMessage = new IncomingGroupUpdateMessage(incoming, storage, body);
            Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(groupMessage);
            if (insertResult.isPresent()) {
                ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
                return insertResult.get().getThreadId();
            } else {
                return null;
            }
        }
    } catch (MmsException e) {
        Log.w(TAG, e);
    }
    return null;
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) AvatarGroupsV1DownloadJob(org.thoughtcrime.securesms.jobs.AvatarGroupsV1DownloadJob) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) IncomingGroupUpdateMessage(org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ByteString(com.google.protobuf.ByteString) OutgoingGroupUpdateMessage(org.thoughtcrime.securesms.mms.OutgoingGroupUpdateMessage) Nullable(androidx.annotation.Nullable)

Example 2 with IncomingGroupUpdateMessage

use of org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage in project Signal-Android by WhisperSystems.

the class SmsDatabase method insertMessageInbox.

@Override
public Optional<InsertResult> insertMessageInbox(IncomingTextMessage message, long type) {
    if (message.isJoined()) {
        type = (type & (Types.TOTAL_MASK - Types.BASE_TYPE_MASK)) | Types.JOINED_TYPE;
    } else if (message.isPreKeyBundle()) {
        type |= Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT;
    } else if (message.isSecureMessage()) {
        type |= Types.SECURE_MESSAGE_BIT;
    } else if (message.isGroup()) {
        IncomingGroupUpdateMessage incomingGroupUpdateMessage = (IncomingGroupUpdateMessage) message;
        type |= Types.SECURE_MESSAGE_BIT;
        if (incomingGroupUpdateMessage.isGroupV2()) {
            type |= Types.GROUP_V2_BIT | Types.GROUP_UPDATE_BIT;
            if (incomingGroupUpdateMessage.isJustAGroupLeave()) {
                type |= Types.GROUP_LEAVE_BIT;
            }
        } else if (incomingGroupUpdateMessage.isUpdate()) {
            type |= Types.GROUP_UPDATE_BIT;
        } else if (incomingGroupUpdateMessage.isQuit()) {
            type |= Types.GROUP_LEAVE_BIT;
        }
    } else if (message.isEndSession()) {
        type |= Types.SECURE_MESSAGE_BIT;
        type |= Types.END_SESSION_BIT;
    }
    if (message.isPush())
        type |= Types.PUSH_MESSAGE_BIT;
    if (message.isIdentityUpdate())
        type |= Types.KEY_EXCHANGE_IDENTITY_UPDATE_BIT;
    if (message.isContentPreKeyBundle())
        type |= Types.KEY_EXCHANGE_CONTENT_FORMAT;
    if (message.isIdentityVerified())
        type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
    else if (message.isIdentityDefault())
        type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;
    Recipient recipient = Recipient.resolved(message.getSender());
    Recipient groupRecipient;
    if (message.getGroupId() == null) {
        groupRecipient = null;
    } else {
        RecipientId id = SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(message.getGroupId());
        groupRecipient = Recipient.resolved(id);
    }
    boolean silent = message.isIdentityUpdate() || message.isIdentityVerified() || message.isIdentityDefault() || message.isJustAGroupLeave();
    boolean unread = !silent && (Util.isDefaultSmsProvider(context) || message.isSecureMessage() || message.isGroup() || message.isPreKeyBundle());
    long threadId;
    if (groupRecipient == null)
        threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
    else
        threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient);
    ContentValues values = new ContentValues();
    values.put(RECIPIENT_ID, message.getSender().serialize());
    values.put(ADDRESS_DEVICE_ID, message.getSenderDeviceId());
    values.put(DATE_RECEIVED, message.getReceivedTimestampMillis());
    values.put(DATE_SENT, message.getSentTimestampMillis());
    values.put(DATE_SERVER, message.getServerTimestampMillis());
    values.put(PROTOCOL, message.getProtocol());
    values.put(READ, unread ? 0 : 1);
    values.put(SUBSCRIPTION_ID, message.getSubscriptionId());
    values.put(EXPIRES_IN, message.getExpiresIn());
    values.put(UNIDENTIFIED, message.isUnidentified());
    if (!TextUtils.isEmpty(message.getPseudoSubject()))
        values.put(SUBJECT, message.getPseudoSubject());
    values.put(REPLY_PATH_PRESENT, message.isReplyPathPresent());
    values.put(SERVICE_CENTER, message.getServiceCenterAddress());
    values.put(BODY, message.getMessageBody());
    values.put(TYPE, type);
    values.put(THREAD_ID, threadId);
    values.put(SERVER_GUID, message.getServerGuid());
    if (message.isPush() && isDuplicate(message, threadId)) {
        Log.w(TAG, "Duplicate message (" + message.getSentTimestampMillis() + "), ignoring...");
        return Optional.absent();
    } else {
        SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
        long messageId = db.insert(TABLE_NAME, null, values);
        if (unread) {
            SignalDatabase.threads().incrementUnread(threadId, 1);
        }
        if (!silent) {
            SignalDatabase.threads().update(threadId, true);
        }
        if (message.getSubscriptionId() != -1) {
            SignalDatabase.recipients().setDefaultSubscriptionId(recipient.getId(), message.getSubscriptionId());
        }
        notifyConversationListeners(threadId);
        if (!silent) {
            TrimThreadJob.enqueueAsync(threadId);
        }
        return Optional.of(new InsertResult(messageId, threadId));
    }
}
Also used : ContentValues(android.content.ContentValues) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) IncomingGroupUpdateMessage(org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Example 3 with IncomingGroupUpdateMessage

use of org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage in project Signal-Android by WhisperSystems.

the class GroupV1MessageProcessor method storeMessage.

@Nullable
private static Long storeMessage(@NonNull Context context, @NonNull SignalServiceContent content, @NonNull SignalServiceGroup group, @NonNull GroupContext storage, boolean outgoing) {
    if (group.getAvatar().isPresent()) {
        ApplicationDependencies.getJobManager().add(new AvatarGroupsV1DownloadJob(GroupId.v1orThrow(group.getGroupId())));
    }
    try {
        if (outgoing) {
            MessageDatabase mmsDatabase = SignalDatabase.mms();
            RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromGroupId(GroupId.v1orThrow(group.getGroupId()));
            Recipient recipient = Recipient.resolved(recipientId);
            OutgoingGroupUpdateMessage outgoingMessage = new OutgoingGroupUpdateMessage(recipient, storage, null, content.getTimestamp(), 0, false, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
            long threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
            long messageId = mmsDatabase.insertMessageOutbox(outgoingMessage, threadId, false, null);
            mmsDatabase.markAsSent(messageId, true);
            return threadId;
        } else {
            MessageDatabase smsDatabase = SignalDatabase.sms();
            String body = Base64.encodeBytes(storage.toByteArray());
            IncomingTextMessage incoming = new IncomingTextMessage(Recipient.externalHighTrustPush(context, content.getSender()).getId(), content.getSenderDevice(), content.getTimestamp(), content.getServerReceivedTimestamp(), System.currentTimeMillis(), body, Optional.of(GroupId.v1orThrow(group.getGroupId())), 0, content.isNeedsReceipt(), content.getServerUuid());
            IncomingGroupUpdateMessage groupMessage = new IncomingGroupUpdateMessage(incoming, storage, body);
            Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(groupMessage);
            if (insertResult.isPresent()) {
                ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
                return insertResult.get().getThreadId();
            } else {
                return null;
            }
        }
    } catch (MmsException e) {
        Log.w(TAG, e);
    }
    return null;
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) AvatarGroupsV1DownloadJob(org.thoughtcrime.securesms.jobs.AvatarGroupsV1DownloadJob) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) IncomingGroupUpdateMessage(org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ByteString(com.google.protobuf.ByteString) OutgoingGroupUpdateMessage(org.thoughtcrime.securesms.mms.OutgoingGroupUpdateMessage) Nullable(androidx.annotation.Nullable)

Example 4 with IncomingGroupUpdateMessage

use of org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage in project Signal-Android by signalapp.

the class SmsDatabase method insertMessageInbox.

@Override
public Optional<InsertResult> insertMessageInbox(IncomingTextMessage message, long type) {
    if (message.isJoined()) {
        type = (type & (Types.TOTAL_MASK - Types.BASE_TYPE_MASK)) | Types.JOINED_TYPE;
    } else if (message.isPreKeyBundle()) {
        type |= Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT;
    } else if (message.isSecureMessage()) {
        type |= Types.SECURE_MESSAGE_BIT;
    } else if (message.isGroup()) {
        IncomingGroupUpdateMessage incomingGroupUpdateMessage = (IncomingGroupUpdateMessage) message;
        type |= Types.SECURE_MESSAGE_BIT;
        if (incomingGroupUpdateMessage.isGroupV2()) {
            type |= Types.GROUP_V2_BIT | Types.GROUP_UPDATE_BIT;
            if (incomingGroupUpdateMessage.isJustAGroupLeave()) {
                type |= Types.GROUP_LEAVE_BIT;
            }
        } else if (incomingGroupUpdateMessage.isUpdate()) {
            type |= Types.GROUP_UPDATE_BIT;
        } else if (incomingGroupUpdateMessage.isQuit()) {
            type |= Types.GROUP_LEAVE_BIT;
        }
    } else if (message.isEndSession()) {
        type |= Types.SECURE_MESSAGE_BIT;
        type |= Types.END_SESSION_BIT;
    }
    if (message.isPush())
        type |= Types.PUSH_MESSAGE_BIT;
    if (message.isIdentityUpdate())
        type |= Types.KEY_EXCHANGE_IDENTITY_UPDATE_BIT;
    if (message.isContentPreKeyBundle())
        type |= Types.KEY_EXCHANGE_CONTENT_FORMAT;
    if (message.isIdentityVerified())
        type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
    else if (message.isIdentityDefault())
        type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;
    Recipient recipient = Recipient.resolved(message.getSender());
    Recipient groupRecipient;
    if (message.getGroupId() == null) {
        groupRecipient = null;
    } else {
        RecipientId id = SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(message.getGroupId());
        groupRecipient = Recipient.resolved(id);
    }
    boolean silent = message.isIdentityUpdate() || message.isIdentityVerified() || message.isIdentityDefault() || message.isJustAGroupLeave();
    boolean unread = !silent && (Util.isDefaultSmsProvider(context) || message.isSecureMessage() || message.isGroup() || message.isPreKeyBundle());
    long threadId;
    if (groupRecipient == null)
        threadId = SignalDatabase.threads().getOrCreateThreadIdFor(recipient);
    else
        threadId = SignalDatabase.threads().getOrCreateThreadIdFor(groupRecipient);
    ContentValues values = new ContentValues();
    values.put(RECIPIENT_ID, message.getSender().serialize());
    values.put(ADDRESS_DEVICE_ID, message.getSenderDeviceId());
    values.put(DATE_RECEIVED, message.getReceivedTimestampMillis());
    values.put(DATE_SENT, message.getSentTimestampMillis());
    values.put(DATE_SERVER, message.getServerTimestampMillis());
    values.put(PROTOCOL, message.getProtocol());
    values.put(READ, unread ? 0 : 1);
    values.put(SUBSCRIPTION_ID, message.getSubscriptionId());
    values.put(EXPIRES_IN, message.getExpiresIn());
    values.put(UNIDENTIFIED, message.isUnidentified());
    if (!TextUtils.isEmpty(message.getPseudoSubject()))
        values.put(SUBJECT, message.getPseudoSubject());
    values.put(REPLY_PATH_PRESENT, message.isReplyPathPresent());
    values.put(SERVICE_CENTER, message.getServiceCenterAddress());
    values.put(BODY, message.getMessageBody());
    values.put(TYPE, type);
    values.put(THREAD_ID, threadId);
    values.put(SERVER_GUID, message.getServerGuid());
    if (message.isPush() && isDuplicate(message, threadId)) {
        Log.w(TAG, "Duplicate message (" + message.getSentTimestampMillis() + "), ignoring...");
        return Optional.absent();
    } else {
        SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
        long messageId = db.insert(TABLE_NAME, null, values);
        if (unread) {
            SignalDatabase.threads().incrementUnread(threadId, 1);
        }
        if (!silent) {
            SignalDatabase.threads().update(threadId, true);
        }
        if (message.getSubscriptionId() != -1) {
            SignalDatabase.recipients().setDefaultSubscriptionId(recipient.getId(), message.getSubscriptionId());
        }
        notifyConversationListeners(threadId);
        if (!silent) {
            TrimThreadJob.enqueueAsync(threadId);
        }
        return Optional.of(new InsertResult(messageId, threadId));
    }
}
Also used : ContentValues(android.content.ContentValues) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) IncomingGroupUpdateMessage(org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Aggregations

Recipient (org.thoughtcrime.securesms.recipients.Recipient)4 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)4 IncomingGroupUpdateMessage (org.thoughtcrime.securesms.sms.IncomingGroupUpdateMessage)4 ContentValues (android.content.ContentValues)2 Nullable (androidx.annotation.Nullable)2 ByteString (com.google.protobuf.ByteString)2 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)2 InsertResult (org.thoughtcrime.securesms.database.MessageDatabase.InsertResult)2 AvatarGroupsV1DownloadJob (org.thoughtcrime.securesms.jobs.AvatarGroupsV1DownloadJob)2 MmsException (org.thoughtcrime.securesms.mms.MmsException)2 OutgoingGroupUpdateMessage (org.thoughtcrime.securesms.mms.OutgoingGroupUpdateMessage)2 IncomingTextMessage (org.thoughtcrime.securesms.sms.IncomingTextMessage)2