package org.asamk.signal.json;
-import org.asamk.Signal;
+import com.fasterxml.jackson.annotation.JsonInclude;
+
import org.asamk.signal.manager.Manager;
-import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
-import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
-import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2;
+import org.asamk.signal.manager.api.MessageEnvelope;
import java.util.List;
-import java.util.stream.Collectors;
-
-class JsonDataMessage {
-
- long timestamp;
- String message;
- int expiresInSeconds;
- JsonReaction reaction;
- JsonQuote quote;
- List<JsonMention> mentions;
- List<JsonAttachment> attachments;
- JsonGroupInfo groupInfo;
+record JsonDataMessage(
+ long timestamp,
+ String message,
+ Integer expiresInSeconds,
+ @JsonInclude(JsonInclude.Include.NON_NULL) Boolean viewOnce,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonReaction reaction,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonQuote quote,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonPayment payment,
+ @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonMention> mentions,
+ @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonPreview> previews,
+ @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonAttachment> attachments,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonSticker sticker,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonRemoteDelete remoteDelete,
+ @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonSharedContact> contacts,
+ @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonTextStyle> textStyles,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonGroupInfo groupInfo,
+ @JsonInclude(JsonInclude.Include.NON_NULL) JsonStoryContext storyContext
+) {
- JsonDataMessage(SignalServiceDataMessage dataMessage, Manager m) {
- this.timestamp = dataMessage.getTimestamp();
- if (dataMessage.getGroupContext().isPresent()) {
- if (dataMessage.getGroupContext().get().getGroupV1().isPresent()) {
- SignalServiceGroup groupInfo = dataMessage.getGroupContext().get().getGroupV1().get();
- this.groupInfo = new JsonGroupInfo(groupInfo);
- } else if (dataMessage.getGroupContext().get().getGroupV2().isPresent()) {
- SignalServiceGroupV2 groupInfo = dataMessage.getGroupContext().get().getGroupV2().get();
- this.groupInfo = new JsonGroupInfo(groupInfo);
- }
- }
- if (dataMessage.getBody().isPresent()) {
- this.message = dataMessage.getBody().get();
- }
- this.expiresInSeconds = dataMessage.getExpiresInSeconds();
- if (dataMessage.getReaction().isPresent()) {
- this.reaction = new JsonReaction(dataMessage.getReaction().get(), m);
- }
- if (dataMessage.getQuote().isPresent()) {
- this.quote = new JsonQuote(dataMessage.getQuote().get(), m);
- }
- if (dataMessage.getMentions().isPresent()) {
- this.mentions = dataMessage.getMentions()
- .get()
- .stream()
- .map(mention -> new JsonMention(mention, m))
- .collect(Collectors.toList());
- } else {
- this.mentions = List.of();
- }
- if (dataMessage.getAttachments().isPresent()) {
- this.attachments = dataMessage.getAttachments()
- .get()
- .stream()
- .map(JsonAttachment::new)
- .collect(Collectors.toList());
- } else {
- this.attachments = List.of();
- }
- }
-
- public JsonDataMessage(Signal.MessageReceived messageReceived) {
- timestamp = messageReceived.getTimestamp();
- message = messageReceived.getMessage();
- groupInfo = new JsonGroupInfo(messageReceived.getGroupId());
- reaction = null; // TODO Replace these 3 with the proper commands
- quote = null;
- mentions = null;
- attachments = messageReceived.getAttachments().stream().map(JsonAttachment::new).collect(Collectors.toList());
- }
+ static JsonDataMessage from(MessageEnvelope.Data dataMessage, Manager m) {
+ final var timestamp = dataMessage.timestamp();
+ final var groupInfo = dataMessage.groupContext().isPresent() ? JsonGroupInfo.from(dataMessage.groupContext()
+ .get(), m) : null;
+ final var storyContext = dataMessage.storyContext().isPresent()
+ ? JsonStoryContext.from(dataMessage.storyContext().get())
+ : null;
+ final var message = dataMessage.body().orElse(null);
+ final var expiresInSeconds = dataMessage.expiresInSeconds();
+ final var viewOnce = dataMessage.isViewOnce();
+ final var reaction = dataMessage.reaction().map(JsonReaction::from).orElse(null);
+ final var quote = dataMessage.quote().isPresent() ? JsonQuote.from(dataMessage.quote().get()) : null;
+ final var payment = dataMessage.payment().isPresent() ? JsonPayment.from(dataMessage.payment().get()) : null;
+ final var mentions = !dataMessage.mentions().isEmpty() ? dataMessage.mentions()
+ .stream()
+ .map(JsonMention::from)
+ .toList() : null;
+ final var previews = !dataMessage.previews().isEmpty() ? dataMessage.previews()
+ .stream()
+ .map(JsonPreview::from)
+ .toList() : null;
+ final var remoteDelete = dataMessage.remoteDeleteId().isPresent()
+ ? new JsonRemoteDelete(dataMessage.remoteDeleteId().get())
+ : null;
+ final var attachments = !dataMessage.attachments().isEmpty() ? dataMessage.attachments()
+ .stream()
+ .map(JsonAttachment::from)
+ .toList() : null;
+ final var sticker = dataMessage.sticker().isPresent() ? JsonSticker.from(dataMessage.sticker().get()) : null;
+ final var contacts = !dataMessage.sharedContacts().isEmpty() ? dataMessage.sharedContacts()
+ .stream()
+ .map(JsonSharedContact::from)
+ .toList() : null;
+ final var textStyles = !dataMessage.textStyles().isEmpty() ? dataMessage.textStyles()
+ .stream()
+ .map(JsonTextStyle::from)
+ .toList() : null;
- public JsonDataMessage(Signal.SyncMessageReceived messageReceived) {
- timestamp = messageReceived.getTimestamp();
- message = messageReceived.getMessage();
- groupInfo = new JsonGroupInfo(messageReceived.getGroupId());
- reaction = null; // TODO Replace these 3 with the proper commands
- quote = null;
- mentions = null;
- attachments = messageReceived.getAttachments().stream().map(JsonAttachment::new).collect(Collectors.toList());
+ return new JsonDataMessage(timestamp,
+ message,
+ expiresInSeconds,
+ viewOnce,
+ reaction,
+ quote,
+ payment,
+ mentions,
+ previews,
+ attachments,
+ sticker,
+ remoteDelete,
+ contacts,
+ textStyles,
+ groupInfo,
+ storyContext);
}
}