Improve final address when merging recipients

This commit is contained in:
AsamK 2025-01-14 20:28:44 +01:00
parent 7a25ae5b9c
commit 94269744ad
3 changed files with 20 additions and 18 deletions

View File

@ -39,7 +39,7 @@ public class MergeRecipientHelper {
) )
) || recipient.address().aci().equals(address.aci())) { ) || recipient.address().aci().equals(address.aci())) {
logger.debug("Got existing recipient {}, updating with high trust address", recipient.id()); 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()); return new Pair<>(recipient.id(), List.of());
} }
@ -93,14 +93,14 @@ public class MergeRecipientHelper {
if (finalAddress == null) { if (finalAddress == null) {
finalAddress = recipient.address(); finalAddress = recipient.address();
} else { } else {
finalAddress = finalAddress.withIdentifiersFrom(recipient.address()); finalAddress = finalAddress.withOtherIdentifiersFrom(recipient.address());
} }
store.removeRecipientAddress(recipient.id()); store.removeRecipientAddress(recipient.id());
} }
if (finalAddress == null) { if (finalAddress == null) {
finalAddress = address; finalAddress = address;
} else { } else {
finalAddress = finalAddress.withIdentifiersFrom(address); finalAddress = address.withOtherIdentifiersFrom(finalAddress);
} }
for (final var recipient : recipientsToBeStripped) { for (final var recipient : recipientsToBeStripped) {

View File

@ -79,11 +79,11 @@ public record RecipientAddress(
this(Optional.of(serviceId), Optional.empty()); this(Optional.of(serviceId), Optional.empty());
} }
public RecipientAddress withIdentifiersFrom(RecipientAddress address) { public RecipientAddress withOtherIdentifiersFrom(RecipientAddress address) {
return new RecipientAddress(address.aci.or(this::aci), return new RecipientAddress(this.aci.or(address::aci),
address.pni.or(this::pni), this.pni.or(address::pni),
address.number.or(this::number), this.number.or(address::number),
address.username.or(this::username)); this.username.or(address::username));
} }
public RecipientAddress removeIdentifiersFrom(RecipientAddress address) { public RecipientAddress removeIdentifiersFrom(RecipientAddress address) {

View File

@ -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)), new T(Set.of(rec(1, ADDR_A.ACI), rec(2, ADDR_A.PNI), rec(3, ADDR_A.NUM)),
ADDR_A.FULL, ADDR_A.FULL,
Set.of(rec(1, 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)), new T(Set.of(rec(1, ADDR_B.PNI.withOtherIdentifiersFrom(ADDR_A.ACI)),
ADDR_A.FULL, rec(2, ADDR_A.PNI),
Set.of(rec(1, ADDR_A.FULL))), 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)), new T(Set.of(rec(1, ADDR_B.NUM.withOtherIdentifiersFrom(ADDR_A.ACI)),
ADDR_A.FULL, rec(2, ADDR_A.PNI),
Set.of(rec(1, ADDR_A.FULL))), 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_A.ACI),
rec(2, ADDR_A.PNI),
rec(3, ADDR_B.ACI.withOtherIdentifiersFrom(ADDR_A.NUM))),
ADDR_A.FULL, ADDR_A.FULL,
Set.of(rec(1, ADDR_A.FULL), rec(3, ADDR_B.ACI))), 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)), new T(Set.of(rec(1, ADDR_A.ACI),
ADDR_A.FULL, rec(2, ADDR_B.ACI.withOtherIdentifiersFrom(ADDR_A.PNI)),
Set.of(rec(1, ADDR_A.FULL), rec(2, ADDR_B.ACI))), rec(3, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL), rec(2, ADDR_B.ACI))),
}; };
@ParameterizedTest @ParameterizedTest