Add json output listContacts command

This commit is contained in:
AsamK 2021-08-21 19:03:51 +02:00
parent 11c90fa032
commit a18d6b3fe4

View file

@ -3,13 +3,17 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.manager.Manager;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.asamk.signal.util.Util.getLegacyIdentifier;
public class ListContactsCommand implements LocalCommand {
public class ListContactsCommand implements JsonRpcLocalCommand {
@Override
public String getName() {
@ -23,14 +27,56 @@ public class ListContactsCommand implements LocalCommand {
@Override
public void handleCommand(final Namespace ns, final Manager m, final OutputWriter outputWriter) {
final var writer = (PlainTextWriter) outputWriter;
var contacts = m.getContacts();
for (var c : contacts) {
writer.println("Number: {} Name: {} Blocked: {}",
getLegacyIdentifier(m.resolveSignalServiceAddress(c.first())),
c.second().getName(),
c.second().isBlocked());
if (outputWriter instanceof PlainTextWriter) {
final var writer = (PlainTextWriter) outputWriter;
for (var c : contacts) {
final var contact = c.second();
writer.println("Number: {} Name: {} Blocked: {} Message expiration: {}",
getLegacyIdentifier(m.resolveSignalServiceAddress(c.first())),
contact.getName(),
contact.isBlocked(),
contact.getMessageExpirationTime() == 0
? "disabled"
: contact.getMessageExpirationTime() + "s");
}
} else {
final var writer = (JsonWriter) outputWriter;
final var jsonContacts = contacts.stream().map(contactPair -> {
final var address = m.resolveSignalServiceAddress(contactPair.first());
final var contact = contactPair.second();
return new JsonContact(address.getNumber().orNull(),
address.getUuid().transform(UUID::toString).orNull(),
contact.getName(),
contact.isBlocked(),
contact.getMessageExpirationTime());
}).collect(Collectors.toList());
writer.write(jsonContacts);
}
}
private static final class JsonContact {
public final String number;
public final String uuid;
public final String name;
public final boolean isBlocked;
public final int messageExpirationTime;
private JsonContact(
final String number,
final String uuid,
final String name,
final boolean isBlocked,
final int messageExpirationTime
) {
this.number = number;
this.uuid = uuid;
this.name = name;
this.isBlocked = isBlocked;
this.messageExpirationTime = messageExpirationTime;
}
}
}