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