Store contact uuids in contact store

This commit is contained in:
AsamK 2020-03-23 17:43:37 +01:00
parent eb0648828a
commit a4e1d69788
3 changed files with 47 additions and 60 deletions

View 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());
ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
ContactInfo contact = account.getContactStore().getContact(new SignalServiceAddress(null, canonicalizedNumber));
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());
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);
@ -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());
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);
@ -1022,7 +1022,7 @@ public class Manager implements Signal {
}
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;
}
@ -1339,10 +1339,9 @@ public class Manager implements Signal {
} 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);
@ -1624,10 +1623,9 @@ public class Manager implements Signal {
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();
@ -1894,7 +1892,7 @@ public class Manager implements Signal {
}
public ContactInfo getContact(String number) {
return account.getContactStore().getContact(number);
return account.getContactStore().getContact(new SignalServiceAddress(null, number));
}
public GroupInfo getGroup(byte[] groupId) {

View file

@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.util.UUID;
public class ContactInfo {
@JsonProperty
@ -13,6 +15,9 @@ public class ContactInfo {
@JsonProperty
public String number;
@JsonProperty
public UUID uuid;
@JsonProperty
public String color;
@ -28,8 +33,16 @@ public class ContactInfo {
@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);
}
}

View file

@ -1,41 +1,40 @@
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;
}
}
public ContactInfo getContact(String number) {
return contacts.get(number);
contacts.add(contact);
}
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);
}
/**
@ -44,27 +43,4 @@ public class JsonContactsStore {
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;
}
}
}