22
33import org .asamk .signal .manager .api .GroupIdV1 ;
44import org .asamk .signal .manager .api .GroupIdV2 ;
5+ import org .asamk .signal .manager .api .Pair ;
56import org .asamk .signal .manager .api .Profile ;
67import org .asamk .signal .manager .internal .SignalDependencies ;
78import org .asamk .signal .manager .storage .SignalAccount ;
3839import java .util .Base64 ;
3940import java .util .Collection ;
4041import java .util .Collections ;
42+ import java .util .HashSet ;
4143import java .util .List ;
4244import java .util .Map ;
4345import java .util .stream .Collectors ;
@@ -211,20 +213,23 @@ private boolean readDataFromStorage(
211213 remoteOnlyRecords .size ());
212214 }
213215
214- // This logic is wrong, records should only be deleted if they're deleted remotely, not if the remote record is updated
215- // if (!idDifference.localOnlyIds().isEmpty()) {
216- // final var updated = account.getRecipientStore()
217- // .removeStorageIdsFromLocalOnlyUnregisteredRecipients(connection,
218- // idDifference.localOnlyIds());
219- //
220- // if (updated > 0) {
221- // logger.warn(
222- // "Found {} records that were deleted remotely but only marked unregistered locally. Removed those from local store.",
223- // updated);
224- // }
225- // }
226- //
227- final var unknownInserts = processKnownRecords (connection , remoteOnlyRecords );
216+ final var listListPair = processKnownRecords (connection , remoteOnlyRecords );
217+ final var unknownInserts = listListPair .first ();
218+ final var updatedStorageIds = listListPair .second ();
219+ final var oldUnregisteredLocalOnlyIds = new HashSet <>(idDifference .localOnlyIds ());
220+ updatedStorageIds .forEach (oldUnregisteredLocalOnlyIds ::remove );
221+ if (!idDifference .localOnlyIds ().isEmpty ()) {
222+ final var updated = account .getRecipientStore ()
223+ .removeStorageIdsFromLocalOnlyUnregisteredRecipients (connection ,
224+ oldUnregisteredLocalOnlyIds );
225+
226+ if (updated > 0 ) {
227+ logger .warn (
228+ "Found {} records that were deleted remotely but only marked unregistered locally. Removed those from local store." ,
229+ updated );
230+ }
231+ }
232+
228233 final var unknownDeletes = idDifference .localOnlyIds ()
229234 .stream ()
230235 .filter (id -> !KNOWN_TYPES .contains (id .getType ()))
@@ -480,13 +485,13 @@ private Map<RecipientId, StorageId> generateContactStorageIds(List<RecipientId>
480485 private Map <GroupIdV1 , StorageId > generateGroupV1StorageIds (List <GroupIdV1 > groupIds ) {
481486 return groupIds .stream ()
482487 .collect (Collectors .toMap (recipientId -> recipientId ,
483- recipientId -> StorageId .forGroupV1 (KeyUtils .createRawStorageId ())));
488+ _ -> StorageId .forGroupV1 (KeyUtils .createRawStorageId ())));
484489 }
485490
486491 private Map <GroupIdV2 , StorageId > generateGroupV2StorageIds (List <GroupIdV2 > groupIds ) {
487492 return groupIds .stream ()
488493 .collect (Collectors .toMap (recipientId -> recipientId ,
489- recipientId -> StorageId .forGroupV2 (KeyUtils .createRawStorageId ())));
494+ _ -> StorageId .forGroupV2 (KeyUtils .createRawStorageId ())));
490495 }
491496
492497 private void storeManifestLocally (
@@ -630,16 +635,17 @@ private static IdDifferenceResult findIdDifference(
630635 return new IdDifferenceResult (remoteOnlyKeys , localOnlyKeys , hasTypeMismatch );
631636 }
632637
633- private List <StorageId > processKnownRecords (
638+ private Pair < List <StorageId >, List < StorageId > > processKnownRecords (
634639 final Connection connection ,
635640 List <SignalStorageRecord > records
636641 ) throws SQLException {
637642 final var unknownRecords = new ArrayList <StorageId >();
643+ final var processedRecords = new ArrayList <StorageId >();
638644
639645 final var accountRecordProcessor = new AccountRecordProcessor (account , connection , context .getJobExecutor ());
640- final var contactRecordProcessor = new ContactRecordProcessor (account , connection , context .getJobExecutor ());
641646 final var groupV1RecordProcessor = new GroupV1RecordProcessor (account , connection );
642647 final var groupV2RecordProcessor = new GroupV2RecordProcessor (account , connection );
648+ final var contactRecordProcessor = new ContactRecordProcessor (account , connection , context .getJobExecutor ());
643649
644650 for (final var record : records ) {
645651 if (record .getProto ().account != null ) {
@@ -662,8 +668,12 @@ private List<StorageId> processKnownRecords(
662668 unknownRecords .add (record .getId ());
663669 }
664670 }
671+ processedRecords .addAll (accountRecordProcessor .getUpdatedStorageIds ());
672+ processedRecords .addAll (groupV1RecordProcessor .getUpdatedStorageIds ());
673+ processedRecords .addAll (groupV2RecordProcessor .getUpdatedStorageIds ());
674+ processedRecords .addAll (contactRecordProcessor .getUpdatedStorageIds ());
665675
666- return unknownRecords ;
676+ return new Pair <>( unknownRecords , processedRecords ) ;
667677 }
668678
669679 /**
0 commit comments