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

@ -1,7 +1,6 @@
package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.Signal;
import org.asamk.signal.manager.Manager;
@ -18,76 +17,72 @@ enum JsonSyncMessageType {
REQUEST_SYNC
}
class JsonSyncMessage {
record JsonSyncMessage(
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncDataMessage sentMessage,
@JsonInclude(JsonInclude.Include.NON_NULL) List<String> blockedNumbers,
@JsonInclude(JsonInclude.Include.NON_NULL) List<String> blockedGroupIds,
@JsonInclude(JsonInclude.Include.NON_NULL) List<JsonSyncReadMessage> readMessages,
@JsonInclude(JsonInclude.Include.NON_NULL) JsonSyncMessageType type
) {
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonSyncDataMessage sentMessage;
JsonSyncMessage(
final JsonSyncDataMessage sentMessage,
final List<String> blockedNumbers,
final List<String> blockedGroupIds,
final List<JsonSyncReadMessage> readMessages,
final JsonSyncMessageType type
) {
this.sentMessage = sentMessage;
this.blockedNumbers = blockedNumbers;
this.blockedGroupIds = blockedGroupIds;
this.readMessages = readMessages;
this.type = type;
}
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<String> blockedNumbers;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<String> blockedGroupIds;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<JsonSyncReadMessage> readMessages;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonSyncMessageType type;
JsonSyncMessage(SignalServiceSyncMessage syncMessage, Manager m) {
this.sentMessage = syncMessage.getSent().isPresent()
? new JsonSyncDataMessage(syncMessage.getSent().get(), m)
: null;
static JsonSyncMessage from(SignalServiceSyncMessage syncMessage, Manager m) {
final var sentMessage = syncMessage.getSent().isPresent() ? JsonSyncDataMessage.from(syncMessage.getSent()
.get(), m) : null;
final List<String> blockedNumbers;
final List<String> blockedGroupIds;
if (syncMessage.getBlockedList().isPresent()) {
final var base64 = Base64.getEncoder();
this.blockedNumbers = syncMessage.getBlockedList()
blockedNumbers = syncMessage.getBlockedList()
.get()
.getAddresses()
.stream()
.map(Util::getLegacyIdentifier)
.collect(Collectors.toList());
this.blockedGroupIds = syncMessage.getBlockedList()
blockedGroupIds = syncMessage.getBlockedList()
.get()
.getGroupIds()
.stream()
.map(base64::encodeToString)
.collect(Collectors.toList());
} else {
this.blockedNumbers = null;
this.blockedGroupIds = null;
}
if (syncMessage.getRead().isPresent()) {
this.readMessages = syncMessage.getRead()
.get()
.stream()
.map(JsonSyncReadMessage::new)
.collect(Collectors.toList());
} else {
this.readMessages = null;
blockedNumbers = null;
blockedGroupIds = null;
}
final var readMessages = syncMessage.getRead().isPresent() ? syncMessage.getRead()
.get()
.stream()
.map(JsonSyncReadMessage::from)
.collect(Collectors.toList()) : null;
final JsonSyncMessageType type;
if (syncMessage.getContacts().isPresent()) {
this.type = JsonSyncMessageType.CONTACTS_SYNC;
type = JsonSyncMessageType.CONTACTS_SYNC;
} else if (syncMessage.getGroups().isPresent()) {
this.type = JsonSyncMessageType.GROUPS_SYNC;
type = JsonSyncMessageType.GROUPS_SYNC;
} else if (syncMessage.getRequest().isPresent()) {
this.type = JsonSyncMessageType.REQUEST_SYNC;
type = JsonSyncMessageType.REQUEST_SYNC;
} else {
this.type = null;
type = null;
}
return new JsonSyncMessage(sentMessage, blockedNumbers, blockedGroupIds, readMessages, type);
}
JsonSyncMessage(Signal.SyncMessageReceived messageReceived) {
this.sentMessage = new JsonSyncDataMessage(messageReceived);
this.blockedNumbers = null;
this.blockedGroupIds = null;
this.readMessages = null;
this.type = null;
static JsonSyncMessage from(Signal.SyncMessageReceived messageReceived) {
return new JsonSyncMessage(JsonSyncDataMessage.from(messageReceived), null, null, null, null);
}
}