mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Improve user status command
This commit is contained in:
parent
1c5de83370
commit
00339b1abe
4 changed files with 21 additions and 33 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue