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

View file

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

View file

@ -44,7 +44,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
final ObjectMapper jsonProcessor; final ObjectMapper jsonProcessor;
if (ns.getBoolean("json")) { if (ns.getBoolean("json")) {
jsonProcessor = new ObjectMapper(); 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); jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
} else { } else {
jsonProcessor = null; 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 // Note "contactDetails" has no optionals. It only gives us info on users who are registered
List<ContactTokenDetails> contactDetails = this.accountManager.getContacts(numbers); List<ContactTokenDetails> contactDetails = this.accountManager.getContacts(numbers);
// Make the initial map with all numbers set to false for now Set<String> registeredUsers = contactDetails.stream()
Map<String, Boolean> usersRegistered = numbers.stream().collect(Collectors.toMap(x -> x, x -> false)); .map(ContactTokenDetails::getNumber)
.collect(Collectors.toSet());
// Override the contacts we did obtain return numbers.stream().collect(Collectors.toMap(x -> x, registeredUsers::contains));
for (ContactTokenDetails contactDetail : contactDetails) {
usersRegistered.put(contactDetail.getNumber(), true);
}
return usersRegistered;
} }
public void register(boolean voiceVerification, String captcha) throws IOException { public void register(boolean voiceVerification, String captcha) throws IOException {
@ -524,8 +520,7 @@ public class Manager implements Closeable {
} }
public void verifyAccount( public void verifyAccount(
String verificationCode, String verificationCode, String pin
String pin
) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException { ) throws IOException, KeyBackupSystemNoDataException, KeyBackupServicePinException {
verificationCode = verificationCode.replace("-", ""); verificationCode = verificationCode.replace("-", "");
account.setSignalingKey(KeyUtils.createSignalingKey()); account.setSignalingKey(KeyUtils.createSignalingKey());