Search in sources :

Example 21 with StorageId

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

the class StorageSyncJob method buildLocalStorageRecords.

@NonNull
private static List<SignalStorageRecord> buildLocalStorageRecords(@NonNull Context context, @NonNull Recipient self, @NonNull Collection<StorageId> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }
    RecipientDatabase recipientDatabase = SignalDatabase.recipients();
    UnknownStorageIdDatabase storageIdDatabase = SignalDatabase.unknownStorageIds();
    List<SignalStorageRecord> records = new ArrayList<>(ids.size());
    for (StorageId id : ids) {
        switch(id.getType()) {
            case ManifestRecord.Identifier.Type.CONTACT_VALUE:
            case ManifestRecord.Identifier.Type.GROUPV1_VALUE:
            case ManifestRecord.Identifier.Type.GROUPV2_VALUE:
                RecipientRecord settings = recipientDatabase.getByStorageId(id.getRaw());
                if (settings != null) {
                    if (settings.getGroupType() == RecipientDatabase.GroupType.SIGNAL_V2 && settings.getSyncExtras().getGroupMasterKey() == null) {
                        throw new MissingGv2MasterKeyError();
                    } else {
                        records.add(StorageSyncModels.localToRemoteRecord(settings));
                    }
                } else {
                    throw new MissingRecipientModelError("Missing local recipient model! Type: " + id.getType());
                }
                break;
            case ManifestRecord.Identifier.Type.ACCOUNT_VALUE:
                if (!Arrays.equals(self.getStorageServiceId(), id.getRaw())) {
                    throw new AssertionError("Local storage ID doesn't match self!");
                }
                records.add(StorageSyncHelper.buildAccountRecord(context, self));
                break;
            default:
                SignalStorageRecord unknown = storageIdDatabase.getById(id.getRaw());
                if (unknown != null) {
                    records.add(unknown);
                } else {
                    throw new MissingUnknownModelError("Missing local unknown model! Type: " + id.getType());
                }
                break;
        }
    }
    return records;
}
Also used : RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) UnknownStorageIdDatabase(org.thoughtcrime.securesms.database.UnknownStorageIdDatabase) StorageId(org.whispersystems.signalservice.api.storage.StorageId) NonNull(androidx.annotation.NonNull)

Example 22 with StorageId

use of org.whispersystems.signalservice.api.storage.StorageId 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

StorageId (org.whispersystems.signalservice.api.storage.StorageId)22 SignalStorageRecord (org.whispersystems.signalservice.api.storage.SignalStorageRecord)16 SignalStorageManifest (org.whispersystems.signalservice.api.storage.SignalStorageManifest)12 ArrayList (java.util.ArrayList)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 NonNull (androidx.annotation.NonNull)8 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)8 SignalAccountRecord (org.whispersystems.signalservice.api.storage.SignalAccountRecord)8 Stream (com.annimon.stream.Stream)6 List (java.util.List)6 Log (org.signal.core.util.logging.Log)6 UnknownStorageIdDatabase (org.thoughtcrime.securesms.database.UnknownStorageIdDatabase)6 RecipientRecord (org.thoughtcrime.securesms.database.model.RecipientRecord)6 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)6 StorageKey (org.whispersystems.signalservice.api.storage.StorageKey)6 Collectors (com.annimon.stream.Collectors)4 ByteString (com.google.protobuf.ByteString)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4