Search in sources :

Example 16 with SignalStorageRecord

use of org.whispersystems.signalservice.api.storage.SignalStorageRecord in project Signal-Android by signalapp.

the class StorageSyncValidations method validateManifestAndInserts.

private static void validateManifestAndInserts(@NonNull SignalStorageManifest manifest, @NonNull List<SignalStorageRecord> inserts, @NonNull Recipient self) {
    Set<StorageId> allSet = new HashSet<>(manifest.getStorageIds());
    Set<StorageId> insertSet = new HashSet<>(Stream.of(inserts).map(SignalStorageRecord::getId).toList());
    Set<ByteBuffer> rawIdSet = Stream.of(allSet).map(id -> ByteBuffer.wrap(id.getRaw())).collect(Collectors.toSet());
    if (allSet.size() != manifest.getStorageIds().size()) {
        throw new DuplicateStorageIdError();
    }
    if (rawIdSet.size() != allSet.size()) {
        throw new DuplicateRawIdError();
    }
    if (inserts.size() > insertSet.size()) {
        throw new DuplicateInsertInWriteError();
    }
    int accountCount = 0;
    for (StorageId id : manifest.getStorageIds()) {
        accountCount += id.getType() == ManifestRecord.Identifier.Type.ACCOUNT_VALUE ? 1 : 0;
    }
    if (accountCount > 1) {
        throw new MultipleAccountError();
    }
    if (accountCount == 0) {
        throw new MissingAccountError();
    }
    for (SignalStorageRecord insert : inserts) {
        if (!allSet.contains(insert.getId())) {
            throw new InsertNotPresentInFullIdSetError();
        }
        if (insert.isUnknown()) {
            throw new UnknownInsertError();
        }
        if (insert.getContact().isPresent()) {
            SignalServiceAddress address = insert.getContact().get().getAddress();
            if (self.requireE164().equals(address.getNumber().or("")) || self.requireServiceId().equals(address.getServiceId())) {
                throw new SelfAddedAsContactError();
            }
        }
    }
}
Also used : Collectors(com.annimon.stream.Collectors) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) Base64(org.thoughtcrime.securesms.util.Base64) ManifestRecord(org.whispersystems.signalservice.internal.storage.protos.ManifestRecord) Set(java.util.Set) SetUtil(org.thoughtcrime.securesms.util.SetUtil) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) Log(org.signal.core.util.logging.Log) List(java.util.List) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) StorageId(org.whispersystems.signalservice.api.storage.StorageId) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) StorageId(org.whispersystems.signalservice.api.storage.StorageId) ByteBuffer(java.nio.ByteBuffer) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) HashSet(java.util.HashSet)

Aggregations

SignalStorageRecord (org.whispersystems.signalservice.api.storage.SignalStorageRecord)16 StorageId (org.whispersystems.signalservice.api.storage.StorageId)14 SignalStorageManifest (org.whispersystems.signalservice.api.storage.SignalStorageManifest)10 ArrayList (java.util.ArrayList)8 NonNull (androidx.annotation.NonNull)6 UnknownStorageIdDatabase (org.thoughtcrime.securesms.database.UnknownStorageIdDatabase)6 Recipient (org.thoughtcrime.securesms.recipients.Recipient)6 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)6 StorageKey (org.whispersystems.signalservice.api.storage.StorageKey)6 Stream (com.annimon.stream.Stream)4 ByteString (com.google.protobuf.ByteString)4 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Log (org.signal.core.util.logging.Log)4 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)4 RetryLaterException (org.thoughtcrime.securesms.transport.RetryLaterException)3 SignalAccountRecord (org.whispersystems.signalservice.api.storage.SignalAccountRecord)3 ManifestRecord (org.whispersystems.signalservice.internal.storage.protos.ManifestRecord)3 ContentValues (android.content.ContentValues)2