]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/Profile.java
Fix creating builder from contact
[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 storageServiceEncryptionV2Capability;
166
167 public static Capability valueOfOrNull(String value) {
168 try {
169 return valueOf(value);
170 } catch (IllegalArgumentException ignored) {
171 return null;
172 }
173 }
174 }
175
176 @Override
177 public boolean equals(final Object o) {
178 if (this == o) return true;
179 if (o == null || getClass() != o.getClass()) return false;
180 final Profile profile = (Profile) o;
181 return lastUpdateTimestamp == profile.lastUpdateTimestamp
182 && Objects.equals(givenName, profile.givenName)
183 && Objects.equals(familyName, profile.familyName)
184 && Objects.equals(about, profile.about)
185 && Objects.equals(aboutEmoji, profile.aboutEmoji)
186 && Objects.equals(avatarUrlPath, profile.avatarUrlPath)
187 && unidentifiedAccessMode == profile.unidentifiedAccessMode
188 && Objects.equals(capabilities, profile.capabilities);
189 }
190
191 @Override
192 public int hashCode() {
193 return Objects.hash(lastUpdateTimestamp,
194 givenName,
195 familyName,
196 about,
197 aboutEmoji,
198 avatarUrlPath,
199 unidentifiedAccessMode,
200 capabilities);
201 }
202
203 public static final class Builder {
204
205 private String givenName;
206 private String familyName;
207 private String about;
208 private String aboutEmoji;
209 private String avatarUrlPath;
210 private byte[] mobileCoinAddress;
211 private UnidentifiedAccessMode unidentifiedAccessMode = UnidentifiedAccessMode.UNKNOWN;
212 private Set<Capability> capabilities = Collections.emptySet();
213 private PhoneNumberSharingMode phoneNumberSharingMode;
214 private long lastUpdateTimestamp = 0;
215
216 private Builder() {
217 }
218
219 public Builder withGivenName(final String val) {
220 givenName = val;
221 return this;
222 }
223
224 public Builder withFamilyName(final String val) {
225 familyName = val;
226 return this;
227 }
228
229 public Builder withAbout(final String val) {
230 about = val;
231 return this;
232 }
233
234 public Builder withAboutEmoji(final String val) {
235 aboutEmoji = val;
236 return this;
237 }
238
239 public Builder withAvatarUrlPath(final String val) {
240 avatarUrlPath = val;
241 return this;
242 }
243
244 public Builder withUnidentifiedAccessMode(final UnidentifiedAccessMode val) {
245 unidentifiedAccessMode = val;
246 return this;
247 }
248
249 public Builder withCapabilities(final Set<Capability> val) {
250 capabilities = val;
251 return this;
252 }
253
254 public Builder withPhoneNumberSharingMode(final PhoneNumberSharingMode val) {
255 phoneNumberSharingMode = val;
256 return this;
257 }
258
259 public Profile build() {
260 return new Profile(this);
261 }
262
263 public Builder withLastUpdateTimestamp(final long val) {
264 lastUpdateTimestamp = val;
265 return this;
266 }
267
268 public Builder withMobileCoinAddress(final byte[] val) {
269 mobileCoinAddress = val;
270 return this;
271 }
272 }
273 }