Convert StorageService protos to wire.

This commit is contained in:
Cody Henthorne
2023-08-28 22:47:02 -04:00
committed by Greyson Parrelli
parent 9a7d8c858d
commit 6a21106347
28 changed files with 620 additions and 882 deletions

View File

@@ -1069,7 +1069,7 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da
GroupV2Record.StorySendMode.DEFAULT -> ShowAsStoryState.IF_ACTIVE
GroupV2Record.StorySendMode.DISABLED -> ShowAsStoryState.NEVER
GroupV2Record.StorySendMode.ENABLED -> ShowAsStoryState.ALWAYS
GroupV2Record.StorySendMode.UNRECOGNIZED -> ShowAsStoryState.IF_ACTIVE
else -> ShowAsStoryState.IF_ACTIVE
}
}

View File

@@ -412,7 +412,7 @@ public class StorageSyncJob extends BaseJob {
new GroupV2RecordProcessor(context).process(records.gv2, StorageSyncHelper.KEY_GENERATOR);
new AccountRecordProcessor(context, freshSelf()).process(records.account, StorageSyncHelper.KEY_GENERATOR);
if (getKnownTypes().contains(ManifestRecord.Identifier.Type.STORY_DISTRIBUTION_LIST_VALUE)) {
if (getKnownTypes().contains(ManifestRecord.Identifier.Type.STORY_DISTRIBUTION_LIST.getValue())) {
new StoryDistributionListRecordProcessor().process(records.storyDistributionLists, StorageSyncHelper.KEY_GENERATOR);
}
}
@@ -434,10 +434,15 @@ public class StorageSyncJob extends BaseJob {
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:
ManifestRecord.Identifier.Type type = ManifestRecord.Identifier.Type.fromValue(id.getType());
if (type == null) {
type = ManifestRecord.Identifier.Type.UNKNOWN;
}
switch (type) {
case CONTACT:
case GROUPV1:
case GROUPV2:
RecipientRecord settings = recipientTable.getByStorageId(id.getRaw());
if (settings != null) {
if (settings.getRecipientType() == RecipientTable.RecipientType.GV2 && settings.getSyncExtras().getGroupMasterKey() == null) {
@@ -449,13 +454,13 @@ public class StorageSyncJob extends BaseJob {
throw new MissingRecipientModelError("Missing local recipient model! Type: " + id.getType());
}
break;
case ManifestRecord.Identifier.Type.ACCOUNT_VALUE:
case ACCOUNT:
if (!Arrays.equals(self.getStorageServiceId(), id.getRaw())) {
throw new AssertionError("Local storage ID doesn't match self!");
}
records.add(StorageSyncHelper.buildAccountRecord(context, self));
break;
case ManifestRecord.Identifier.Type.STORY_DISTRIBUTION_LIST_VALUE:
case STORY_DISTRIBUTION_LIST:
RecipientRecord record = recipientTable.getByStorageId(id.getRaw());
if (record != null) {
if (record.getDistributionListId() != null) {
@@ -488,8 +493,8 @@ public class StorageSyncJob extends BaseJob {
private static List<Integer> getKnownTypes() {
return Arrays.stream(ManifestRecord.Identifier.Type.values())
.filter(it -> !it.equals(ManifestRecord.Identifier.Type.UNKNOWN) && !it.equals(ManifestRecord.Identifier.Type.UNRECOGNIZED))
.map(it -> it.getNumber())
.filter(it -> !it.equals(ManifestRecord.Identifier.Type.UNKNOWN))
.map(ManifestRecord.Identifier.Type::getValue)
.collect(Collectors.toList());
}

View File

@@ -17,6 +17,8 @@ import org.whispersystems.signalservice.api.storage.SignalAccountRecord;
import org.whispersystems.signalservice.api.storage.StorageId;
import org.whispersystems.signalservice.internal.storage.protos.AccountRecord;
import java.io.IOException;
/**
* Check for unknown fields stored on self and attempt to apply them.
*/
@@ -69,12 +71,12 @@ public class ApplyUnknownFieldsToSelfMigrationJob extends MigrationJob {
try {
StorageId storageId = StorageId.forAccount(self.getStorageServiceId());
AccountRecord accountRecord = AccountRecord.parseFrom(settings.getSyncExtras().getStorageProto());
AccountRecord accountRecord = AccountRecord.ADAPTER.decode(settings.getSyncExtras().getStorageProto());
SignalAccountRecord signalAccountRecord = new SignalAccountRecord(storageId, accountRecord);
Log.d(TAG, "Applying potentially now known unknowns");
StorageSyncHelper.applyAccountStorageSyncUpdates(context, self, signalAccountRecord, false);
} catch (InvalidProtocolBufferException e) {
} catch (IOException e) {
Log.w(TAG, e);
}
}

View File

@@ -8,11 +8,9 @@ import androidx.annotation.VisibleForTesting;
import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.google.protobuf.ByteString;
import org.signal.core.util.SetUtil;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.components.settings.app.usernamelinks.UsernameQrCodeColorScheme;
import org.thoughtcrime.securesms.database.RecipientTable;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.database.model.RecipientRecord;
@@ -31,7 +29,6 @@ import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.api.push.UsernameLinkComponents;
import org.whispersystems.signalservice.api.storage.SignalAccountRecord;
import org.whispersystems.signalservice.api.storage.SignalContactRecord;
import org.whispersystems.signalservice.api.storage.SignalRecord;
import org.whispersystems.signalservice.api.storage.SignalStorageManifest;
import org.whispersystems.signalservice.api.storage.SignalStorageRecord;
import org.whispersystems.signalservice.api.storage.StorageId;
@@ -49,6 +46,8 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import okio.ByteString;
public final class StorageSyncHelper {
private static final String TAG = Log.tag(StorageSyncHelper.class);
@@ -167,11 +166,11 @@ public final class StorageSyncHelper {
UsernameLinkComponents linkComponents = SignalStore.account().getUsernameLink();
if (linkComponents != null) {
account.setUsernameLink(AccountRecord.UsernameLink.newBuilder()
.setEntropy(ByteString.copyFrom(linkComponents.getEntropy()))
.setServerId(UuidUtil.toByteString(linkComponents.getServerId()))
.setColor(StorageSyncModels.localToRemoteUsernameColor(SignalStore.misc().getUsernameQrCodeColorScheme()))
.build());
account.setUsernameLink(new AccountRecord.UsernameLink.Builder()
.entropy(ByteString.of(linkComponents.getEntropy()))
.serverId(UuidUtil.toOkioByteString(linkComponents.getServerId()))
.color(StorageSyncModels.localToRemoteUsernameColor(SignalStore.misc().getUsernameQrCodeColorScheme()))
.build());
} else {
account.setUsernameLink(null);
}
@@ -235,11 +234,11 @@ public final class StorageSyncHelper {
if (update.getNew().getUsernameLink() != null) {
SignalStore.account().setUsernameLink(
new UsernameLinkComponents(
update.getNew().getUsernameLink().getEntropy().toByteArray(),
UuidUtil.parseOrThrow(update.getNew().getUsernameLink().getServerId().toByteArray())
update.getNew().getUsernameLink().entropy.toByteArray(),
UuidUtil.parseOrThrow(update.getNew().getUsernameLink().serverId.toByteArray())
)
);
SignalStore.misc().setUsernameQrCodeColorScheme(StorageSyncModels.remoteToLocalUsernameColor(update.getNew().getUsernameLink().getColor()));
SignalStore.misc().setUsernameQrCodeColorScheme(StorageSyncModels.remoteToLocalUsernameColor(update.getNew().getUsernameLink().color));
}
}

View File

@@ -104,7 +104,7 @@ public final class StorageSyncValidations {
private static void validateManifestAndInserts(@NonNull SignalStorageManifest manifest, @NonNull List<SignalStorageRecord> inserts, @NonNull Recipient self) {
int accountCount = 0;
for (StorageId id : manifest.getStorageIds()) {
accountCount += id.getType() == ManifestRecord.Identifier.Type.ACCOUNT_VALUE ? 1 : 0;
accountCount += id.getType() == ManifestRecord.Identifier.Type.ACCOUNT.getValue() ? 1 : 0;
}
if (accountCount > 1) {
@@ -124,22 +124,22 @@ public final class StorageSyncValidations {
}
if (rawIdSet.size() != allSet.size()) {
List<StorageId> ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.CONTACT_VALUE);
List<StorageId> ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.CONTACT.getValue());
if (ids.size() != new HashSet<>(ids).size()) {
throw new DuplicateContactIdError();
}
ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.GROUPV1_VALUE);
ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.GROUPV1.getValue());
if (ids.size() != new HashSet<>(ids).size()) {
throw new DuplicateGroupV1IdError();
}
ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.GROUPV2_VALUE);
ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.GROUPV2.getValue());
if (ids.size() != new HashSet<>(ids).size()) {
throw new DuplicateGroupV2IdError();
}
ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.STORY_DISTRIBUTION_LIST_VALUE);
ids = manifest.getStorageIdsByType().get(ManifestRecord.Identifier.Type.STORY_DISTRIBUTION_LIST.getValue());
if (ids.size() != new HashSet<>(ids).size()) {
throw new DuplicateDistributionListIdError();
}