mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Extend listContacts command with profiles and filtering
This commit is contained in:
parent
496cd5e621
commit
5f941004f5
11 changed files with 196 additions and 83 deletions
|
@ -1,13 +1,20 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.impl.Arguments;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.storage.recipients.Contact;
|
||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ListContactsCommand implements JsonRpcLocalCommand {
|
||||
|
@ -19,19 +26,39 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
|
|||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("Show a list of known contacts with names.");
|
||||
subparser.help("Show a list of known contacts with names and profiles.");
|
||||
subparser.addArgument("recipient").help("Specify one ore more phone numbers to show.").nargs("*");
|
||||
subparser.addArgument("-a", "--all-recipients")
|
||||
.action(Arguments.storeTrue())
|
||||
.help("Include all known recipients, not only contacts.");
|
||||
subparser.addArgument("--blocked")
|
||||
.type(Boolean.class)
|
||||
.help("Specify if only blocked or unblocked contacts should be shown (default: all contacts)");
|
||||
subparser.addArgument("--name").help("Find contacts with the given contact or profile name.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m, final OutputWriter outputWriter) {
|
||||
var contacts = m.getContacts();
|
||||
public void handleCommand(
|
||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var allRecipients = Boolean.TRUE.equals(ns.getBoolean("all-recipients"));
|
||||
final var blocked = ns.getBoolean("blocked");
|
||||
final var recipientStrings = ns.<String>getList("recipient");
|
||||
final var recipientIdentifiers = CommandUtil.getSingleRecipientIdentifiers(recipientStrings, m.getSelfNumber());
|
||||
final var name = ns.getString("name");
|
||||
final var recipients = m.getRecipients(!allRecipients,
|
||||
Optional.ofNullable(blocked),
|
||||
recipientIdentifiers,
|
||||
Optional.ofNullable(name));
|
||||
|
||||
if (outputWriter instanceof PlainTextWriter writer) {
|
||||
for (var c : contacts) {
|
||||
final var contact = c.second();
|
||||
writer.println("Number: {} Name: {} Blocked: {} Message expiration: {}",
|
||||
c.first().getLegacyIdentifier(),
|
||||
for (var r : recipients) {
|
||||
final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
|
||||
final var profile = r.getProfile() == null ? Profile.newBuilder().build() : r.getProfile();
|
||||
writer.println("Number: {} Name: {} Profile name: {} Blocked: {} Message expiration: {}",
|
||||
r.getAddress().getLegacyIdentifier(),
|
||||
contact.getName(),
|
||||
profile.getDisplayName(),
|
||||
contact.isBlocked(),
|
||||
contact.getMessageExpirationTime() == 0
|
||||
? "disabled"
|
||||
|
@ -39,19 +66,47 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
|
|||
}
|
||||
} else {
|
||||
final var writer = (JsonWriter) outputWriter;
|
||||
final var jsonContacts = contacts.stream().map(contactPair -> {
|
||||
final var address = contactPair.first();
|
||||
final var contact = contactPair.second();
|
||||
final var jsonContacts = recipients.stream().map(r -> {
|
||||
final var address = r.getAddress();
|
||||
final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
|
||||
return new JsonContact(address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null),
|
||||
contact.getName(),
|
||||
contact.isBlocked(),
|
||||
contact.getMessageExpirationTime());
|
||||
contact.getMessageExpirationTime(),
|
||||
r.getProfile() == null
|
||||
? null
|
||||
: new JsonContact.JsonProfile(r.getProfile().getLastUpdateTimestamp(),
|
||||
r.getProfile().getGivenName(),
|
||||
r.getProfile().getFamilyName(),
|
||||
r.getProfile().getAbout(),
|
||||
r.getProfile().getAboutEmoji(),
|
||||
r.getProfile().getPaymentAddress() == null
|
||||
? null
|
||||
: Base64.getEncoder()
|
||||
.encodeToString(r.getProfile().getPaymentAddress())));
|
||||
}).toList();
|
||||
|
||||
writer.write(jsonContacts);
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonContact(String number, String uuid, String name, boolean isBlocked, int messageExpirationTime) {}
|
||||
private record JsonContact(
|
||||
String number,
|
||||
String uuid,
|
||||
String name,
|
||||
boolean isBlocked,
|
||||
int messageExpirationTime,
|
||||
JsonProfile profile
|
||||
) {
|
||||
|
||||
private record JsonProfile(
|
||||
long lastUpdateTimestamp,
|
||||
String givenName,
|
||||
String familyName,
|
||||
String about,
|
||||
String aboutEmoji,
|
||||
String paymentAddress
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ import org.asamk.signal.manager.groups.LastGroupAdminException;
|
|||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.manager.storage.recipients.Contact;
|
||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||
import org.asamk.signal.manager.storage.recipients.Recipient;
|
||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||
import org.freedesktop.dbus.DBusMap;
|
||||
import org.freedesktop.dbus.DBusPath;
|
||||
|
@ -547,14 +548,32 @@ public class DbusManagerImpl implements Manager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<RecipientAddress, Contact>> getContacts() {
|
||||
return signal.listNumbers().stream().map(n -> {
|
||||
final var contactName = signal.getContactName(n);
|
||||
if (contactName.length() == 0) {
|
||||
public List<Recipient> getRecipients(
|
||||
final boolean onlyContacts,
|
||||
final Optional<Boolean> blocked,
|
||||
final Collection<RecipientIdentifier.Single> addresses,
|
||||
final Optional<String> name
|
||||
) {
|
||||
final var numbers = addresses.stream()
|
||||
.filter(s -> s instanceof RecipientIdentifier.Number)
|
||||
.map(s -> ((RecipientIdentifier.Number) s).number())
|
||||
.collect(Collectors.toSet());
|
||||
return signal.listNumbers().stream().filter(n -> addresses.isEmpty() || numbers.contains(n)).map(n -> {
|
||||
final var contactBlocked = signal.isContactBlocked(n);
|
||||
if (blocked.isPresent() && blocked.get() != contactBlocked) {
|
||||
return null;
|
||||
}
|
||||
return new Pair<>(new RecipientAddress(null, n),
|
||||
new Contact(contactName, null, 0, signal.isContactBlocked(n), false, false));
|
||||
final var contactName = signal.getContactName(n);
|
||||
if (onlyContacts && contactName.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (name.isPresent() && !name.get().equals(contactName)) {
|
||||
return null;
|
||||
}
|
||||
return Recipient.newBuilder()
|
||||
.withAddress(new RecipientAddress(null, n))
|
||||
.withContact(new Contact(contactName, null, 0, contactBlocked, false, false))
|
||||
.build();
|
||||
}).filter(Objects::nonNull).toList();
|
||||
}
|
||||
|
||||
|
|
|
@ -4,14 +4,12 @@ import org.asamk.Signal;
|
|||
import org.asamk.signal.BaseConfig;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||
import org.asamk.signal.manager.api.Identity;
|
||||
import org.asamk.signal.manager.api.InactiveGroupLinkException;
|
||||
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||
import org.asamk.signal.manager.api.InvalidNumberException;
|
||||
import org.asamk.signal.manager.api.InvalidStickerException;
|
||||
import org.asamk.signal.manager.api.Message;
|
||||
import org.asamk.signal.manager.api.NotMasterDeviceException;
|
||||
import org.asamk.signal.manager.api.Pair;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.asamk.signal.manager.api.SendMessageResult;
|
||||
import org.asamk.signal.manager.api.SendMessageResults;
|
||||
|
@ -28,7 +26,6 @@ import org.asamk.signal.manager.groups.GroupPermission;
|
|||
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
|
||||
import org.asamk.signal.manager.groups.LastGroupAdminException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||
import org.asamk.signal.util.SendMessageResultUtils;
|
||||
import org.freedesktop.dbus.DBusPath;
|
||||
|
@ -55,7 +52,6 @@ import java.util.Objects;
|
|||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class DbusSignalImpl implements Signal {
|
||||
|
||||
|
@ -719,9 +715,9 @@ public class DbusSignalImpl implements Signal {
|
|||
// all numbers the system knows
|
||||
@Override
|
||||
public List<String> listNumbers() {
|
||||
return Stream.concat(m.getIdentities().stream().map(Identity::recipient),
|
||||
m.getContacts().stream().map(Pair::first))
|
||||
.map(a -> a.number().orElse(null))
|
||||
return m.getRecipients(false, Optional.empty(), Set.of(), Optional.empty())
|
||||
.stream()
|
||||
.map(r -> r.getAddress().number().orElse(null))
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.toList();
|
||||
|
@ -729,30 +725,10 @@ public class DbusSignalImpl implements Signal {
|
|||
|
||||
@Override
|
||||
public List<String> getContactNumber(final String name) {
|
||||
// Contact names have precedence.
|
||||
var numbers = new ArrayList<String>();
|
||||
var contacts = m.getContacts();
|
||||
for (var c : contacts) {
|
||||
if (name.equals(c.second().getName())) {
|
||||
numbers.add(c.first().getLegacyIdentifier());
|
||||
}
|
||||
}
|
||||
// Try profiles if no contact name was found
|
||||
for (var identity : m.getIdentities()) {
|
||||
final var address = identity.recipient();
|
||||
var number = address.number().orElse(null);
|
||||
if (number != null) {
|
||||
Profile profile = null;
|
||||
try {
|
||||
profile = m.getRecipientProfile(RecipientIdentifier.Single.fromAddress(address));
|
||||
} catch (IOException | UnregisteredRecipientException ignored) {
|
||||
}
|
||||
if (profile != null && profile.getDisplayName().equals(name)) {
|
||||
numbers.add(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
return numbers;
|
||||
return m.getRecipients(false, Optional.empty(), Set.of(), Optional.of(name))
|
||||
.stream()
|
||||
.map(r -> r.getAddress().getLegacyIdentifier())
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue