Search in sources :

Example 1 with Base64

use of org.thoughtcrime.securesms.util.Base64 in project Signal-Android by WhisperSystems.

the class StorageSyncHelper method findIdDifference.

/**
 * Given a list of all the local and remote keys you know about, this will return a result telling
 * you which keys are exclusively remote and which are exclusively local.
 *
 * @param remoteIds All remote keys available.
 * @param localIds All local keys available.
 *
 * @return An object describing which keys are exclusive to the remote data set and which keys are
 *         exclusive to the local data set.
 */
@NonNull
public static IdDifferenceResult findIdDifference(@NonNull Collection<StorageId> remoteIds, @NonNull Collection<StorageId> localIds) {
    Map<String, StorageId> remoteByRawId = Stream.of(remoteIds).collect(Collectors.toMap(id -> Base64.encodeBytes(id.getRaw()), id -> id));
    Map<String, StorageId> localByRawId = Stream.of(localIds).collect(Collectors.toMap(id -> Base64.encodeBytes(id.getRaw()), id -> id));
    boolean hasTypeMismatch = remoteByRawId.size() != remoteIds.size() || localByRawId.size() != localIds.size();
    Set<String> remoteOnlyRawIds = SetUtil.difference(remoteByRawId.keySet(), localByRawId.keySet());
    Set<String> localOnlyRawIds = SetUtil.difference(localByRawId.keySet(), remoteByRawId.keySet());
    Set<String> sharedRawIds = SetUtil.intersection(localByRawId.keySet(), remoteByRawId.keySet());
    for (String rawId : sharedRawIds) {
        StorageId remote = Objects.requireNonNull(remoteByRawId.get(rawId));
        StorageId local = Objects.requireNonNull(localByRawId.get(rawId));
        if (remote.getType() != local.getType()) {
            remoteOnlyRawIds.remove(rawId);
            localOnlyRawIds.remove(rawId);
            hasTypeMismatch = true;
            Log.w(TAG, "Remote type " + remote.getType() + " did not match local type " + local.getType() + "!");
        }
    }
    List<StorageId> remoteOnlyKeys = Stream.of(remoteOnlyRawIds).map(remoteByRawId::get).toList();
    List<StorageId> localOnlyKeys = Stream.of(localOnlyRawIds).map(localByRawId::get).toList();
    return new IdDifferenceResult(remoteOnlyKeys, localOnlyKeys, hasTypeMismatch);
}
Also used : SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) Context(android.content.Context) SignalAccountRecord(org.whispersystems.signalservice.api.storage.SignalAccountRecord) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) Stream(com.annimon.stream.Stream) Util(org.thoughtcrime.securesms.util.Util) NonNull(androidx.annotation.NonNull) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) TextSecurePreferences(org.thoughtcrime.securesms.util.TextSecurePreferences) Locale(java.util.Locale) Map(java.util.Map) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) StorageId(org.whispersystems.signalservice.api.storage.StorageId) RetrieveProfileAvatarJob(org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob) Entropy(org.thoughtcrime.securesms.payments.Entropy) Collectors(com.annimon.stream.Collectors) Base64(org.thoughtcrime.securesms.util.Base64) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) Collection(java.util.Collection) PhoneNumberPrivacyValues(org.thoughtcrime.securesms.keyvalue.PhoneNumberPrivacyValues) Subscriber(org.thoughtcrime.securesms.subscription.Subscriber) Set(java.util.Set) SetUtil(org.thoughtcrime.securesms.util.SetUtil) OptionalUtil(org.whispersystems.signalservice.api.util.OptionalUtil) Optional(org.whispersystems.libsignal.util.guava.Optional) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Log(org.signal.core.util.logging.Log) List(java.util.List) Nullable(androidx.annotation.Nullable) StorageSyncJob(org.thoughtcrime.securesms.jobs.StorageSyncJob) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) VisibleForTesting(androidx.annotation.VisibleForTesting) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) StorageId(org.whispersystems.signalservice.api.storage.StorageId) NonNull(androidx.annotation.NonNull)

Aggregations

Context (android.content.Context)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 Collectors (com.annimon.stream.Collectors)1 Stream (com.annimon.stream.Stream)1 Collection (java.util.Collection)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 Log (org.signal.core.util.logging.Log)1 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)1 SignalDatabase (org.thoughtcrime.securesms.database.SignalDatabase)1 RecipientRecord (org.thoughtcrime.securesms.database.model.RecipientRecord)1 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)1 RetrieveProfileAvatarJob (org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob)1 StorageSyncJob (org.thoughtcrime.securesms.jobs.StorageSyncJob)1