]> nmode's Git Repositories - signal-cli/commitdiff
Store contact uuids in contact store
authorAsamK <asamk@gmx.de>
Mon, 23 Mar 2020 16:43:37 +0000 (17:43 +0100)
committerAsamK <asamk@gmx.de>
Mon, 23 Mar 2020 16:43:37 +0000 (17:43 +0100)
src/main/java/org/asamk/signal/manager/Manager.java
src/main/java/org/asamk/signal/storage/contacts/ContactInfo.java
src/main/java/org/asamk/signal/storage/contacts/JsonContactsStore.java

index a8be8ad8912610691c7f03925de71512c0266ff8..8364888cbaa29b0d7e1a8af8ae3f1feee1e5d7fb 100644 (file)
@@ -742,7 +742,7 @@ public class Manager implements Signal {
     @Override
     public String getContactName(String number) throws InvalidNumberException {
         String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
     @Override
     public String getContactName(String number) throws InvalidNumberException {
         String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
-        ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
+        ContactInfo contact = account.getContactStore().getContact(new SignalServiceAddress(null, canonicalizedNumber));
         if (contact == null) {
             return "";
         } else {
         if (contact == null) {
             return "";
         } else {
@@ -753,10 +753,10 @@ public class Manager implements Signal {
     @Override
     public void setContactName(String number, String name) throws InvalidNumberException {
         String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
     @Override
     public void setContactName(String number, String name) throws InvalidNumberException {
         String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
-        ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
+        final SignalServiceAddress address = new SignalServiceAddress(null, canonicalizedNumber);
+        ContactInfo contact = account.getContactStore().getContact(address);
         if (contact == null) {
         if (contact == null) {
-            contact = new ContactInfo();
-            contact.number = canonicalizedNumber;
+            contact = new ContactInfo(address);
             System.err.println("Add contact " + canonicalizedNumber + " named " + name);
         } else {
             System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
             System.err.println("Add contact " + canonicalizedNumber + " named " + name);
         } else {
             System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
@@ -769,10 +769,10 @@ public class Manager implements Signal {
     @Override
     public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
         number = Utils.canonicalizeNumber(number, account.getUsername());
     @Override
     public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
         number = Utils.canonicalizeNumber(number, account.getUsername());
-        ContactInfo contact = account.getContactStore().getContact(number);
+        final SignalServiceAddress address = new SignalServiceAddress(null, number);
+        ContactInfo contact = account.getContactStore().getContact(address);
         if (contact == null) {
         if (contact == null) {
-            contact = new ContactInfo();
-            contact.number = number;
+            contact = new ContactInfo(address);
             System.err.println("Adding and " + (blocked ? "blocking" : "unblocking") + " contact " + number);
         } else {
             System.err.println((blocked ? "Blocking" : "Unblocking") + " contact " + number);
             System.err.println("Adding and " + (blocked ? "blocking" : "unblocking") + " contact " + number);
         } else {
             System.err.println((blocked ? "Blocking" : "Unblocking") + " contact " + number);
@@ -1022,7 +1022,7 @@ public class Manager implements Signal {
     }
 
     private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) throws IOException {
     }
 
     private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) throws IOException {
-        ContactInfo contact = account.getContactStore().getContact(recipient.getNumber().get());
+        ContactInfo contact = account.getContactStore().getContact(recipient);
         if (contact == null || contact.profileKey == null) {
             return null;
         }
         if (contact == null || contact.profileKey == null) {
             return null;
         }
@@ -1339,10 +1339,9 @@ public class Manager implements Signal {
                 } catch (InvalidInputException ignored) {
                 }
             }
                 } catch (InvalidInputException ignored) {
                 }
             }
-            ContactInfo contact = account.getContactStore().getContact(source.getNumber().get());
+            ContactInfo contact = account.getContactStore().getContact(source);
             if (contact == null) {
             if (contact == null) {
-                contact = new ContactInfo();
-                contact.number = source.getNumber().get();
+                contact = new ContactInfo(source);
             }
             contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
             account.getContactStore().updateContact(contact);
             }
             contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
             account.getContactStore().updateContact(contact);
@@ -1624,10 +1623,9 @@ public class Manager implements Signal {
                                 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
                                     account.setProfileKey(c.getProfileKey().get());
                                 }
                                 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
                                     account.setProfileKey(c.getProfileKey().get());
                                 }
-                                ContactInfo contact = account.getContactStore().getContact(c.getAddress().getNumber().get());
+                                ContactInfo contact = account.getContactStore().getContact(c.getAddress());
                                 if (contact == null) {
                                 if (contact == null) {
-                                    contact = new ContactInfo();
-                                    contact.number = c.getAddress().getNumber().get();
+                                    contact = new ContactInfo(c.getAddress());
                                 }
                                 if (c.getName().isPresent()) {
                                     contact.name = c.getName().get();
                                 }
                                 if (c.getName().isPresent()) {
                                     contact.name = c.getName().get();
@@ -1894,7 +1892,7 @@ public class Manager implements Signal {
     }
 
     public ContactInfo getContact(String number) {
     }
 
     public ContactInfo getContact(String number) {
-        return account.getContactStore().getContact(number);
+        return account.getContactStore().getContact(new SignalServiceAddress(null, number));
     }
 
     public GroupInfo getGroup(byte[] groupId) {
     }
 
     public GroupInfo getGroup(byte[] groupId) {
index 2ab2a515e69299959fdb77e44aee178eaf9eb25a..b5dadd1ad364272ebbd8bd2ba0ff4f25059d7714 100644 (file)
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 
 
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 
+import java.util.UUID;
+
 public class ContactInfo {
 
     @JsonProperty
 public class ContactInfo {
 
     @JsonProperty
@@ -13,6 +15,9 @@ public class ContactInfo {
     @JsonProperty
     public String number;
 
     @JsonProperty
     public String number;
 
+    @JsonProperty
+    public UUID uuid;
+
     @JsonProperty
     public String color;
 
     @JsonProperty
     public String color;
 
@@ -28,8 +33,16 @@ public class ContactInfo {
     @JsonProperty(defaultValue = "false")
     public boolean archived;
 
     @JsonProperty(defaultValue = "false")
     public boolean archived;
 
+    public ContactInfo() {
+    }
+
+    public ContactInfo(SignalServiceAddress address) {
+        this.number = address.getNumber().orNull();
+        this.uuid = address.getUuid().orNull();
+    }
+
     @JsonIgnore
     public SignalServiceAddress getAddress() {
     @JsonIgnore
     public SignalServiceAddress getAddress() {
-        return new SignalServiceAddress(null, number);
+        return new SignalServiceAddress(uuid, number);
     }
 }
     }
 }
index c10dfbb7bb72d22089a2417f5493441bd3446ac8..86514bc16e14aa2968a951d7f01c548f6c5ca007 100644 (file)
@@ -1,41 +1,40 @@
 package org.asamk.signal.storage.contacts;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 package org.asamk.signal.storage.contacts;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.JsonDeserializer;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.JsonSerializer;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializerProvider;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 
 
-import java.io.IOException;
+import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+
 import java.util.ArrayList;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.List;
-import java.util.Map;
 
 public class JsonContactsStore {
 
 
 public class JsonContactsStore {
 
-    private static final ObjectMapper jsonProcessor = new ObjectMapper();
     @JsonProperty("contacts")
     @JsonProperty("contacts")
-    @JsonSerialize(using = JsonContactsStore.MapToListSerializer.class)
-    @JsonDeserialize(using = ContactsDeserializer.class)
-    private Map<String, ContactInfo> contacts = new HashMap<>();
+    private List<ContactInfo> contacts = new ArrayList<>();
 
     public void updateContact(ContactInfo contact) {
 
     public void updateContact(ContactInfo contact) {
-        contacts.put(contact.number, contact);
+        final SignalServiceAddress contactAddress = contact.getAddress();
+        for (int i = 0; i < contacts.size(); i++) {
+            if (contacts.get(i).getAddress().matches(contactAddress)) {
+                contacts.set(i, contact);
+                return;
+            }
+        }
+
+        contacts.add(contact);
     }
 
     }
 
-    public ContactInfo getContact(String number) {
-        return contacts.get(number);
+    public ContactInfo getContact(SignalServiceAddress address) {
+        for (ContactInfo contact : contacts) {
+            if (contact.getAddress().matches(address)) {
+                return contact;
+            }
+        }
+        return null;
     }
 
     public List<ContactInfo> getContacts() {
     }
 
     public List<ContactInfo> getContacts() {
-        return new ArrayList<>(contacts.values());
+        return new ArrayList<>(contacts);
     }
 
     /**
     }
 
     /**
@@ -44,27 +43,4 @@ public class JsonContactsStore {
     public void clear() {
         contacts.clear();
     }
     public void clear() {
         contacts.clear();
     }
-
-    private 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());
-        }
-    }
-
-    private static class ContactsDeserializer extends JsonDeserializer<Map<String, ContactInfo>> {
-
-        @Override
-        public Map<String, ContactInfo> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
-            Map<String, ContactInfo> contacts = new HashMap<>();
-            JsonNode node = jsonParser.getCodec().readTree(jsonParser);
-            for (JsonNode n : node) {
-                ContactInfo c = jsonProcessor.treeToValue(n, ContactInfo.class);
-                contacts.put(c.number, c);
-            }
-
-            return contacts;
-        }
-    }
 }
 }