mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 19:10:38 +00:00
Implement support for usernames
This commit is contained in:
parent
03f193b34c
commit
9f60ed534a
18 changed files with 440 additions and 46 deletions
|
@ -1,12 +1,17 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.impl.Arguments;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
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.Manager;
|
||||
import org.asamk.signal.manager.api.InvalidUsernameException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -21,6 +26,11 @@ public class UpdateAccountCommand implements JsonRpcLocalCommand {
|
|||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("Update the account attributes on the signal server.");
|
||||
subparser.addArgument("-n", "--device-name").help("Specify a name to describe this device.");
|
||||
var mut = subparser.addMutuallyExclusiveGroup();
|
||||
mut.addArgument("-u", "--username").help("Specify a username that can then be used to contact this account.");
|
||||
mut.addArgument("--delete-username")
|
||||
.action(Arguments.storeTrue())
|
||||
.help("Delete the username associated with this account.");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -33,5 +43,34 @@ public class UpdateAccountCommand implements JsonRpcLocalCommand {
|
|||
} catch (IOException e) {
|
||||
throw new IOErrorException("UpdateAccount error: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
var username = ns.getString("username");
|
||||
if (username != null) {
|
||||
try {
|
||||
final var newUsername = m.setUsername(username);
|
||||
if (outputWriter instanceof PlainTextWriter w) {
|
||||
w.println("Your new username: {}", newUsername);
|
||||
} else if (outputWriter instanceof JsonWriter w) {
|
||||
w.write(new JsonAccountResponse(newUsername));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to set username: " + e.getMessage(), e);
|
||||
} catch (InvalidUsernameException e) {
|
||||
throw new UserErrorException("Invalid username: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
var deleteUsername = Boolean.TRUE.equals(ns.getBoolean("delete-username"));
|
||||
if (deleteUsername) {
|
||||
try {
|
||||
m.deleteUsername();
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to delete username: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonAccountResponse(
|
||||
String username
|
||||
) {}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue