Implement startLink and finishLink for jsonrpc daemon

This commit is contained in:
AsamK 2021-11-11 18:27:16 +01:00
parent 79cc225869
commit b7005884fd
9 changed files with 242 additions and 1 deletions

View file

@ -13,6 +13,7 @@ public class Commands {
addCommand(new AddDeviceCommand());
addCommand(new BlockCommand());
addCommand(new DaemonCommand());
addCommand(new FinishLinkCommand());
addCommand(new GetUserStatusCommand());
addCommand(new JoinGroupCommand());
addCommand(new JsonRpcDispatcherCommand());
@ -36,6 +37,7 @@ public class Commands {
addCommand(new SendTypingCommand());
addCommand(new SetPinCommand());
addCommand(new SubmitRateLimitChallengeCommand());
addCommand(new StartLinkCommand());
addCommand(new TrustCommand());
addCommand(new UnblockCommand());
addCommand(new UnregisterCommand());

View file

@ -0,0 +1,73 @@
package org.asamk.signal.commands;
import com.fasterxml.jackson.core.type.TypeReference;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeoutException;
public class FinishLinkCommand implements JsonRpcMultiCommand<FinishLinkCommand.FinishLinkParams> {
private final static Logger logger = LoggerFactory.getLogger(FinishLinkCommand.class);
@Override
public String getName() {
return "finishLink";
}
@Override
public TypeReference<FinishLinkParams> getRequestType() {
return new TypeReference<>() {};
}
@Override
public void handleCommand(
final FinishLinkParams request, final MultiAccountManager m, final JsonWriter jsonWriter
) throws CommandException {
final URI deviceLinkUri;
try {
deviceLinkUri = new URI(request.deviceLinkUri());
} catch (URISyntaxException e) {
throw new UserErrorException("Invalid device link uri.");
}
final var provisioningManager = m.getProvisioningManagerFor(deviceLinkUri);
if (provisioningManager == null) {
throw new UserErrorException("Unknown device link uri.");
}
var deviceName = request.deviceName();
if (deviceName == null) {
deviceName = "cli";
}
final String number;
try {
number = provisioningManager.finishDeviceLink(deviceName);
} catch (TimeoutException e) {
throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage(), e);
} catch (UserAlreadyExists e) {
throw new UserErrorException("The user "
+ e.getNumber()
+ " already exists\nDelete \""
+ e.getFileName()
+ "\" before trying again.");
}
jsonWriter.write(new JsonFinishLink(number));
}
record FinishLinkParams(String deviceLinkUri, String deviceName) {}
private record JsonFinishLink(String number) {}
}

View file

@ -0,0 +1,41 @@
package org.asamk.signal.commands;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.MultiAccountManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeoutException;
public class StartLinkCommand implements JsonRpcMultiCommand<Void> {
private final static Logger logger = LoggerFactory.getLogger(StartLinkCommand.class);
@Override
public String getName() {
return "startLink";
}
@Override
public void handleCommand(
final Void request, final MultiAccountManager m, final JsonWriter jsonWriter
) throws CommandException {
final URI deviceLinkUri;
try {
deviceLinkUri = m.getNewProvisioningDeviceLinkUri();
} catch (TimeoutException e) {
throw new UserErrorException("Device link creation timed out, please try again.");
} catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage(), e);
}
jsonWriter.write(new JsonLink(deviceLinkUri.toString()));
}
private record JsonLink(String deviceLinkUri) {}
}

View file

@ -122,7 +122,6 @@ public class SignalJsonRpcDispatcherHandler {
final ObjectMapper objectMapper, final String method, ContainerNode<?> params
) throws JsonRpcException {
var command = getCommand(method);
// TODO implement link
if (c != null) {
if (command instanceof JsonRpcMultiCommand<?> jsonRpcCommand) {
return runCommand(objectMapper, params, new MultiCommandRunnerImpl<>(c, jsonRpcCommand));