mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Store contact uuids in contact store
This commit is contained in:
parent
eb0648828a
commit
a4e1d69788
3 changed files with 47 additions and 60 deletions
|
@ -742,7 +742,7 @@ public class Manager implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public String getContactName(String number) throws InvalidNumberException {
|
public String getContactName(String number) throws InvalidNumberException {
|
||||||
String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
|
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) {
|
if (contact == null) {
|
||||||
return "";
|
return "";
|
||||||
} else {
|
} else {
|
||||||
|
@ -753,10 +753,10 @@ public class Manager implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public void setContactName(String number, String name) throws InvalidNumberException {
|
public void setContactName(String number, String name) throws InvalidNumberException {
|
||||||
String canonicalizedNumber = Utils.canonicalizeNumber(number, account.getUsername());
|
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 = new ContactInfo(address);
|
||||||
contact.number = canonicalizedNumber;
|
|
||||||
System.err.println("Add contact " + canonicalizedNumber + " named " + name);
|
System.err.println("Add contact " + canonicalizedNumber + " named " + name);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
|
System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
|
||||||
|
@ -769,10 +769,10 @@ public class Manager implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
|
public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
|
||||||
number = Utils.canonicalizeNumber(number, account.getUsername());
|
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 = new ContactInfo(address);
|
||||||
contact.number = number;
|
|
||||||
System.err.println("Adding and " + (blocked ? "blocking" : "unblocking") + " contact " + number);
|
System.err.println("Adding and " + (blocked ? "blocking" : "unblocking") + " contact " + number);
|
||||||
} else {
|
} else {
|
||||||
System.err.println((blocked ? "Blocking" : "Unblocking") + " contact " + number);
|
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) {
|
if (contact == null || contact.profileKey == null) {
|
||||||
return 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 = new ContactInfo(source);
|
||||||
contact.number = source.getNumber().get();
|
|
||||||
}
|
}
|
||||||
contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
|
contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
|
||||||
account.getContactStore().updateContact(contact);
|
account.getContactStore().updateContact(contact);
|
||||||
|
@ -1624,10 +1623,9 @@ public class Manager implements Signal {
|
||||||
if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
|
if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
|
||||||
account.setProfileKey(c.getProfileKey().get());
|
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 = new ContactInfo(c.getAddress());
|
||||||
contact.number = c.getAddress().getNumber().get();
|
|
||||||
}
|
}
|
||||||
if (c.getName().isPresent()) {
|
if (c.getName().isPresent()) {
|
||||||
contact.name = c.getName().get();
|
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) {
|
||||||
|
|
|
@ -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 {
|
public class ContactInfo {
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
|
@ -13,6 +15,9 @@ public class ContactInfo {
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
public String number;
|
public String number;
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
public UUID uuid;
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
public String color;
|
public String color;
|
||||||
|
|
||||||
|
@ -28,8 +33,16 @@ public class ContactInfo {
|
||||||
@JsonProperty(defaultValue = "false")
|
@JsonProperty(defaultValue = "false")
|
||||||
public boolean archived;
|
public boolean archived;
|
||||||
|
|
||||||
|
public ContactInfo() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContactInfo(SignalServiceAddress address) {
|
||||||
|
this.number = address.getNumber().orNull();
|
||||||
|
this.uuid = address.getUuid().orNull();
|
||||||
|
}
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
public SignalServiceAddress getAddress() {
|
public SignalServiceAddress getAddress() {
|
||||||
return new SignalServiceAddress(null, number);
|
return new SignalServiceAddress(uuid, number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,40 @@
|
||||||
package org.asamk.signal.storage.contacts;
|
package org.asamk.signal.storage.contacts;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
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)
|
private List<ContactInfo> contacts = new ArrayList<>();
|
||||||
@JsonDeserialize(using = ContactsDeserializer.class)
|
|
||||||
private Map<String, ContactInfo> contacts = new HashMap<>();
|
|
||||||
|
|
||||||
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) {
|
public ContactInfo getContact(SignalServiceAddress address) {
|
||||||
return contacts.get(number);
|
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() {
|
public void clear() {
|
||||||
contacts.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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue