Skip to content

Commit

Permalink
Add support for using PNI as recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
AsamK committed Nov 29, 2024
1 parent 68c9d84 commit 2c68b5a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class InvalidNumberException extends Exception {

InvalidNumberException(String message) {
super(message);
}

InvalidNumberException(String message, Throwable e) {
super(message, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ static Single fromString(String identifier, String localNumber) throws InvalidNu
return new Uuid(UUID.fromString(identifier));
}

if (identifier.startsWith("PNI:")) {
final var pni = identifier.substring(4);
if (!UuidUtil.isUuid(pni)) {
throw new InvalidNumberException("Invalid PNI");
}
return new Pni(UUID.fromString(pni));
}

if (identifier.startsWith("u:")) {
return new Username(identifier.substring(2));
}
Expand All @@ -50,7 +58,7 @@ static Single fromAddress(RecipientAddress address) {
} else if (address.aci().isPresent()) {
return new Uuid(UUID.fromString(address.aci().get()));
} else if (address.pni().isPresent()) {
return new Pni(address.pni().get());
return new Pni(UUID.fromString(address.pni().get().substring(4)));
} else if (address.username().isPresent()) {
return new Username(address.username().get());
}
Expand All @@ -73,16 +81,16 @@ public RecipientAddress toPartialRecipientAddress() {
}
}

record Pni(String pni) implements Single {
record Pni(UUID pni) implements Single {

@Override
public String getIdentifier() {
return pni;
return "PNI:" + pni.toString();
}

@Override
public RecipientAddress toPartialRecipientAddress() {
return new RecipientAddress(null, pni, null, null);
return new RecipientAddress(null, getIdentifier(), null, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public RecipientId resolveRecipient(final RecipientIdentifier.Single recipient)
if (recipient instanceof RecipientIdentifier.Uuid uuidRecipient) {
return account.getRecipientResolver().resolveRecipient(ACI.from(uuidRecipient.uuid()));
} else if (recipient instanceof RecipientIdentifier.Pni pniRecipient) {
return account.getRecipientResolver().resolveRecipient(PNI.parseOrThrow(pniRecipient.pni()));
return account.getRecipientResolver().resolveRecipient(PNI.from(pniRecipient.pni()));
} else if (recipient instanceof RecipientIdentifier.Number numberRecipient) {
final var number = numberRecipient.number();
return account.getRecipientStore().resolveRecipientByNumber(number, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ public SendMessageResults sendRemoteDeleteMessage(
.deleteEntryForRecipientNonGroup(targetSentTimestamp, ACI.from(u.uuid()));
} else if (recipient instanceof RecipientIdentifier.Pni pni) {
account.getMessageSendLogStore()
.deleteEntryForRecipientNonGroup(targetSentTimestamp, PNI.parseOrThrow(pni.pni()));
.deleteEntryForRecipientNonGroup(targetSentTimestamp, PNI.from(pni.pni()));
} else if (recipient instanceof RecipientIdentifier.Single r) {
try {
final var recipientId = context.getRecipientHelper().resolveRecipient(r);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public RecipientAddress(SignalServiceAddress address) {
}

public RecipientAddress(org.asamk.signal.manager.api.RecipientAddress address) {
this(address.aci().map(ACI::parseOrNull),
address.pni().map(PNI::parseOrNull),
this(address.aci().map(ACI::parseOrThrow),
address.pni().map(PNI::parseOrThrow),
address.number(),
address.username());
}
Expand Down

0 comments on commit 2c68b5a

Please sign in to comment.