Implement support for usernames

This commit is contained in:
AsamK 2023-04-03 19:00:05 +02:00
parent 03f193b34c
commit 9f60ed534a
18 changed files with 440 additions and 46 deletions

View file

@ -55,10 +55,12 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
for (var r : recipients) {
final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
final var profile = r.getProfile() == null ? Profile.newBuilder().build() : r.getProfile();
writer.println("Number: {} Name: {} Profile name: {} Color: {} Blocked: {} Message expiration: {}",
writer.println(
"Number: {} Name: {} Profile name: {} Username: {} Color: {} Blocked: {} Message expiration: {}",
r.getAddress().getLegacyIdentifier(),
contact.getName(),
profile.getDisplayName(),
r.getAddress().username().orElse(""),
contact.getColor(),
contact.isBlocked(),
contact.getMessageExpirationTime() == 0
@ -72,6 +74,7 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
final var contact = r.getContact() == null ? Contact.newBuilder().build() : r.getContact();
return new JsonContact(address.number().orElse(null),
address.uuid().map(UUID::toString).orElse(null),
address.username().orElse(null),
contact.getName(),
contact.getColor(),
contact.isBlocked(),
@ -96,6 +99,7 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
private record JsonContact(
String number,
String uuid,
String username,
String name,
String color,
boolean isBlocked,

View file

@ -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
) {}
}

View file

@ -10,6 +10,7 @@ import org.asamk.signal.manager.api.Group;
import org.asamk.signal.manager.api.Identity;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidUsernameException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NotPrimaryDeviceException;
@ -151,6 +152,16 @@ public class DbusManagerImpl implements Manager {
updateProfile.isDeleteAvatar());
}
@Override
public String setUsername(final String username) throws IOException, InvalidUsernameException {
throw new UnsupportedOperationException();
}
@Override
public void deleteUsername() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void unregister() throws IOException {
signal.unregister();