Search in sources :

Example 1 with SignalContactRecord

use of org.whispersystems.signalservice.api.storage.SignalContactRecord in project Signal-Android by WhisperSystems.

the class StorageSyncHelperTest method ContactUpdate_equals_differentProfileKeys.

@Test
public void ContactUpdate_equals_differentProfileKeys() {
    byte[] profileKey = new byte[32];
    byte[] profileKeyCopy = profileKey.clone();
    profileKeyCopy[0] = 1;
    SignalContactRecord a = contactBuilder(1, ACI_A, E164_A, "a").setProfileKey(profileKey).build();
    SignalContactRecord b = contactBuilder(1, ACI_A, E164_A, "a").setProfileKey(profileKeyCopy).build();
    assertNotEquals(a, b);
    assertNotEquals(a.hashCode(), b.hashCode());
    assertTrue(StorageSyncHelper.profileKeyChanged(update(a, b)));
}
Also used : SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with SignalContactRecord

use of org.whispersystems.signalservice.api.storage.SignalContactRecord in project Signal-Android by WhisperSystems.

the class StorageSyncJob method performSync.

private boolean performSync() throws IOException, RetryLaterException, InvalidKeyException {
    final Stopwatch stopwatch = new Stopwatch("StorageSync");
    final SQLiteDatabase db = SignalDatabase.getRawDatabase();
    final SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    final UnknownStorageIdDatabase storageIdDatabase = SignalDatabase.unknownStorageIds();
    final StorageKey storageServiceKey = SignalStore.storageService().getOrCreateStorageKey();
    final SignalStorageManifest localManifest = SignalStore.storageService().getManifest();
    final SignalStorageManifest remoteManifest = accountManager.getStorageManifestIfDifferentVersion(storageServiceKey, localManifest.getVersion()).or(localManifest);
    stopwatch.split("remote-manifest");
    Recipient self = freshSelf();
    boolean needsMultiDeviceSync = false;
    boolean needsForcePush = false;
    if (self.getStorageServiceId() == null) {
        Log.w(TAG, "No storageId for self. Generating.");
        SignalDatabase.recipients().updateStorageId(self.getId(), StorageSyncHelper.generateKey());
        self = freshSelf();
    }
    Log.i(TAG, "Our version: " + localManifest.getVersion() + ", their version: " + remoteManifest.getVersion());
    if (remoteManifest.getVersion() > localManifest.getVersion()) {
        Log.i(TAG, "[Remote Sync] Newer manifest version found!");
        List<StorageId> localStorageIdsBeforeMerge = getAllLocalStorageIds(context, self);
        IdDifferenceResult idDifference = StorageSyncHelper.findIdDifference(remoteManifest.getStorageIds(), localStorageIdsBeforeMerge);
        if (idDifference.hasTypeMismatches() && SignalStore.account().isPrimaryDevice()) {
            Log.w(TAG, "[Remote Sync] Found type mismatches in the ID sets! Scheduling a force push after this sync completes.");
            needsForcePush = true;
        }
        Log.i(TAG, "[Remote Sync] Pre-Merge ID Difference :: " + idDifference);
        stopwatch.split("remote-id-diff");
        if (!idDifference.isEmpty()) {
            Log.i(TAG, "[Remote Sync] Retrieving records for key difference.");
            List<SignalStorageRecord> remoteOnly = accountManager.readStorageRecords(storageServiceKey, idDifference.getRemoteOnlyIds());
            stopwatch.split("remote-records");
            if (remoteOnly.size() != idDifference.getRemoteOnlyIds().size()) {
                Log.w(TAG, "[Remote Sync] Could not find all remote-only records! Requested: " + idDifference.getRemoteOnlyIds().size() + ", Found: " + remoteOnly.size() + ". These stragglers should naturally get deleted during the sync.");
            }
            List<SignalContactRecord> remoteContacts = new LinkedList<>();
            List<SignalGroupV1Record> remoteGv1 = new LinkedList<>();
            List<SignalGroupV2Record> remoteGv2 = new LinkedList<>();
            List<SignalAccountRecord> remoteAccount = new LinkedList<>();
            List<SignalStorageRecord> remoteUnknown = new LinkedList<>();
            for (SignalStorageRecord remote : remoteOnly) {
                if (remote.getContact().isPresent()) {
                    remoteContacts.add(remote.getContact().get());
                } else if (remote.getGroupV1().isPresent()) {
                    remoteGv1.add(remote.getGroupV1().get());
                } else if (remote.getGroupV2().isPresent()) {
                    remoteGv2.add(remote.getGroupV2().get());
                } else if (remote.getAccount().isPresent()) {
                    remoteAccount.add(remote.getAccount().get());
                } else if (remote.getId().isUnknown()) {
                    remoteUnknown.add(remote);
                } else {
                    Log.w(TAG, "Bad record! Type is a known value (" + remote.getId().getType() + "), but doesn't have a matching inner record. Dropping it.");
                }
            }
            db.beginTransaction();
            try {
                self = freshSelf();
                Log.i(TAG, "[Remote Sync] Remote-Only :: Contacts: " + remoteContacts.size() + ", GV1: " + remoteGv1.size() + ", GV2: " + remoteGv2.size() + ", Account: " + remoteAccount.size());
                new ContactRecordProcessor(context, self).process(remoteContacts, StorageSyncHelper.KEY_GENERATOR);
                new GroupV1RecordProcessor(context).process(remoteGv1, StorageSyncHelper.KEY_GENERATOR);
                new GroupV2RecordProcessor(context).process(remoteGv2, StorageSyncHelper.KEY_GENERATOR);
                self = freshSelf();
                new AccountRecordProcessor(context, self).process(remoteAccount, StorageSyncHelper.KEY_GENERATOR);
                List<SignalStorageRecord> unknownInserts = remoteUnknown;
                List<StorageId> unknownDeletes = Stream.of(idDifference.getLocalOnlyIds()).filter(StorageId::isUnknown).toList();
                Log.i(TAG, "[Remote Sync] Unknowns :: " + unknownInserts.size() + " inserts, " + unknownDeletes.size() + " deletes");
                storageIdDatabase.insert(unknownInserts);
                storageIdDatabase.delete(unknownDeletes);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
                ApplicationDependencies.getDatabaseObserver().notifyConversationListListeners();
                stopwatch.split("remote-merge-transaction");
            }
        } else {
            Log.i(TAG, "[Remote Sync] Remote version was newer, but there were no remote-only IDs.");
        }
    } else if (remoteManifest.getVersion() < localManifest.getVersion()) {
        Log.w(TAG, "[Remote Sync] Remote version was older. User might have switched accounts.");
    }
    if (remoteManifest != localManifest) {
        Log.i(TAG, "[Remote Sync] Saved new manifest. Now at version: " + remoteManifest.getVersion());
        SignalStore.storageService().setManifest(remoteManifest);
    }
    Log.i(TAG, "We are up-to-date with the remote storage state.");
    final WriteOperationResult remoteWriteOperation;
    db.beginTransaction();
    try {
        self = freshSelf();
        List<StorageId> localStorageIds = getAllLocalStorageIds(context, self);
        IdDifferenceResult idDifference = StorageSyncHelper.findIdDifference(remoteManifest.getStorageIds(), localStorageIds);
        List<SignalStorageRecord> remoteInserts = buildLocalStorageRecords(context, self, idDifference.getLocalOnlyIds());
        List<byte[]> remoteDeletes = Stream.of(idDifference.getRemoteOnlyIds()).map(StorageId::getRaw).toList();
        Log.i(TAG, "ID Difference :: " + idDifference);
        remoteWriteOperation = new WriteOperationResult(new SignalStorageManifest(remoteManifest.getVersion() + 1, localStorageIds), remoteInserts, remoteDeletes);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        stopwatch.split("local-data-transaction");
    }
    if (!remoteWriteOperation.isEmpty()) {
        Log.i(TAG, "We have something to write remotely.");
        Log.i(TAG, "WriteOperationResult :: " + remoteWriteOperation);
        StorageSyncValidations.validate(remoteWriteOperation, remoteManifest, needsForcePush, self);
        Optional<SignalStorageManifest> conflict = accountManager.writeStorageRecords(storageServiceKey, remoteWriteOperation.getManifest(), remoteWriteOperation.getInserts(), remoteWriteOperation.getDeletes());
        if (conflict.isPresent()) {
            Log.w(TAG, "Hit a conflict when trying to resolve the conflict! Retrying.");
            throw new RetryLaterException();
        }
        Log.i(TAG, "Saved new manifest. Now at version: " + remoteWriteOperation.getManifest().getVersion());
        SignalStore.storageService().setManifest(remoteWriteOperation.getManifest());
        stopwatch.split("remote-write");
        needsMultiDeviceSync = true;
    } else {
        Log.i(TAG, "No remote writes needed. Still at version: " + remoteManifest.getVersion());
    }
    if (needsForcePush && SignalStore.account().isPrimaryDevice()) {
        Log.w(TAG, "Scheduling a force push.");
        ApplicationDependencies.getJobManager().add(new StorageForcePushJob());
    }
    stopwatch.stop(TAG);
    return needsMultiDeviceSync;
}
Also used : SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) StorageId(org.whispersystems.signalservice.api.storage.StorageId) GroupV2RecordProcessor(org.thoughtcrime.securesms.storage.GroupV2RecordProcessor) SignalGroupV2Record(org.whispersystems.signalservice.api.storage.SignalGroupV2Record) UnknownStorageIdDatabase(org.thoughtcrime.securesms.database.UnknownStorageIdDatabase) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey) SignalAccountRecord(org.whispersystems.signalservice.api.storage.SignalAccountRecord) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) IdDifferenceResult(org.thoughtcrime.securesms.storage.StorageSyncHelper.IdDifferenceResult) GroupV1RecordProcessor(org.thoughtcrime.securesms.storage.GroupV1RecordProcessor) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) WriteOperationResult(org.thoughtcrime.securesms.storage.StorageSyncHelper.WriteOperationResult) LinkedList(java.util.LinkedList) SignalGroupV1Record(org.whispersystems.signalservice.api.storage.SignalGroupV1Record) AccountRecordProcessor(org.thoughtcrime.securesms.storage.AccountRecordProcessor) SQLiteDatabase(net.zetetic.database.sqlcipher.SQLiteDatabase) ContactRecordProcessor(org.thoughtcrime.securesms.storage.ContactRecordProcessor) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException)

Example 3 with SignalContactRecord

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

the class ContactRecordProcessor method getMatching.

@Override
@NonNull
Optional<SignalContactRecord> getMatching(@NonNull SignalContactRecord remote, @NonNull StorageKeyGenerator keyGenerator) {
    SignalServiceAddress address = remote.getAddress();
    Optional<RecipientId> byUuid = recipientDatabase.getByServiceId(address.getServiceId());
    Optional<RecipientId> byE164 = address.getNumber().isPresent() ? recipientDatabase.getByE164(address.getNumber().get()) : Optional.absent();
    return byUuid.or(byE164).transform(recipientDatabase::getRecordForSync).transform(settings -> {
        if (settings.getStorageId() != null) {
            return StorageSyncModels.localToRemoteRecord(settings);
        } else {
            Log.w(TAG, "Newly discovering a registered user via storage service. Saving a storageId for them.");
            recipientDatabase.updateStorageId(settings.getId(), keyGenerator.generate());
            RecipientRecord updatedSettings = Objects.requireNonNull(recipientDatabase.getRecordForSync(settings.getId()));
            return StorageSyncModels.localToRemoteRecord(updatedSettings);
        }
    }).transform(r -> r.getContact().get());
}
Also used : Context(android.content.Context) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) Arrays(java.util.Arrays) ACI(org.whispersystems.signalservice.api.push.ACI) NonNull(androidx.annotation.NonNull) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IdentityState(org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState) Optional(org.whispersystems.libsignal.util.guava.Optional) Objects(java.util.Objects) Log(org.signal.core.util.logging.Log) Nullable(androidx.annotation.Nullable) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) ServiceId(org.whispersystems.signalservice.api.push.ServiceId) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) NonNull(androidx.annotation.NonNull)

Example 4 with SignalContactRecord

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

the class ContactRecordProcessor method merge.

@NonNull
SignalContactRecord merge(@NonNull SignalContactRecord remote, @NonNull SignalContactRecord local, @NonNull StorageKeyGenerator keyGenerator) {
    String givenName;
    String familyName;
    if (remote.getGivenName().isPresent() || remote.getFamilyName().isPresent()) {
        givenName = remote.getGivenName().or("");
        familyName = remote.getFamilyName().or("");
    } else {
        givenName = local.getGivenName().or("");
        familyName = local.getFamilyName().or("");
    }
    byte[] unknownFields = remote.serializeUnknownFields();
    ServiceId serviceId = local.getAddress().getServiceId() == ServiceId.UNKNOWN ? remote.getAddress().getServiceId() : local.getAddress().getServiceId();
    String e164 = remote.getAddress().getNumber().or(local.getAddress().getNumber()).orNull();
    SignalServiceAddress address = new SignalServiceAddress(serviceId, e164);
    byte[] profileKey = remote.getProfileKey().or(local.getProfileKey()).orNull();
    String username = remote.getUsername().or(local.getUsername()).or("");
    IdentityState identityState = remote.getIdentityState();
    byte[] identityKey = remote.getIdentityKey().or(local.getIdentityKey()).orNull();
    boolean blocked = remote.isBlocked();
    boolean profileSharing = remote.isProfileSharingEnabled();
    boolean archived = remote.isArchived();
    boolean forcedUnread = remote.isForcedUnread();
    long muteUntil = remote.getMuteUntil();
    boolean matchesRemote = doParamsMatch(remote, unknownFields, address, givenName, familyName, profileKey, username, identityState, identityKey, blocked, profileSharing, archived, forcedUnread, muteUntil);
    boolean matchesLocal = doParamsMatch(local, unknownFields, address, givenName, familyName, profileKey, username, identityState, identityKey, blocked, profileSharing, archived, forcedUnread, muteUntil);
    if (matchesRemote) {
        return remote;
    } else if (matchesLocal) {
        return local;
    } else {
        return new SignalContactRecord.Builder(keyGenerator.generate(), address, unknownFields).setGivenName(givenName).setFamilyName(familyName).setProfileKey(profileKey).setUsername(username).setIdentityState(identityState).setIdentityKey(identityKey).setBlocked(blocked).setProfileSharingEnabled(profileSharing).setArchived(archived).setForcedUnread(forcedUnread).setMuteUntil(muteUntil).build();
    }
}
Also used : SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IdentityState(org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState) ServiceId(org.whispersystems.signalservice.api.push.ServiceId) NonNull(androidx.annotation.NonNull)

Example 5 with SignalContactRecord

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

the class StorageSyncHelperTest method ContactUpdate_equals_differentProfileKeys.

@Test
public void ContactUpdate_equals_differentProfileKeys() {
    byte[] profileKey = new byte[32];
    byte[] profileKeyCopy = profileKey.clone();
    profileKeyCopy[0] = 1;
    SignalContactRecord a = contactBuilder(1, ACI_A, E164_A, "a").setProfileKey(profileKey).build();
    SignalContactRecord b = contactBuilder(1, ACI_A, E164_A, "a").setProfileKey(profileKeyCopy).build();
    assertNotEquals(a, b);
    assertNotEquals(a.hashCode(), b.hashCode());
    assertTrue(StorageSyncHelper.profileKeyChanged(update(a, b)));
}
Also used : SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SignalContactRecord (org.whispersystems.signalservice.api.storage.SignalContactRecord)10 NonNull (androidx.annotation.NonNull)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)4 ServiceId (org.whispersystems.signalservice.api.push.ServiceId)4 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)4 IdentityState (org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState)4 Context (android.content.Context)2 Nullable (androidx.annotation.Nullable)2 Arrays (java.util.Arrays)2 LinkedList (java.util.LinkedList)2 Objects (java.util.Objects)2 SQLiteDatabase (net.zetetic.database.sqlcipher.SQLiteDatabase)2 Log (org.signal.core.util.logging.Log)2 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)2 SignalDatabase (org.thoughtcrime.securesms.database.SignalDatabase)2 UnknownStorageIdDatabase (org.thoughtcrime.securesms.database.UnknownStorageIdDatabase)2 RecipientRecord (org.thoughtcrime.securesms.database.model.RecipientRecord)2 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)2