From 94269744ad6ffa34e088a3e5dc21399a8782b60d Mon Sep 17 00:00:00 2001 From: AsamK Date: Tue, 14 Jan 2025 20:28:44 +0100 Subject: [PATCH] Improve final address when merging recipients --- .../recipients/MergeRecipientHelper.java | 6 ++--- .../storage/recipients/RecipientAddress.java | 10 ++++----- .../recipients/MergeRecipientHelperTest.java | 22 ++++++++++--------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelper.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelper.java index 95c171026..0a96785bc 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelper.java @@ -39,7 +39,7 @@ static Pair> resolveRecipientTrustedLocked( ) ) || recipient.address().aci().equals(address.aci())) { logger.debug("Got existing recipient {}, updating with high trust address", recipient.id()); - store.updateRecipientAddress(recipient.id(), recipient.address().withIdentifiersFrom(address)); + store.updateRecipientAddress(recipient.id(), address.withOtherIdentifiersFrom(recipient.address())); return new Pair<>(recipient.id(), List.of()); } @@ -93,14 +93,14 @@ static Pair> resolveRecipientTrustedLocked( if (finalAddress == null) { finalAddress = recipient.address(); } else { - finalAddress = finalAddress.withIdentifiersFrom(recipient.address()); + finalAddress = finalAddress.withOtherIdentifiersFrom(recipient.address()); } store.removeRecipientAddress(recipient.id()); } if (finalAddress == null) { finalAddress = address; } else { - finalAddress = finalAddress.withIdentifiersFrom(address); + finalAddress = address.withOtherIdentifiersFrom(finalAddress); } for (final var recipient : recipientsToBeStripped) { diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java index ba2b9f266..25dc3a3f9 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java @@ -79,11 +79,11 @@ public RecipientAddress(ServiceId serviceId) { this(Optional.of(serviceId), Optional.empty()); } - public RecipientAddress withIdentifiersFrom(RecipientAddress address) { - return new RecipientAddress(address.aci.or(this::aci), - address.pni.or(this::pni), - address.number.or(this::number), - address.username.or(this::username)); + public RecipientAddress withOtherIdentifiersFrom(RecipientAddress address) { + return new RecipientAddress(this.aci.or(address::aci), + this.pni.or(address::pni), + this.number.or(address::number), + this.username.or(address::username)); } public RecipientAddress removeIdentifiersFrom(RecipientAddress address) { diff --git a/lib/src/test/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelperTest.java b/lib/src/test/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelperTest.java index 0931b008f..f2f427c1a 100644 --- a/lib/src/test/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelperTest.java +++ b/lib/src/test/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelperTest.java @@ -111,18 +111,20 @@ class MergeRecipientHelperTest { new T(Set.of(rec(1, ADDR_A.ACI), rec(2, ADDR_A.PNI), rec(3, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))), - new T(Set.of(rec(1, ADDR_A.ACI.withIdentifiersFrom(ADDR_B.PNI)), rec(2, ADDR_A.PNI), rec(3, ADDR_A.NUM)), - ADDR_A.FULL, - Set.of(rec(1, ADDR_A.FULL))), - new T(Set.of(rec(1, ADDR_A.ACI.withIdentifiersFrom(ADDR_B.NUM)), rec(2, ADDR_A.PNI), rec(3, ADDR_A.NUM)), - ADDR_A.FULL, - Set.of(rec(1, ADDR_A.FULL))), - new T(Set.of(rec(1, ADDR_A.ACI), rec(2, ADDR_A.PNI), rec(3, ADDR_A.NUM.withIdentifiersFrom(ADDR_B.ACI))), + new T(Set.of(rec(1, ADDR_B.PNI.withOtherIdentifiersFrom(ADDR_A.ACI)), + rec(2, ADDR_A.PNI), + rec(3, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))), + new T(Set.of(rec(1, ADDR_B.NUM.withOtherIdentifiersFrom(ADDR_A.ACI)), + rec(2, ADDR_A.PNI), + rec(3, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))), + new T(Set.of(rec(1, ADDR_A.ACI), + rec(2, ADDR_A.PNI), + rec(3, ADDR_B.ACI.withOtherIdentifiersFrom(ADDR_A.NUM))), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL), rec(3, ADDR_B.ACI))), - new T(Set.of(rec(1, ADDR_A.ACI), rec(2, ADDR_A.PNI.withIdentifiersFrom(ADDR_B.ACI)), rec(3, ADDR_A.NUM)), - ADDR_A.FULL, - Set.of(rec(1, ADDR_A.FULL), rec(2, ADDR_B.ACI))), + new T(Set.of(rec(1, ADDR_A.ACI), + rec(2, ADDR_B.ACI.withOtherIdentifiersFrom(ADDR_A.PNI)), + rec(3, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL), rec(2, ADDR_B.ACI))), }; @ParameterizedTest