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