Make fields in json classes final and omit unnecessary nulls when serializing

Fix sender in sync read messages
This commit is contained in:
AsamK 2021-01-17 11:23:02 +01:00
parent dd4225dcb1
commit 46a4c2c0d0
16 changed files with 374 additions and 150 deletions

View file

@ -20,7 +20,7 @@ public class JsonWriter {
this.writer = new OutputStreamWriter(writer, StandardCharsets.UTF_8); this.writer = new OutputStreamWriter(writer, StandardCharsets.UTF_8);
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
} }

View file

@ -1,31 +1,45 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment; import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer; import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
class JsonAttachment { class JsonAttachment {
String contentType; @JsonProperty
String filename; final String contentType;
String id;
int size; @JsonProperty
final String filename;
@JsonProperty
final String id;
@JsonProperty
final Long size;
JsonAttachment(SignalServiceAttachment attachment) { JsonAttachment(SignalServiceAttachment attachment) {
this.contentType = attachment.getContentType(); this.contentType = attachment.getContentType();
final SignalServiceAttachmentPointer pointer = attachment.asPointer();
if (attachment.isPointer()) { if (attachment.isPointer()) {
this.id = String.valueOf(pointer.getRemoteId()); final SignalServiceAttachmentPointer pointer = attachment.asPointer();
if (pointer.getFileName().isPresent()) { this.id = pointer.getRemoteId().toString();
this.filename = pointer.getFileName().get(); this.filename = pointer.getFileName().orNull();
} this.size = pointer.getSize().transform(Integer::longValue).orNull();
if (pointer.getSize().isPresent()) { } else {
this.size = pointer.getSize().get(); final SignalServiceAttachmentStream stream = attachment.asStream();
} this.id = null;
this.filename = stream.getFileName().orNull();
this.size = stream.getLength();
} }
} }
JsonAttachment(String filename) { JsonAttachment(String filename) {
this.filename = filename; this.filename = filename;
this.contentType = null;
this.id = null;
this.size = null;
} }
} }

View file

@ -1,5 +1,8 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.messages.calls.AnswerMessage; import org.whispersystems.signalservice.api.messages.calls.AnswerMessage;
import org.whispersystems.signalservice.api.messages.calls.BusyMessage; import org.whispersystems.signalservice.api.messages.calls.BusyMessage;
import org.whispersystems.signalservice.api.messages.calls.HangupMessage; import org.whispersystems.signalservice.api.messages.calls.HangupMessage;
@ -11,27 +14,31 @@ import java.util.List;
class JsonCallMessage { class JsonCallMessage {
OfferMessage offerMessage; @JsonProperty
AnswerMessage answerMessage; @JsonInclude(JsonInclude.Include.NON_NULL)
BusyMessage busyMessage; final OfferMessage offerMessage;
HangupMessage hangupMessage;
List<IceUpdateMessage> iceUpdateMessages; @JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final AnswerMessage answerMessage;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final BusyMessage busyMessage;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final HangupMessage hangupMessage;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<IceUpdateMessage> iceUpdateMessages;
JsonCallMessage(SignalServiceCallMessage callMessage) { JsonCallMessage(SignalServiceCallMessage callMessage) {
if (callMessage.getOfferMessage().isPresent()) { this.offerMessage = callMessage.getOfferMessage().orNull();
this.offerMessage = callMessage.getOfferMessage().get(); this.answerMessage = callMessage.getAnswerMessage().orNull();
} this.busyMessage = callMessage.getBusyMessage().orNull();
if (callMessage.getAnswerMessage().isPresent()) { this.hangupMessage = callMessage.getHangupMessage().orNull();
this.answerMessage = callMessage.getAnswerMessage().get(); this.iceUpdateMessages = callMessage.getIceUpdateMessages().orNull();
}
if (callMessage.getBusyMessage().isPresent()) {
this.busyMessage = callMessage.getBusyMessage().get();
}
if (callMessage.getHangupMessage().isPresent()) {
this.hangupMessage = callMessage.getHangupMessage().get();
}
if (callMessage.getIceUpdateMessages().isPresent()) {
this.iceUpdateMessages = callMessage.getIceUpdateMessages().get();
}
} }
} }

View file

@ -1,9 +1,13 @@
package org.asamk.signal.json; 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;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup; import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
import org.whispersystems.signalservice.api.messages.SignalServiceGroupContext;
import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2; import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2;
import java.util.List; import java.util.List;
@ -11,38 +15,61 @@ import java.util.stream.Collectors;
class JsonDataMessage { class JsonDataMessage {
long timestamp; @JsonProperty
String message; final long timestamp;
int expiresInSeconds;
JsonReaction reaction; @JsonProperty
JsonQuote quote; final String message;
List<JsonMention> mentions;
List<JsonAttachment> attachments; @JsonProperty
JsonSticker sticker; final Integer expiresInSeconds;
JsonGroupInfo groupInfo;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonReaction reaction;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonQuote quote;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<JsonMention> mentions;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<JsonAttachment> attachments;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonSticker sticker;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonGroupInfo groupInfo;
JsonDataMessage(SignalServiceDataMessage dataMessage, Manager m) { JsonDataMessage(SignalServiceDataMessage dataMessage, Manager m) {
this.timestamp = dataMessage.getTimestamp(); this.timestamp = dataMessage.getTimestamp();
if (dataMessage.getGroupContext().isPresent()) { if (dataMessage.getGroupContext().isPresent()) {
if (dataMessage.getGroupContext().get().getGroupV1().isPresent()) { final SignalServiceGroupContext groupContext = dataMessage.getGroupContext().get();
SignalServiceGroup groupInfo = dataMessage.getGroupContext().get().getGroupV1().get(); if (groupContext.getGroupV1().isPresent()) {
SignalServiceGroup groupInfo = groupContext.getGroupV1().get();
this.groupInfo = new JsonGroupInfo(groupInfo); this.groupInfo = new JsonGroupInfo(groupInfo);
} else if (dataMessage.getGroupContext().get().getGroupV2().isPresent()) { } else if (groupContext.getGroupV2().isPresent()) {
SignalServiceGroupV2 groupInfo = dataMessage.getGroupContext().get().getGroupV2().get(); SignalServiceGroupV2 groupInfo = groupContext.getGroupV2().get();
this.groupInfo = new JsonGroupInfo(groupInfo); this.groupInfo = new JsonGroupInfo(groupInfo);
} else {
this.groupInfo = null;
} }
} else {
this.groupInfo = null;
} }
if (dataMessage.getBody().isPresent()) { this.message = dataMessage.getBody().orNull();
this.message = dataMessage.getBody().get();
}
this.expiresInSeconds = dataMessage.getExpiresInSeconds(); this.expiresInSeconds = dataMessage.getExpiresInSeconds();
if (dataMessage.getReaction().isPresent()) { this.reaction = dataMessage.getReaction().isPresent()
this.reaction = new JsonReaction(dataMessage.getReaction().get(), m); ? new JsonReaction(dataMessage.getReaction().get(), m)
} : null;
if (dataMessage.getQuote().isPresent()) { this.quote = dataMessage.getQuote().isPresent() ? new JsonQuote(dataMessage.getQuote().get(), m) : null;
this.quote = new JsonQuote(dataMessage.getQuote().get(), m);
}
if (dataMessage.getMentions().isPresent()) { if (dataMessage.getMentions().isPresent()) {
this.mentions = dataMessage.getMentions() this.mentions = dataMessage.getMentions()
.get() .get()
@ -61,15 +88,14 @@ class JsonDataMessage {
} else { } else {
this.attachments = List.of(); this.attachments = List.of();
} }
if (dataMessage.getSticker().isPresent()) { this.sticker = dataMessage.getSticker().isPresent() ? new JsonSticker(dataMessage.getSticker().get()) : null;
this.sticker = new JsonSticker(dataMessage.getSticker().get());
}
} }
public JsonDataMessage(Signal.MessageReceived messageReceived) { public JsonDataMessage(Signal.MessageReceived messageReceived) {
timestamp = messageReceived.getTimestamp(); timestamp = messageReceived.getTimestamp();
message = messageReceived.getMessage(); message = messageReceived.getMessage();
groupInfo = messageReceived.getGroupId().length > 0 ? new JsonGroupInfo(messageReceived.getGroupId()) : null; groupInfo = messageReceived.getGroupId().length > 0 ? new JsonGroupInfo(messageReceived.getGroupId()) : null;
expiresInSeconds = null;
reaction = null; // TODO Replace these 4 with the proper commands reaction = null; // TODO Replace these 4 with the proper commands
quote = null; quote = null;
mentions = null; mentions = null;
@ -81,6 +107,7 @@ class JsonDataMessage {
timestamp = messageReceived.getTimestamp(); timestamp = messageReceived.getTimestamp();
message = messageReceived.getMessage(); message = messageReceived.getMessage();
groupInfo = messageReceived.getGroupId().length > 0 ? new JsonGroupInfo(messageReceived.getGroupId()) : null; groupInfo = messageReceived.getGroupId().length > 0 ? new JsonGroupInfo(messageReceived.getGroupId()) : null;
expiresInSeconds = null;
reaction = null; // TODO Replace these 4 with the proper commands reaction = null; // TODO Replace these 4 with the proper commands
quote = null; quote = null;
mentions = null; mentions = null;

View file

@ -1,8 +1,11 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
public class JsonError { public class JsonError {
String message; @JsonProperty
final String message;
public JsonError(Throwable exception) { public JsonError(Throwable exception) {
this.message = exception.getMessage(); this.message = exception.getMessage();

View file

@ -1,41 +1,59 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.signal.manager.groups.GroupUtils; import org.asamk.signal.manager.groups.GroupUtils;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup; import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2; import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
class JsonGroupInfo { class JsonGroupInfo {
String groupId; @JsonProperty
List<String> members; final String groupId;
String name;
String type; @JsonProperty
final String type;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final String name;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<String> members;
JsonGroupInfo(SignalServiceGroup groupInfo) { JsonGroupInfo(SignalServiceGroup groupInfo) {
this.groupId = Base64.getEncoder().encodeToString(groupInfo.getGroupId()); this.groupId = Base64.getEncoder().encodeToString(groupInfo.getGroupId());
if (groupInfo.getMembers().isPresent()) {
this.members = new ArrayList<>(groupInfo.getMembers().get().size());
for (SignalServiceAddress address : groupInfo.getMembers().get()) {
this.members.add(address.getLegacyIdentifier());
}
}
if (groupInfo.getName().isPresent()) {
this.name = groupInfo.getName().get();
}
this.type = groupInfo.getType().toString(); this.type = groupInfo.getType().toString();
this.name = groupInfo.getName().orNull();
if (groupInfo.getMembers().isPresent()) {
this.members = groupInfo.getMembers()
.get()
.stream()
.map(SignalServiceAddress::getLegacyIdentifier)
.collect(Collectors.toList());
} else {
this.members = null;
}
} }
JsonGroupInfo(SignalServiceGroupV2 groupInfo) { JsonGroupInfo(SignalServiceGroupV2 groupInfo) {
this.groupId = GroupUtils.getGroupIdV2(groupInfo.getMasterKey()).toBase64(); this.groupId = GroupUtils.getGroupIdV2(groupInfo.getMasterKey()).toBase64();
this.type = groupInfo.hasSignedGroupChange() ? "UPDATE" : "DELIVER"; this.type = groupInfo.hasSignedGroupChange() ? "UPDATE" : "DELIVER";
this.members = null;
this.name = null;
} }
JsonGroupInfo(byte[] groupId) { JsonGroupInfo(byte[] groupId) {
this.groupId = Base64.getEncoder().encodeToString(groupId); this.groupId = Base64.getEncoder().encodeToString(groupId);
this.type = "DELIVER";
this.members = null;
this.name = null;
} }
} }

View file

@ -1,14 +1,21 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
public class JsonMention { public class JsonMention {
String name; @JsonProperty
int start; final String name;
int length;
@JsonProperty
final int start;
@JsonProperty
final int length;
JsonMention(SignalServiceDataMessage.Mention mention, Manager m) { JsonMention(SignalServiceDataMessage.Mention mention, Manager m) {
this.name = m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid(), null)) this.name = m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid(), null))

View file

@ -1,5 +1,8 @@
package org.asamk.signal.json; 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;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceContent; import org.whispersystems.signalservice.api.messages.SignalServiceContent;
@ -10,61 +13,99 @@ import java.util.List;
public class JsonMessageEnvelope { public class JsonMessageEnvelope {
String source; @JsonProperty
int sourceDevice; final String source;
String relay;
long timestamp; @JsonProperty
JsonDataMessage dataMessage; final Integer sourceDevice;
JsonSyncMessage syncMessage;
JsonCallMessage callMessage; @JsonProperty
JsonReceiptMessage receiptMessage; final String relay;
@JsonProperty
final long timestamp;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonDataMessage dataMessage;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonSyncMessage syncMessage;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonCallMessage callMessage;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonReceiptMessage receiptMessage;
public JsonMessageEnvelope(SignalServiceEnvelope envelope, SignalServiceContent content, Manager m) { public JsonMessageEnvelope(SignalServiceEnvelope envelope, SignalServiceContent content, Manager m) {
if (!envelope.isUnidentifiedSender() && envelope.hasSource()) { if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
SignalServiceAddress source = envelope.getSourceAddress(); SignalServiceAddress source = envelope.getSourceAddress();
this.source = source.getLegacyIdentifier(); this.source = source.getLegacyIdentifier();
this.relay = source.getRelay().isPresent() ? source.getRelay().get() : null; this.sourceDevice = envelope.getSourceDevice();
this.relay = source.getRelay().orNull();
} else if (envelope.isUnidentifiedSender() && content != null) {
this.source = content.getSender().getLegacyIdentifier();
this.sourceDevice = content.getSenderDevice();
this.relay = null;
} else {
this.source = null;
this.sourceDevice = null;
this.relay = null;
} }
this.sourceDevice = envelope.getSourceDevice();
this.timestamp = envelope.getTimestamp(); this.timestamp = envelope.getTimestamp();
if (envelope.isReceipt()) { if (envelope.isReceipt()) {
this.receiptMessage = JsonReceiptMessage.deliveryReceipt(timestamp, List.of(timestamp)); this.receiptMessage = JsonReceiptMessage.deliveryReceipt(timestamp, List.of(timestamp));
} else if (content != null && content.getReceiptMessage().isPresent()) {
this.receiptMessage = new JsonReceiptMessage(content.getReceiptMessage().get());
} else {
this.receiptMessage = null;
} }
if (content != null) {
if (envelope.isUnidentifiedSender()) { this.dataMessage = content != null && content.getDataMessage().isPresent()
this.source = content.getSender().getLegacyIdentifier(); ? new JsonDataMessage(content.getDataMessage().get(), m)
this.sourceDevice = content.getSenderDevice(); : null;
} this.syncMessage = content != null && content.getSyncMessage().isPresent()
if (content.getDataMessage().isPresent()) { ? new JsonSyncMessage(content.getSyncMessage().get(), m)
this.dataMessage = new JsonDataMessage(content.getDataMessage().get(), m); : null;
} this.callMessage = content != null && content.getCallMessage().isPresent()
if (content.getSyncMessage().isPresent()) { ? new JsonCallMessage(content.getCallMessage().get())
this.syncMessage = new JsonSyncMessage(content.getSyncMessage().get(), m); : null;
}
if (content.getCallMessage().isPresent()) {
this.callMessage = new JsonCallMessage(content.getCallMessage().get());
}
if (content.getReceiptMessage().isPresent()) {
this.receiptMessage = new JsonReceiptMessage(content.getReceiptMessage().get());
}
}
} }
public JsonMessageEnvelope(Signal.MessageReceived messageReceived) { public JsonMessageEnvelope(Signal.MessageReceived messageReceived) {
source = messageReceived.getSender(); source = messageReceived.getSender();
sourceDevice = null;
relay = null;
timestamp = messageReceived.getTimestamp(); timestamp = messageReceived.getTimestamp();
receiptMessage = null;
dataMessage = new JsonDataMessage(messageReceived); dataMessage = new JsonDataMessage(messageReceived);
syncMessage = null;
callMessage = null;
} }
public JsonMessageEnvelope(Signal.ReceiptReceived receiptReceived) { public JsonMessageEnvelope(Signal.ReceiptReceived receiptReceived) {
source = receiptReceived.getSender(); source = receiptReceived.getSender();
sourceDevice = null;
relay = null;
timestamp = receiptReceived.getTimestamp(); timestamp = receiptReceived.getTimestamp();
receiptMessage = JsonReceiptMessage.deliveryReceipt(timestamp, List.of(timestamp)); receiptMessage = JsonReceiptMessage.deliveryReceipt(timestamp, List.of(timestamp));
dataMessage = null;
syncMessage = null;
callMessage = null;
} }
public JsonMessageEnvelope(Signal.SyncMessageReceived messageReceived) { public JsonMessageEnvelope(Signal.SyncMessageReceived messageReceived) {
source = messageReceived.getSource(); source = messageReceived.getSource();
sourceDevice = null;
relay = null;
timestamp = messageReceived.getTimestamp(); timestamp = messageReceived.getTimestamp();
receiptMessage = null;
dataMessage = null;
syncMessage = new JsonSyncMessage(messageReceived); syncMessage = new JsonSyncMessage(messageReceived);
callMessage = null;
} }
} }

View file

@ -1,5 +1,8 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
@ -9,12 +12,21 @@ import java.util.stream.Collectors;
public class JsonQuote { public class JsonQuote {
long id; @JsonProperty
String author; final long id;
String text;
List<JsonMention> mentions; @JsonProperty
List<JsonQuotedAttachment> attachments; final String author;
@JsonProperty
final String text;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final List<JsonMention> mentions;
@JsonProperty
final List<JsonQuotedAttachment> attachments;
JsonQuote(SignalServiceDataMessage.Quote quote, Manager m) { JsonQuote(SignalServiceDataMessage.Quote quote, Manager m) {
this.id = quote.getId(); this.id = quote.getId();
@ -26,6 +38,8 @@ public class JsonQuote {
.stream() .stream()
.map(quotedMention -> new JsonMention(quotedMention, m)) .map(quotedMention -> new JsonMention(quotedMention, m))
.collect(Collectors.toList()); .collect(Collectors.toList());
} else {
this.mentions = null;
} }
if (quote.getAttachments().size() > 0) { if (quote.getAttachments().size() > 0) {

View file

@ -1,12 +1,21 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
public class JsonQuotedAttachment { public class JsonQuotedAttachment {
String contentType; @JsonProperty
String filename; final String contentType;
JsonAttachment thumbnail;
@JsonProperty
final String filename;
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
final JsonAttachment thumbnail;
JsonQuotedAttachment(SignalServiceDataMessage.Quote.QuotedAttachment quotedAttachment) { JsonQuotedAttachment(SignalServiceDataMessage.Quote.QuotedAttachment quotedAttachment) {
contentType = quotedAttachment.getContentType(); contentType = quotedAttachment.getContentType();

View file

@ -1,14 +1,23 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Reaction; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Reaction;
public class JsonReaction { public class JsonReaction {
String emoji; @JsonProperty
String targetAuthor; final String emoji;
long targetSentTimestamp;
boolean isRemove; @JsonProperty
final String targetAuthor;
@JsonProperty
final long targetSentTimestamp;
@JsonProperty
final boolean isRemove;
JsonReaction(Reaction reaction, Manager m) { JsonReaction(Reaction reaction, Manager m) {
this.emoji = reaction.getEmoji(); this.emoji = reaction.getEmoji();

View file

@ -1,25 +1,29 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage; import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
import java.util.List; import java.util.List;
class JsonReceiptMessage { class JsonReceiptMessage {
long when; @JsonProperty
boolean isDelivery; final long when;
boolean isRead;
List<Long> timestamps; @JsonProperty
final boolean isDelivery;
@JsonProperty
final boolean isRead;
@JsonProperty
final List<Long> timestamps;
JsonReceiptMessage(SignalServiceReceiptMessage receiptMessage) { JsonReceiptMessage(SignalServiceReceiptMessage receiptMessage) {
this.when = receiptMessage.getWhen(); this.when = receiptMessage.getWhen();
if (receiptMessage.isDeliveryReceipt()) { this.isDelivery = receiptMessage.isDeliveryReceipt();
this.isDelivery = true; this.isRead = receiptMessage.isReadReceipt();
}
if (receiptMessage.isReadReceipt()) {
this.isRead = true;
}
this.timestamps = receiptMessage.getTimestamps(); this.timestamps = receiptMessage.getTimestamps();
} }

View file

@ -1,19 +1,25 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import java.util.Base64; import java.util.Base64;
public class JsonSticker { public class JsonSticker {
String packId; @JsonProperty
String packKey; final String packId;
int stickerId;
@JsonProperty
final String packKey;
@JsonProperty
final int stickerId;
public JsonSticker(SignalServiceDataMessage.Sticker sticker) { public JsonSticker(SignalServiceDataMessage.Sticker sticker) {
this.packId = Base64.getEncoder().encodeToString(sticker.getPackId()); this.packId = Base64.getEncoder().encodeToString(sticker.getPackId());
this.packKey = Base64.getEncoder().encodeToString(sticker.getPackKey()); this.packKey = Base64.getEncoder().encodeToString(sticker.getPackKey());
this.stickerId = sticker.getStickerId(); this.stickerId = sticker.getStickerId();
// TODO also download sticker image ??
} }
} }

View file

@ -1,18 +1,23 @@
package org.asamk.signal.json; package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage; import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
class JsonSyncDataMessage extends JsonDataMessage { class JsonSyncDataMessage extends JsonDataMessage {
String destination; @JsonProperty
final String destination;
JsonSyncDataMessage(SentTranscriptMessage transcriptMessage, Manager m) { JsonSyncDataMessage(SentTranscriptMessage transcriptMessage, Manager m) {
super(transcriptMessage.getMessage(), m); super(transcriptMessage.getMessage(), m);
if (transcriptMessage.getDestination().isPresent()) {
this.destination = transcriptMessage.getDestination().get().getLegacyIdentifier(); this.destination = transcriptMessage.getDestination()
} .transform(SignalServiceAddress::getLegacyIdentifier)
.orNull();
} }
JsonSyncDataMessage(Signal.SyncMessageReceived messageReceived) { JsonSyncDataMessage(Signal.SyncMessageReceived messageReceived) {

View file

@ -1,13 +1,16 @@
package org.asamk.signal.json; 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;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.multidevice.ReadMessage;
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage; import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.util.ArrayList; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
enum JsonSyncMessageType { enum JsonSyncMessageType {
CONTACTS_SYNC, CONTACTS_SYNC,
@ -17,23 +20,57 @@ enum JsonSyncMessageType {
class JsonSyncMessage { class JsonSyncMessage {
JsonSyncDataMessage sentMessage; @JsonProperty
List<String> blockedNumbers; @JsonInclude(JsonInclude.Include.NON_NULL)
List<ReadMessage> readMessages; final JsonSyncDataMessage sentMessage;
JsonSyncMessageType 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) { JsonSyncMessage(SignalServiceSyncMessage syncMessage, Manager m) {
if (syncMessage.getSent().isPresent()) { this.sentMessage = syncMessage.getSent().isPresent()
this.sentMessage = new JsonSyncDataMessage(syncMessage.getSent().get(), m); ? new JsonSyncDataMessage(syncMessage.getSent().get(), m)
} : null;
if (syncMessage.getBlockedList().isPresent()) { if (syncMessage.getBlockedList().isPresent()) {
this.blockedNumbers = new ArrayList<>(syncMessage.getBlockedList().get().getAddresses().size()); final Base64.Encoder base64 = Base64.getEncoder();
for (SignalServiceAddress address : syncMessage.getBlockedList().get().getAddresses()) { this.blockedNumbers = syncMessage.getBlockedList()
this.blockedNumbers.add(address.getLegacyIdentifier()); .get()
} .getAddresses()
.stream()
.map(SignalServiceAddress::getLegacyIdentifier)
.collect(Collectors.toList());
this.blockedGroupIds = syncMessage.getBlockedList()
.get()
.getGroupIds()
.stream()
.map(base64::encodeToString)
.collect(Collectors.toList());
} else {
this.blockedNumbers = null;
this.blockedGroupIds = null;
} }
if (syncMessage.getRead().isPresent()) { if (syncMessage.getRead().isPresent()) {
this.readMessages = syncMessage.getRead().get(); this.readMessages = syncMessage.getRead()
.get()
.stream()
.map(message -> new JsonSyncReadMessage(message.getSender().getLegacyIdentifier(),
message.getTimestamp()))
.collect(Collectors.toList());
} else {
this.readMessages = null;
} }
if (syncMessage.getContacts().isPresent()) { if (syncMessage.getContacts().isPresent()) {
@ -42,10 +79,16 @@ class JsonSyncMessage {
this.type = JsonSyncMessageType.GROUPS_SYNC; this.type = JsonSyncMessageType.GROUPS_SYNC;
} else if (syncMessage.getRequest().isPresent()) { } else if (syncMessage.getRequest().isPresent()) {
this.type = JsonSyncMessageType.REQUEST_SYNC; this.type = JsonSyncMessageType.REQUEST_SYNC;
} else {
this.type = null;
} }
} }
JsonSyncMessage(Signal.SyncMessageReceived messageReceived) { JsonSyncMessage(Signal.SyncMessageReceived messageReceived) {
sentMessage = new JsonSyncDataMessage(messageReceived); this.sentMessage = new JsonSyncDataMessage(messageReceived);
this.blockedNumbers = null;
this.blockedGroupIds = null;
this.readMessages = null;
this.type = null;
} }
} }

View file

@ -0,0 +1,17 @@
package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonProperty;
class JsonSyncReadMessage {
@JsonProperty
final String sender;
@JsonProperty
final long timestamp;
public JsonSyncReadMessage(final String sender, final long timestamp) {
this.sender = sender;
this.timestamp = timestamp;
}
}