]>
nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/GetUserStatusCommand.java
1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
4 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
6 import org
.asamk
.signal
.JsonWriter
;
7 import org
.asamk
.signal
.OutputType
;
8 import org
.asamk
.signal
.PlainTextWriterImpl
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
11 import org
.asamk
.signal
.manager
.Manager
;
12 import org
.slf4j
.Logger
;
13 import org
.slf4j
.LoggerFactory
;
15 import java
.io
.IOException
;
16 import java
.util
.HashSet
;
19 import java
.util
.stream
.Collectors
;
21 public class GetUserStatusCommand
implements LocalCommand
{
23 private final static Logger logger
= LoggerFactory
.getLogger(GetUserStatusCommand
.class);
26 public void attachToSubparser(final Subparser subparser
) {
27 subparser
.help("Check if the specified phone number/s have been registered");
28 subparser
.addArgument("number").help("Phone number").nargs("+");
32 public Set
<OutputType
> getSupportedOutputTypes() {
33 return Set
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
37 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
38 // Setup the json object mapper
39 var inJson
= ns
.get("output") == OutputType
.JSON
;
41 // Get a map of registration statuses
42 Map
<String
, Boolean
> registered
;
44 registered
= m
.areUsersRegistered(new HashSet
<>(ns
.getList("number")));
45 } catch (IOException e
) {
46 logger
.debug("Failed to check registered users", e
);
47 throw new IOErrorException("Unable to check if users are registered");
52 final var jsonWriter
= new JsonWriter(System
.out
);
54 var jsonUserStatuses
= registered
.entrySet()
56 .map(entry
-> new JsonUserStatus(entry
.getKey(), entry
.getValue()))
57 .collect(Collectors
.toList());
59 jsonWriter
.write(jsonUserStatuses
);
61 final var writer
= new PlainTextWriterImpl(System
.out
);
63 for (var entry
: registered
.entrySet()) {
64 writer
.println("{}: {}", entry
.getKey(), entry
.getValue());
69 private static final class JsonUserStatus
{
73 public boolean isRegistered
;
75 public JsonUserStatus(String name
, boolean isRegistered
) {
77 this.isRegistered
= isRegistered
;