]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/Profile.java
Store profile phone number sharing mode and discoverable state
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / api / Profile.java
1 package org.asamk.signal.manager.api;
2
3 import org.whispersystems.signalservice.internal.util.Util;
4
5 import java.util.Collections;
6 import java.util.Objects;
7 import java.util.Set;
8
9 public class Profile {
10
11 private final long lastUpdateTimestamp;
12
13 private final String givenName;
14
15 private final String familyName;
16
17 private final String about;
18
19 private final String aboutEmoji;
20
21 private final String avatarUrlPath;
22
23 private final byte[] mobileCoinAddress;
24
25 private final UnidentifiedAccessMode unidentifiedAccessMode;
26
27 private final Set<Capability> capabilities;
28
29 private final PhoneNumberSharingMode phoneNumberSharingMode;
30
31 public Profile(
32 final long lastUpdateTimestamp,
33 final String givenName,
34 final String familyName,
35 final String about,
36 final String aboutEmoji,
37 final String avatarUrlPath,
38 final byte[] mobileCoinAddress,
39 final UnidentifiedAccessMode unidentifiedAccessMode,
40 final Set<Capability> capabilities,
41 final PhoneNumberSharingMode phoneNumberSharingMode
42 ) {
43 this.lastUpdateTimestamp = lastUpdateTimestamp;
44 this.givenName = givenName;
45 this.familyName = familyName;
46 this.about = about;
47 this.aboutEmoji = aboutEmoji;
48 this.avatarUrlPath = avatarUrlPath;
49 this.mobileCoinAddress = mobileCoinAddress;
50 this.unidentifiedAccessMode = unidentifiedAccessMode;
51 this.capabilities = capabilities;
52 this.phoneNumberSharingMode = phoneNumberSharingMode;
53 }
54
55 private Profile(final Builder builder) {
56 lastUpdateTimestamp = builder.lastUpdateTimestamp;
57 givenName = builder.givenName;
58 familyName = builder.familyName;
59 about = builder.about;
60 aboutEmoji = builder.aboutEmoji;
61 avatarUrlPath = builder.avatarUrlPath;
62 mobileCoinAddress = builder.mobileCoinAddress;
63 unidentifiedAccessMode = builder.unidentifiedAccessMode;
64 capabilities = builder.capabilities;
65 phoneNumberSharingMode = builder.phoneNumberSharingMode;
66 }
67
68 public static Builder newBuilder() {
69 return new Builder();
70 }
71
72 public static Builder newBuilder(final Profile copy) {
73 Builder builder = new Builder();
74 builder.lastUpdateTimestamp = copy.getLastUpdateTimestamp();
75 builder.givenName = copy.getGivenName();
76 builder.familyName = copy.getFamilyName();
77 builder.about = copy.getAbout();
78 builder.aboutEmoji = copy.getAboutEmoji();
79 builder.avatarUrlPath = copy.getAvatarUrlPath();
80 builder.mobileCoinAddress = copy.getMobileCoinAddress();
81 builder.unidentifiedAccessMode = copy.getUnidentifiedAccessMode();
82 builder.capabilities = copy.getCapabilities();
83 return builder;
84 }
85
86 public long getLastUpdateTimestamp() {
87 return lastUpdateTimestamp;
88 }
89
90 public String getGivenName() {
91 return givenName;
92 }
93
94 public String getFamilyName() {
95 return familyName;
96 }
97
98 public String getInternalServiceName() {
99 if (familyName == null) {
100 return givenName == null ? "" : givenName;
101 }
102 return String.join("\0", givenName == null ? "" : givenName, familyName);
103 }
104
105 public String getDisplayName() {
106 final var noGivenName = Util.isEmpty(givenName);
107 final var noFamilyName = Util.isEmpty(familyName);
108
109 if (noGivenName && noFamilyName) {
110 return "";
111 } else if (noGivenName) {
112 return familyName;
113 } else if (noFamilyName) {
114 return givenName;
115 }
116
117 return givenName + " " + familyName;
118 }
119
120 public String getAbout() {
121 return about;
122 }
123
124 public String getAboutEmoji() {
125 return aboutEmoji;
126 }
127
128 public String getAvatarUrlPath() {
129 return avatarUrlPath;
130 }
131
132 public byte[] getMobileCoinAddress() {
133 return mobileCoinAddress;
134 }
135
136 public UnidentifiedAccessMode getUnidentifiedAccessMode() {
137 return unidentifiedAccessMode;
138 }
139
140 public Set<Capability> getCapabilities() {
141 return capabilities;
142 }
143
144 public PhoneNumberSharingMode getPhoneNumberSharingMode() {
145 return phoneNumberSharingMode;
146 }
147
148 public enum UnidentifiedAccessMode {
149 UNKNOWN,
150 DISABLED,
151 ENABLED,
152 UNRESTRICTED;
153
154 public static UnidentifiedAccessMode valueOfOrUnknown(String value) {
155 try {
156 return valueOf(value);
157 } catch (IllegalArgumentException ignored) {
158 return UNKNOWN;
159 }
160 }
161 }
162
163 public enum Capability {
164 storage;
165
166 public static Capability valueOfOrNull(String value) {
167 try {
168 return valueOf(value);
169 } catch (IllegalArgumentException ignored) {
170 return null;
171 }
172 }
173 }
174
175 @Override
176 public boolean equals(final Object o) {
177 if (this == o) return true;
178 if (o == null || getClass() != o.getClass()) return false;
179 final Profile profile = (Profile) o;
180 return lastUpdateTimestamp == profile.lastUpdateTimestamp
181 && Objects.equals(givenName, profile.givenName)
182 && Objects.equals(familyName, profile.familyName)
183 && Objects.equals(about, profile.about)
184 && Objects.equals(aboutEmoji, profile.aboutEmoji)
185 && Objects.equals(avatarUrlPath, profile.avatarUrlPath)
186 && unidentifiedAccessMode == profile.unidentifiedAccessMode
187 && Objects.equals(capabilities, profile.capabilities);
188 }
189
190 @Override
191 public int hashCode() {
192 return Objects.hash(lastUpdateTimestamp,
193 givenName,
194 familyName,
195 about,
196 aboutEmoji,
197 avatarUrlPath,
198 unidentifiedAccessMode,
199 capabilities);
200 }
201
202 public static final class Builder {
203
204 private String givenName;
205 private String familyName;
206 private String about;
207 private String aboutEmoji;
208 private String avatarUrlPath;
209 private byte[] mobileCoinAddress;
210 private UnidentifiedAccessMode unidentifiedAccessMode = UnidentifiedAccessMode.UNKNOWN;
211 private Set<Capability> capabilities = Collections.emptySet();
212 private PhoneNumberSharingMode phoneNumberSharingMode;
213 private long lastUpdateTimestamp = 0;
214
215 private Builder() {
216 }
217
218 public Builder withGivenName(final String val) {
219 givenName = val;
220 return this;
221 }
222
223 public Builder withFamilyName(final String val) {
224 familyName = val;
225 return this;
226 }
227
228 public Builder withAbout(final String val) {
229 about = val;
230 return this;
231 }
232
233 public Builder withAboutEmoji(final String val) {
234 aboutEmoji = val;
235 return this;
236 }
237
238 public Builder withAvatarUrlPath(final String val) {
239 avatarUrlPath = val;
240 return this;
241 }
242
243 public Builder withUnidentifiedAccessMode(final UnidentifiedAccessMode val) {
244 unidentifiedAccessMode = val;
245 return this;
246 }
247
248 public Builder withCapabilities(final Set<Capability> val) {
249 capabilities = val;
250 return this;
251 }
252
253 public Builder withPhoneNumberSharingMode(final PhoneNumberSharingMode val) {
254 phoneNumberSharingMode = val;
255 return this;
256 }
257
258 public Profile build() {
259 return new Profile(this);
260 }
261
262 public Builder withLastUpdateTimestamp(final long val) {
263 lastUpdateTimestamp = val;
264 return this;
265 }
266
267 public Builder withMobileCoinAddress(final byte[] val) {
268 mobileCoinAddress = val;
269 return this;
270 }
271 }
272 }