]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/storage/groups/GroupInfo.java
21ba910f485b80fc9773da05ca0c8fe5518d6cff
[signal-cli] / src / main / java / org / asamk / signal / storage / groups / GroupInfo.java
1 package org.asamk.signal.storage.groups;
2
3 import com.fasterxml.jackson.annotation.JsonIgnore;
4 import com.fasterxml.jackson.annotation.JsonProperty;
5 import com.fasterxml.jackson.core.JsonGenerator;
6 import com.fasterxml.jackson.core.JsonParser;
7 import com.fasterxml.jackson.databind.DeserializationContext;
8 import com.fasterxml.jackson.databind.JsonDeserializer;
9 import com.fasterxml.jackson.databind.JsonNode;
10 import com.fasterxml.jackson.databind.JsonSerializer;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import com.fasterxml.jackson.databind.SerializerProvider;
13 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
14 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
15
16 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
17
18 import java.io.IOException;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.Set;
22 import java.util.UUID;
23
24 public class GroupInfo {
25
26 private static final ObjectMapper jsonProcessor = new ObjectMapper();
27
28 @JsonProperty
29 public final byte[] groupId;
30
31 @JsonProperty
32 public String name;
33
34 @JsonProperty
35 @JsonDeserialize(using = MembersDeserializer.class)
36 @JsonSerialize(using = MembersSerializer.class)
37 public Set<SignalServiceAddress> members = new HashSet<>();
38 @JsonProperty
39 public String color;
40 @JsonProperty(defaultValue = "false")
41 public boolean blocked;
42 @JsonProperty
43 public Integer inboxPosition;
44 @JsonProperty(defaultValue = "false")
45 public boolean archived;
46
47 private long avatarId;
48
49 @JsonProperty
50 @JsonIgnore
51 private boolean active;
52
53 public GroupInfo(byte[] groupId) {
54 this.groupId = groupId;
55 }
56
57 public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<SignalServiceAddress> members, @JsonProperty("avatarId") long avatarId, @JsonProperty("color") String color, @JsonProperty("blocked") boolean blocked, @JsonProperty("inboxPosition") Integer inboxPosition, @JsonProperty("archived") boolean archived) {
58 this.groupId = groupId;
59 this.name = name;
60 this.members.addAll(members);
61 this.avatarId = avatarId;
62 this.color = color;
63 this.blocked = blocked;
64 this.inboxPosition = inboxPosition;
65 this.archived = archived;
66 }
67
68 @JsonIgnore
69 public long getAvatarId() {
70 return avatarId;
71 }
72
73 @JsonIgnore
74 public Set<SignalServiceAddress> getMembers() {
75 return members;
76 }
77
78 @JsonIgnore
79 public Set<String> getMembersE164() {
80 Set<String> membersE164 = new HashSet<>();
81 for (SignalServiceAddress member : members) {
82 if (!member.getNumber().isPresent()) {
83 continue;
84 }
85 membersE164.add(member.getNumber().get());
86 }
87 return membersE164;
88 }
89
90 @JsonIgnore
91 public Set<SignalServiceAddress> getMembersWithout(SignalServiceAddress address) {
92 Set<SignalServiceAddress> members = new HashSet<>(this.members.size());
93 for (SignalServiceAddress member : this.members) {
94 if (!member.matches(address)) {
95 members.add(member);
96 }
97 }
98 return members;
99 }
100
101 public void addMembers(Collection<SignalServiceAddress> addresses) {
102 for (SignalServiceAddress address : addresses) {
103 removeMember(address);
104 this.members.add(address);
105 }
106 }
107
108 public void removeMember(SignalServiceAddress address) {
109 this.members.removeIf(member -> member.matches(address));
110 }
111
112 @JsonIgnore
113 public boolean isMember(SignalServiceAddress address) {
114 for (SignalServiceAddress member : this.members) {
115 if (member.matches(address)) {
116 return true;
117 }
118 }
119 return false;
120 }
121
122 private static final class JsonSignalServiceAddress {
123
124 @JsonProperty
125 private UUID uuid;
126
127 @JsonProperty
128 private String number;
129
130 JsonSignalServiceAddress(@JsonProperty("uuid") final UUID uuid, @JsonProperty("number") final String number) {
131 this.uuid = uuid;
132 this.number = number;
133 }
134
135 JsonSignalServiceAddress(SignalServiceAddress address) {
136 this.uuid = address.getUuid().orNull();
137 this.number = address.getNumber().orNull();
138 }
139
140 SignalServiceAddress toSignalServiceAddress() {
141 return new SignalServiceAddress(uuid, number);
142 }
143 }
144
145 private static class MembersSerializer extends JsonSerializer<Set<SignalServiceAddress>> {
146
147 @Override
148 public void serialize(final Set<SignalServiceAddress> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
149 jgen.writeStartArray(value.size());
150 for (SignalServiceAddress address : value) {
151 if (address.getUuid().isPresent()) {
152 jgen.writeObject(new JsonSignalServiceAddress(address));
153 } else {
154 jgen.writeString(address.getNumber().get());
155 }
156 }
157 jgen.writeEndArray();
158 }
159 }
160
161 private static class MembersDeserializer extends JsonDeserializer<Set<SignalServiceAddress>> {
162
163 @Override
164 public Set<SignalServiceAddress> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
165 Set<SignalServiceAddress> addresses = new HashSet<>();
166 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
167 for (JsonNode n : node) {
168 if (n.isTextual()) {
169 addresses.add(new SignalServiceAddress(null, n.textValue()));
170 } else {
171 JsonSignalServiceAddress address = jsonProcessor.treeToValue(n, JsonSignalServiceAddress.class);
172 addresses.add(address.toSignalServiceAddress());
173 }
174 }
175
176 return addresses;
177 }
178 }
179 }