@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 {
@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) {
- 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);
@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) {
- 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);
}
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;
}
} catch (InvalidInputException ignored) {
}
}
- ContactInfo contact = account.getContactStore().getContact(source.getNumber().get());
+ ContactInfo contact = account.getContactStore().getContact(source);
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);
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) {
- contact = new ContactInfo();
- contact.number = c.getAddress().getNumber().get();
+ contact = new ContactInfo(c.getAddress());
}
if (c.getName().isPresent()) {
contact.name = c.getName().get();
}
public ContactInfo getContact(String number) {
- return account.getContactStore().getContact(number);
+ return account.getContactStore().getContact(new SignalServiceAddress(null, number));
}
public GroupInfo getGroup(byte[] groupId) {
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+import java.util.UUID;
+
public class ContactInfo {
@JsonProperty
@JsonProperty
public String number;
+ @JsonProperty
+ public UUID uuid;
+
@JsonProperty
public String color;
@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() {
- return new SignalServiceAddress(null, number);
+ return new SignalServiceAddress(uuid, number);
}
}
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.HashMap;
import java.util.List;
-import java.util.Map;
public class JsonContactsStore {
- private static final ObjectMapper jsonProcessor = new ObjectMapper();
@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) {
- 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() {
- return new ArrayList<>(contacts.values());
+ return new ArrayList<>(contacts);
}
/**
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;
- }
- }
}