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

@ -3,7 +3,6 @@ package org.asamk.signal;
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 com.fasterxml.jackson.databind.node.ObjectNode;
@ -23,8 +22,7 @@ public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler
public JsonReceiveMessageHandler(Manager m) {
this.m = m;
this.jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}

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;

View file

@ -394,15 +394,11 @@ public class Manager implements Closeable {
// Note "contactDetails" has no optionals. It only gives us info on users who are registered
List<ContactTokenDetails> contactDetails = this.accountManager.getContacts(numbers);
// Make the initial map with all numbers set to false for now
Map<String, Boolean> usersRegistered = numbers.stream().collect(Collectors.toMap(x -> x, x -> false));
Set<String> registeredUsers = contactDetails.stream()
.map(ContactTokenDetails::getNumber)
.collect(Collectors.toSet());
// Override the contacts we did obtain
for (ContactTokenDetails contactDetail : contactDetails) {
usersRegistered.put(contactDetail.getNumber(), true);
}
return usersRegistered;
return numbers.stream().collect(Collectors.toMap(x -> x, registeredUsers::contains));
}
public void register(boolean voiceVerification, String captcha) throws IOException {
@ -524,8 +520,7 @@ public class Manager implements Closeable {
}
public void verifyAccount(
String verificationCode,
String pin
String verificationCode, String pin
) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
verificationCode = verificationCode.replace("-", "");
account.setSignalingKey(KeyUtils.createSignalingKey());