Canonicalize number before getting/setting contact info

This commit is contained in:
AsamK 2020-01-21 22:00:38 +01:00
parent eca1737d28
commit cfd1e5544d
3 changed files with 18 additions and 10 deletions

View file

@ -6,6 +6,7 @@ import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.exceptions.DBusException;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException;
import java.util.List;
@ -20,9 +21,9 @@ public interface Signal extends DBusInterface {
void sendGroupMessage(String message, List<String> attachments, byte[] groupId) throws EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, IOException;
String getContactName(String number);
String getContactName(String number) throws InvalidNumberException;
void setContactName(String number, String name);
void setContactName(String number, String name) throws InvalidNumberException;
List<byte[]> getGroupIds();

View file

@ -4,6 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
public class UpdateContactCommand implements LocalCommand {
@ -27,7 +28,11 @@ public class UpdateContactCommand implements LocalCommand {
String number = ns.getString("number");
String name = ns.getString("name");
m.setContactName(number, name);
try {
m.setContactName(number, name);
} catch (InvalidNumberException e) {
System.out.println("Invalid contact number: " + e.getMessage());
}
return 0;
}

View file

@ -675,8 +675,9 @@ public class Manager implements Signal {
}
@Override
public String getContactName(String number) {
ContactInfo contact = account.getContactStore().getContact(number);
public String getContactName(String number) throws InvalidNumberException {
String canonicalizedNumber = Utils.canonicalizeNumber(number, username);
ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
if (contact == null) {
return "";
} else {
@ -685,14 +686,15 @@ public class Manager implements Signal {
}
@Override
public void setContactName(String number, String name) {
ContactInfo contact = account.getContactStore().getContact(number);
public void setContactName(String number, String name) throws InvalidNumberException {
String canonicalizedNumber = Utils.canonicalizeNumber(number, username);
ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
if (contact == null) {
contact = new ContactInfo();
contact.number = number;
System.err.println("Add contact " + number + " named " + name);
contact.number = canonicalizedNumber;
System.err.println("Add contact " + canonicalizedNumber + " named " + name);
} else {
System.err.println("Updating contact " + number + " name " + contact.name + " -> " + name);
System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
}
contact.name = name;
account.getContactStore().updateContact(contact);