]>
nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java
1 package org
.asamk
.signal
.manager
.api
;
3 import org
.asamk
.signal
.manager
.groups
.GroupId
;
4 import org
.slf4j
.Logger
;
5 import org
.slf4j
.LoggerFactory
;
6 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
7 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
11 public sealed interface RecipientIdentifier
{
13 String
getIdentifier();
15 record NoteToSelf() implements RecipientIdentifier
{
17 public static final NoteToSelf INSTANCE
= new NoteToSelf();
20 public String
getIdentifier() {
21 return "Note-To-Self";
25 sealed interface Single
extends RecipientIdentifier
{
27 static Single
fromString(String identifier
, String localNumber
) throws InvalidNumberException
{
29 if (UuidUtil
.isUuid(identifier
)) {
30 return new Uuid(UUID
.fromString(identifier
));
33 if (identifier
.startsWith("u:")) {
34 return new Username(identifier
.substring(2));
37 final var normalizedNumber
= PhoneNumberFormatter
.formatNumber(identifier
, localNumber
);
38 if (!normalizedNumber
.equals(identifier
)) {
39 final Logger logger
= LoggerFactory
.getLogger(RecipientIdentifier
.class);
40 logger
.debug("Normalized number {} to {}.", identifier
, normalizedNumber
);
42 return new Number(normalizedNumber
);
43 } catch (org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException e
) {
44 throw new InvalidNumberException(e
.getMessage(), e
);
48 static Single
fromAddress(RecipientAddress address
) {
49 if (address
.number().isPresent()) {
50 return new Number(address
.number().get());
51 } else if (address
.uuid().isPresent()) {
52 return new Uuid(address
.uuid().get());
53 } else if (address
.username().isPresent()) {
54 return new Username(address
.username().get());
56 throw new AssertionError("RecipientAddress without identifier");
59 RecipientAddress
toPartialRecipientAddress();
62 record Uuid(UUID uuid
) implements Single
{
65 public String
getIdentifier() {
66 return uuid
.toString();
70 public RecipientAddress
toPartialRecipientAddress() {
71 return new RecipientAddress(uuid
);
75 record Number(String number
) implements Single
{
78 public String
getIdentifier() {
83 public RecipientAddress
toPartialRecipientAddress() {
84 return new RecipientAddress(null, number
);
88 record Username(String username
) implements Single
{
91 public String
getIdentifier() {
92 return "u:" + username
;
96 public RecipientAddress
toPartialRecipientAddress() {
97 return new RecipientAddress(null, null, username
);
101 record Group(GroupId groupId
) implements RecipientIdentifier
{
104 public String
getIdentifier() {
105 return groupId
.toBase64();