mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Store group info in json
This commit is contained in:
parent
d0dae4e064
commit
c41ac8e7a3
4 changed files with 121 additions and 10 deletions
28
src/main/java/cli/GroupInfo.java
Normal file
28
src/main/java/cli/GroupInfo.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package cli;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class GroupInfo {
|
||||
@JsonProperty
|
||||
public final byte[] groupId;
|
||||
|
||||
@JsonProperty
|
||||
public String name;
|
||||
|
||||
@JsonProperty
|
||||
public List<String> members = new ArrayList<>();
|
||||
|
||||
@JsonProperty
|
||||
public long avatarId;
|
||||
|
||||
public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection<String> members, @JsonProperty("avatarId") long avatarId) {
|
||||
this.groupId = groupId;
|
||||
this.name = name;
|
||||
this.members.addAll(members);
|
||||
this.avatarId = avatarId;
|
||||
}
|
||||
}
|
50
src/main/java/cli/JsonGroupStore.java
Normal file
50
src/main/java/cli/JsonGroupStore.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
package cli;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonGroupStore {
|
||||
@JsonProperty("groups")
|
||||
@JsonSerialize(using = JsonGroupStore.MapToListSerializer.class)
|
||||
@JsonDeserialize(using = JsonGroupStore.GroupsDeserializer.class)
|
||||
private Map<String, GroupInfo> groups = new HashMap<>();
|
||||
|
||||
private static final ObjectMapper jsonProcessot = new ObjectMapper();
|
||||
|
||||
void updateGroup(GroupInfo group) {
|
||||
groups.put(Base64.encodeBytes(group.groupId), group);
|
||||
}
|
||||
|
||||
GroupInfo getGroup(byte[] groupId) {
|
||||
return groups.get(Base64.encodeBytes(groupId));
|
||||
}
|
||||
|
||||
public static class MapToListSerializer extends JsonSerializer<Map<?, ?>> {
|
||||
@Override
|
||||
public void serialize(final Map<?, ?> value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
|
||||
jgen.writeObject(value.values());
|
||||
}
|
||||
}
|
||||
|
||||
public static class GroupsDeserializer extends JsonDeserializer<Map<String, GroupInfo>> {
|
||||
@Override
|
||||
public Map<String, GroupInfo> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
|
||||
Map<String, GroupInfo> groups = new HashMap<>();
|
||||
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
|
||||
for (JsonNode n : node) {
|
||||
GroupInfo g = jsonProcessot.treeToValue(n, GroupInfo.class);
|
||||
groups.put(Base64.encodeBytes(g.groupId), g);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -262,15 +262,13 @@ public class Main {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(TextSecureEnvelope envelope) {
|
||||
public void handleMessage(TextSecureEnvelope envelope, TextSecureContent content, GroupInfo group) {
|
||||
System.out.println("Envelope from: " + envelope.getSource());
|
||||
System.out.println("Timestamp: " + envelope.getTimestamp());
|
||||
|
||||
if (envelope.isReceipt()) {
|
||||
System.out.println("Got receipt.");
|
||||
} else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) {
|
||||
TextSecureContent content = m.decryptMessage(envelope);
|
||||
|
||||
if (content == null) {
|
||||
System.out.println("Failed to decrypt message.");
|
||||
} else {
|
||||
|
@ -288,6 +286,10 @@ public class Main {
|
|||
System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
|
||||
if (groupInfo.getName().isPresent()) {
|
||||
System.out.println(" Name: " + groupInfo.getName().get());
|
||||
} else if (group != null) {
|
||||
System.out.println(" Name: " + group.name);
|
||||
} else {
|
||||
System.out.println(" Name: <Unknown group>");
|
||||
}
|
||||
System.out.println(" Type: " + groupInfo.getType());
|
||||
if (groupInfo.getMembers().isPresent()) {
|
||||
|
|
|
@ -36,10 +36,7 @@ import org.whispersystems.textsecure.api.TextSecureMessagePipe;
|
|||
import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
|
||||
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
||||
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureContent;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
|
||||
import org.whispersystems.textsecure.api.messages.*;
|
||||
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
||||
import org.whispersystems.textsecure.api.push.TrustStore;
|
||||
import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
|
||||
|
@ -75,6 +72,7 @@ class Manager {
|
|||
|
||||
private JsonAxolotlStore axolotlStore;
|
||||
private TextSecureAccountManager accountManager;
|
||||
private JsonGroupStore groupStore;
|
||||
|
||||
public Manager(String username) {
|
||||
this.username = username;
|
||||
|
@ -107,7 +105,6 @@ class Manager {
|
|||
return node;
|
||||
}
|
||||
|
||||
|
||||
public void load() throws IOException, InvalidKeyException {
|
||||
JsonNode rootNode = jsonProcessot.readTree(new File(getFileName()));
|
||||
|
||||
|
@ -128,6 +125,10 @@ class Manager {
|
|||
}
|
||||
axolotlStore = jsonProcessot.convertValue(getNotNullNode(rootNode, "axolotlStore"), JsonAxolotlStore.class); //new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
|
||||
registered = getNotNullNode(rootNode, "registered").asBoolean();
|
||||
JsonNode groupStoreNode = rootNode.get("groupStore");
|
||||
if (groupStoreNode != null) {
|
||||
groupStore = jsonProcessot.convertValue(groupStoreNode, JsonGroupStore.class);
|
||||
}
|
||||
accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
|
||||
}
|
||||
|
||||
|
@ -140,6 +141,7 @@ class Manager {
|
|||
.put("nextSignedPreKeyId", nextSignedPreKeyId)
|
||||
.put("registered", registered)
|
||||
.putPOJO("axolotlStore", axolotlStore)
|
||||
.putPOJO("groupStore", groupStore)
|
||||
;
|
||||
try {
|
||||
jsonProcessot.writeValue(new File(getFileName()), rootNode);
|
||||
|
@ -152,6 +154,7 @@ class Manager {
|
|||
IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
|
||||
int registrationId = KeyHelper.generateRegistrationId(false);
|
||||
axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
|
||||
groupStore = new JsonGroupStore();
|
||||
registered = false;
|
||||
}
|
||||
|
||||
|
@ -262,7 +265,7 @@ class Manager {
|
|||
}
|
||||
|
||||
public interface ReceiveMessageHandler {
|
||||
void handleMessage(TextSecureEnvelope envelope);
|
||||
void handleMessage(TextSecureEnvelope envelope, TextSecureContent decryptedContent, GroupInfo group);
|
||||
}
|
||||
|
||||
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
|
||||
|
@ -274,9 +277,37 @@ class Manager {
|
|||
|
||||
while (true) {
|
||||
TextSecureEnvelope envelope;
|
||||
TextSecureContent content = null;
|
||||
GroupInfo group = null;
|
||||
try {
|
||||
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS);
|
||||
handler.handleMessage(envelope);
|
||||
if (!envelope.isReceipt()) {
|
||||
content = decryptMessage(envelope);
|
||||
if (content != null) {
|
||||
if (content.getDataMessage().isPresent()) {
|
||||
TextSecureDataMessage message = content.getDataMessage().get();
|
||||
if (message.getGroupInfo().isPresent()) {
|
||||
TextSecureGroup groupInfo = message.getGroupInfo().get();
|
||||
switch (groupInfo.getType()) {
|
||||
case UPDATE:
|
||||
group = new GroupInfo(groupInfo.getGroupId(), groupInfo.getName().get(), groupInfo.getMembers().get(), groupInfo.getAvatar().get().asPointer().getId());
|
||||
groupStore.updateGroup(group);
|
||||
break;
|
||||
case DELIVER:
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
break;
|
||||
case QUIT:
|
||||
group = groupStore.getGroup(groupInfo.getGroupId());
|
||||
if (group != null) {
|
||||
group.members.remove(envelope.getSource());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
handler.handleMessage(envelope, content, group);
|
||||
} catch (TimeoutException e) {
|
||||
if (returnOnTimeout)
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue