Improve user status command

This commit is contained in:
AsamK 2021-01-03 21:04:32 +01:00
parent 1c5de83370
commit 00339b1abe
4 changed files with 21 additions and 33 deletions

View file

@ -1,9 +1,6 @@
package org.asamk.signal.commands;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.argparse4j.impl.Arguments;
@ -13,11 +10,10 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.manager.Manager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GetUserStatusCommand implements LocalCommand {
@ -39,14 +35,12 @@ public class GetUserStatusCommand implements LocalCommand {
// Setup the json object mapper
ObjectMapper jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
// Get a map of registration statuses
Map<String, Boolean> registered;
try {
registered = m.areUsersRegistered(new HashSet<>(ns.<String>getList("number")));
registered = m.areUsersRegistered(new HashSet<>(ns.getList("number")));
} catch (IOException e) {
System.err.println("Unable to check if users are registered");
return 1;
@ -54,17 +48,17 @@ public class GetUserStatusCommand implements LocalCommand {
// Output
if (ns.getBoolean("json")) {
List<JsonIsRegistered> objects = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : registered.entrySet()) {
objects.add(new JsonIsRegistered(entry.getKey(), entry.getValue()));
}
List<JsonIsRegistered> objects = registered.entrySet()
.stream()
.map(entry -> new JsonIsRegistered(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
try {
System.out.println(jsonProcessor.writeValueAsString(objects));
jsonProcessor.writeValue(System.out, objects);
System.out.println();
} catch (IOException e) {
System.err.println(e.getMessage());
}
} else {
for (Map.Entry<String, Boolean> entry : registered.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
@ -74,14 +68,15 @@ public class GetUserStatusCommand implements LocalCommand {
return 0;
}
private class JsonIsRegistered {
String name;
boolean isRegistered;
private static final class JsonIsRegistered {
public String name;
public boolean isRegistered;
public JsonIsRegistered(String name, boolean isRegistered) {
this.name = name;
this.isRegistered = isRegistered;
}
}
}

View file

@ -44,7 +44,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
final ObjectMapper jsonProcessor;
if (ns.getBoolean("json")) {
jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
} else {
jsonProcessor = null;