Use record classes

This commit is contained in:
AsamK 2021-10-24 22:26:12 +02:00
parent ce70a623c2
commit ce7aa580b6
66 changed files with 754 additions and 1877 deletions

View file

@ -66,21 +66,5 @@ public class GetUserStatusCommand implements JsonRpcLocalCommand {
}
}
private static final class JsonUserStatus {
public final String recipient;
public final String number;
public final String uuid;
public final boolean isRegistered;
public JsonUserStatus(String recipient, String number, String uuid, boolean isRegistered) {
this.recipient = recipient;
this.number = number;
this.uuid = uuid;
this.isRegistered = isRegistered;
}
}
private record JsonUserStatus(String recipient, String number, String uuid, boolean isRegistered) {}
}

View file

@ -69,7 +69,7 @@ public class JoinGroupCommand implements JsonRpcLocalCommand {
writer.println("Joined group \"{}\"", newGroupId.toBase64());
}
}
handleSendMessageResults(results.second().getResults());
handleSendMessageResults(results.second().results());
} catch (GroupPatchNotAcceptedException e) {
throw new UserErrorException("Failed to join group, maybe already a member");
} catch (IOException e) {

View file

@ -16,8 +16,7 @@ import java.util.Map;
public interface JsonRpcLocalCommand extends JsonRpcCommand<Map<String, Object>>, LocalCommand {
default TypeReference<Map<String, Object>> getRequestType() {
return new TypeReference<>() {
};
return new TypeReference<>() {};
}
default void handleCommand(

View file

@ -54,26 +54,5 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
}
}
private static final class JsonContact {
public final String number;
public final String uuid;
public final String name;
public final boolean isBlocked;
public final int messageExpirationTime;
private JsonContact(
final String number,
final String uuid,
final String name,
final boolean isBlocked,
final int messageExpirationTime
) {
this.number = number;
this.uuid = uuid;
this.name = name;
this.isBlocked = isBlocked;
this.messageExpirationTime = messageExpirationTime;
}
}
private record JsonContact(String number, String uuid, String name, boolean isBlocked, int messageExpirationTime) {}
}

View file

@ -45,36 +45,21 @@ public class ListDevicesCommand implements JsonRpcLocalCommand {
if (outputWriter instanceof PlainTextWriter writer) {
for (var d : devices) {
writer.println("- Device {}{}:", d.getId(), (d.isThisDevice() ? " (this device)" : ""));
writer.println("- Device {}{}:", d.id(), (d.isThisDevice() ? " (this device)" : ""));
writer.indent(w -> {
w.println("Name: {}", d.getName());
w.println("Created: {}", DateUtils.formatTimestamp(d.getCreated()));
w.println("Last seen: {}", DateUtils.formatTimestamp(d.getLastSeen()));
w.println("Name: {}", d.name());
w.println("Created: {}", DateUtils.formatTimestamp(d.created()));
w.println("Last seen: {}", DateUtils.formatTimestamp(d.lastSeen()));
});
}
} else {
final var writer = (JsonWriter) outputWriter;
final var jsonDevices = devices.stream()
.map(d -> new JsonDevice(d.getId(), d.getName(), d.getCreated(), d.getLastSeen()))
.map(d -> new JsonDevice(d.id(), d.name(), d.created(), d.lastSeen()))
.collect(Collectors.toList());
writer.write(jsonDevices);
}
}
private static final class JsonDevice {
public final long id;
public final String name;
public final long createdTimestamp;
public final long lastSeenTimestamp;
private JsonDevice(
final long id, final String name, final long createdTimestamp, final long lastSeenTimestamp
) {
this.id = id;
this.name = name;
this.createdTimestamp = createdTimestamp;
this.lastSeenTimestamp = lastSeenTimestamp;
}
}
private record JsonDevice(long id, String name, long createdTimestamp, long lastSeenTimestamp) {}
}

View file

@ -50,25 +50,25 @@ public class ListGroupsCommand implements JsonRpcLocalCommand {
PlainTextWriter writer, Group group, boolean detailed
) {
if (detailed) {
final var groupInviteLink = group.getGroupInviteLinkUrl();
final var groupInviteLink = group.groupInviteLinkUrl();
writer.println(
"Id: {} Name: {} Description: {} Active: {} Blocked: {} Members: {} Pending members: {} Requesting members: {} Admins: {} Message expiration: {} Link: {}",
group.getGroupId().toBase64(),
group.getTitle(),
group.getDescription(),
group.groupId().toBase64(),
group.title(),
group.description(),
group.isMember(),
group.isBlocked(),
resolveMembers(group.getMembers()),
resolveMembers(group.getPendingMembers()),
resolveMembers(group.getRequestingMembers()),
resolveMembers(group.getAdminMembers()),
group.getMessageExpirationTimer() == 0 ? "disabled" : group.getMessageExpirationTimer() + "s",
resolveMembers(group.members()),
resolveMembers(group.pendingMembers()),
resolveMembers(group.requestingMembers()),
resolveMembers(group.adminMembers()),
group.messageExpirationTimer() == 0 ? "disabled" : group.messageExpirationTimer() + "s",
groupInviteLink == null ? '-' : groupInviteLink.getUrl());
} else {
writer.println("Id: {} Name: {} Active: {} Blocked: {}",
group.getGroupId().toBase64(),
group.getTitle(),
group.groupId().toBase64(),
group.title(),
group.isMember(),
group.isBlocked());
}
@ -83,21 +83,21 @@ public class ListGroupsCommand implements JsonRpcLocalCommand {
if (outputWriter instanceof JsonWriter jsonWriter) {
var jsonGroups = groups.stream().map(group -> {
final var groupInviteLink = group.getGroupInviteLinkUrl();
final var groupInviteLink = group.groupInviteLinkUrl();
return new JsonGroup(group.getGroupId().toBase64(),
group.getTitle(),
group.getDescription(),
return new JsonGroup(group.groupId().toBase64(),
group.title(),
group.description(),
group.isMember(),
group.isBlocked(),
group.getMessageExpirationTimer(),
resolveJsonMembers(group.getMembers()),
resolveJsonMembers(group.getPendingMembers()),
resolveJsonMembers(group.getRequestingMembers()),
resolveJsonMembers(group.getAdminMembers()),
group.getPermissionAddMember().name(),
group.getPermissionEditDetails().name(),
group.getPermissionSendMessage().name(),
group.messageExpirationTimer(),
resolveJsonMembers(group.members()),
resolveJsonMembers(group.pendingMembers()),
resolveJsonMembers(group.requestingMembers()),
resolveJsonMembers(group.adminMembers()),
group.permissionAddMember().name(),
group.permissionEditDetails().name(),
group.permissionSendMessage().name(),
groupInviteLink == null ? null : groupInviteLink.getUrl());
}).collect(Collectors.toList());
@ -111,66 +111,22 @@ public class ListGroupsCommand implements JsonRpcLocalCommand {
}
}
private static final class JsonGroup {
private record JsonGroup(
String id,
String name,
String description,
boolean isMember,
boolean isBlocked,
int messageExpirationTime,
Set<JsonGroupMember> members,
Set<JsonGroupMember> pendingMembers,
Set<JsonGroupMember> requestingMembers,
Set<JsonGroupMember> admins,
String permissionAddMember,
String permissionEditDetails,
String permissionSendMessage,
String groupInviteLink
) {}
public final String id;
public final String name;
public final String description;
public final boolean isMember;
public final boolean isBlocked;
public final int messageExpirationTime;
public final Set<JsonGroupMember> members;
public final Set<JsonGroupMember> pendingMembers;
public final Set<JsonGroupMember> requestingMembers;
public final Set<JsonGroupMember> admins;
public final String permissionAddMember;
public final String permissionEditDetails;
public final String permissionSendMessage;
public final String groupInviteLink;
public JsonGroup(
String id,
String name,
String description,
boolean isMember,
boolean isBlocked,
final int messageExpirationTime,
Set<JsonGroupMember> members,
Set<JsonGroupMember> pendingMembers,
Set<JsonGroupMember> requestingMembers,
Set<JsonGroupMember> admins,
final String permissionAddMember,
final String permissionEditDetails,
final String permissionSendMessage,
String groupInviteLink
) {
this.id = id;
this.name = name;
this.description = description;
this.isMember = isMember;
this.isBlocked = isBlocked;
this.messageExpirationTime = messageExpirationTime;
this.members = members;
this.pendingMembers = pendingMembers;
this.requestingMembers = requestingMembers;
this.admins = admins;
this.permissionAddMember = permissionAddMember;
this.permissionEditDetails = permissionEditDetails;
this.permissionSendMessage = permissionSendMessage;
this.groupInviteLink = groupInviteLink;
}
}
private static final class JsonGroupMember {
public final String number;
public final String uuid;
private JsonGroupMember(final String number, final String uuid) {
this.number = number;
this.uuid = uuid;
}
}
private record JsonGroupMember(String number, String uuid) {}
}

View file

@ -30,12 +30,12 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
}
private static void printIdentityFingerprint(PlainTextWriter writer, Manager m, Identity theirId) {
final SignalServiceAddress address = theirId.getRecipient().toSignalServiceAddress();
var digits = Util.formatSafetyNumber(theirId.getSafetyNumber());
final SignalServiceAddress address = theirId.recipient().toSignalServiceAddress();
var digits = Util.formatSafetyNumber(theirId.safetyNumber());
writer.println("{}: {} Added: {} Fingerprint: {} Safety Number: {}",
address.getNumber().orNull(),
theirId.getTrustLevel(),
theirId.getDateAdded(),
theirId.trustLevel(),
theirId.dateAdded(),
Hex.toString(theirId.getFingerprint()),
digits);
}
@ -66,9 +66,9 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
} else {
final var writer = (JsonWriter) outputWriter;
final var jsonIdentities = identities.stream().map(id -> {
final var address = id.getRecipient().toSignalServiceAddress();
var safetyNumber = Util.formatSafetyNumber(id.getSafetyNumber());
var scannableSafetyNumber = id.getScannableSafetyNumber();
final var address = id.recipient().toSignalServiceAddress();
var safetyNumber = Util.formatSafetyNumber(id.safetyNumber());
var scannableSafetyNumber = id.scannableSafetyNumber();
return new JsonIdentity(address.getNumber().orNull(),
address.getUuid().toString(),
Hex.toString(id.getFingerprint()),
@ -76,40 +76,21 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
scannableSafetyNumber == null
? null
: Base64.getEncoder().encodeToString(scannableSafetyNumber),
id.getTrustLevel().name(),
id.getDateAdded().getTime());
id.trustLevel().name(),
id.dateAdded().getTime());
}).collect(Collectors.toList());
writer.write(jsonIdentities);
}
}
private static final class JsonIdentity {
public final String number;
public final String uuid;
public final String fingerprint;
public final String safetyNumber;
public final String scannableSafetyNumber;
public final String trustLevel;
public final long addedTimestamp;
private JsonIdentity(
final String number,
final String uuid,
final String fingerprint,
final String safetyNumber,
final String scannableSafetyNumber,
final String trustLevel,
final long addedTimestamp
) {
this.number = number;
this.uuid = uuid;
this.fingerprint = fingerprint;
this.safetyNumber = safetyNumber;
this.scannableSafetyNumber = scannableSafetyNumber;
this.trustLevel = trustLevel;
this.addedTimestamp = addedTimestamp;
}
}
private record JsonIdentity(
String number,
String uuid,
String fingerprint,
String safetyNumber,
String scannableSafetyNumber,
String trustLevel,
long addedTimestamp
) {}
}

View file

@ -55,9 +55,9 @@ public class QuitGroupCommand implements JsonRpcLocalCommand {
try {
try {
final var results = m.quitGroup(groupId, groupAdmins);
final var timestamp = results.getTimestamp();
final var timestamp = results.timestamp();
outputResult(outputWriter, timestamp);
handleSendMessageResults(results.getResults());
handleSendMessageResults(results.results());
} catch (NotAGroupMemberException e) {
logger.info("User is not a group member");
}

View file

@ -61,19 +61,19 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
if (outputWriter instanceof JsonWriter jsonWriter) {
dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
var envelope = new JsonMessageEnvelope(messageReceived);
var envelope = JsonMessageEnvelope.from(messageReceived);
final var object = Map.of("envelope", envelope);
jsonWriter.write(object);
});
dbusconnection.addSigHandler(Signal.ReceiptReceived.class, signal, receiptReceived -> {
var envelope = new JsonMessageEnvelope(receiptReceived);
var envelope = JsonMessageEnvelope.from(receiptReceived);
final var object = Map.of("envelope", envelope);
jsonWriter.write(object);
});
dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, signal, syncReceived -> {
var envelope = new JsonMessageEnvelope(syncReceived);
var envelope = JsonMessageEnvelope.from(syncReceived);
final var object = Map.of("envelope", envelope);
jsonWriter.write(object);
});

View file

@ -56,8 +56,8 @@ public class RemoteDeleteCommand implements JsonRpcLocalCommand {
try {
final var results = m.sendRemoteDeleteMessage(targetTimestamp, recipientIdentifiers);
outputResult(outputWriter, results.getTimestamp());
ErrorUtils.handleSendMessageResults(results.getResults());
outputResult(outputWriter, results.timestamp());
ErrorUtils.handleSendMessageResults(results.results());
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
throw new UserErrorException(e.getMessage());
} catch (IOException e) {

View file

@ -102,8 +102,8 @@ public class SendCommand implements JsonRpcLocalCommand {
try {
var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
outputResult(outputWriter, results.getTimestamp());
ErrorUtils.handleSendMessageResults(results.getResults());
outputResult(outputWriter, results.timestamp());
ErrorUtils.handleSendMessageResults(results.results());
} catch (AttachmentInvalidException | IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
.getSimpleName() + ")", e);

View file

@ -72,8 +72,8 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
targetTimestamp,
recipientIdentifiers);
outputResult(outputWriter, results.getTimestamp());
ErrorUtils.handleSendMessageResults(results.getResults());
outputResult(outputWriter, results.timestamp());
ErrorUtils.handleSendMessageResults(results.results());
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
throw new UserErrorException(e.getMessage());
} catch (IOException e) {

View file

@ -127,8 +127,8 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
var results = m.createGroup(groupName,
groupMembers,
groupAvatar == null ? null : new File(groupAvatar));
timestamp = results.second().getTimestamp();
ErrorUtils.handleSendMessageResults(results.second().getResults());
timestamp = results.second().timestamp();
ErrorUtils.handleSendMessageResults(results.second().results());
groupId = results.first();
groupName = null;
groupMembers = null;
@ -154,8 +154,8 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
: groupSendMessagesPermission == GroupPermission.ONLY_ADMINS)
.build());
if (results != null) {
timestamp = results.getTimestamp();
ErrorUtils.handleSendMessageResults(results.getResults());
timestamp = results.timestamp();
ErrorUtils.handleSendMessageResults(results.results());
}
outputResult(outputWriter, timestamp, isNewGroup ? groupId : null);
} catch (AttachmentInvalidException e) {