]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/json/JsonDataMessage.java
add group info on json message
[signal-cli] / src / main / java / org / asamk / signal / json / JsonDataMessage.java
index 6dbda9783e5b081eac7533e94b6c2b8cc7b5beb2..f24fdbba51c2e33b3bb2f040dcccd79f889213e5 100644 (file)
 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;
-import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
+import org.asamk.signal.manager.api.MessageEnvelope;
 
 import java.util.List;
-import java.util.stream.Collectors;
 
-class JsonDataMessage {
-
-    @JsonProperty
-    final long timestamp;
-
-    @JsonProperty
-    final String message;
-
-    @JsonProperty
-    final Integer expiresInSeconds;
-
-    @JsonProperty
-    @JsonInclude(JsonInclude.Include.NON_NULL)
-    final Boolean viewOnce;
-
-    @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 JsonRemoteDelete remoteDelete;
-
-    @JsonProperty
-    @JsonInclude(JsonInclude.Include.NON_NULL)
-    final List<JsonSharedContact> contacts;
-
-    @JsonProperty
-    @JsonInclude(JsonInclude.Include.NON_NULL)
-    final JsonGroupInfo groupInfo;
-
-    JsonDataMessage(SignalServiceDataMessage dataMessage, Manager m) {
-        this.timestamp = dataMessage.getTimestamp();
-        if (dataMessage.getGroupContext().isPresent()) {
-            final var groupContext = dataMessage.getGroupContext().get();
-            if (groupContext.getGroupV1().isPresent()) {
-                var groupInfo = groupContext.getGroupV1().get();
-                this.groupInfo = new JsonGroupInfo(groupInfo);
-            } else if (groupContext.getGroupV2().isPresent()) {
-                var groupInfo = groupContext.getGroupV2().get();
-                this.groupInfo = new JsonGroupInfo(groupInfo);
-            } else {
-                this.groupInfo = null;
-            }
-        } else {
-            this.groupInfo = null;
-        }
-        this.message = dataMessage.getBody().orNull();
-        this.expiresInSeconds = dataMessage.getExpiresInSeconds();
-        this.viewOnce = dataMessage.isViewOnce();
-        this.reaction = dataMessage.getReaction().isPresent()
-                ? new JsonReaction(dataMessage.getReaction().get(), m)
+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
+) {
+
+    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;
-        this.quote = dataMessage.getQuote().isPresent() ? new JsonQuote(dataMessage.getQuote().get(), m) : null;
-        if (dataMessage.getMentions().isPresent()) {
-            this.mentions = dataMessage.getMentions()
-                    .get()
-                    .stream()
-                    .map(mention -> new JsonMention(mention, m))
-                    .collect(Collectors.toList());
-        } else {
-            this.mentions = List.of();
-        }
-        remoteDelete = dataMessage.getRemoteDelete().isPresent() ? new JsonRemoteDelete(dataMessage.getRemoteDelete()
-                .get()) : null;
-        if (dataMessage.getAttachments().isPresent()) {
-            this.attachments = dataMessage.getAttachments()
-                    .get()
-                    .stream()
-                    .map(JsonAttachment::new)
-                    .collect(Collectors.toList());
-        } else {
-            this.attachments = List.of();
-        }
-        this.sticker = dataMessage.getSticker().isPresent() ? new JsonSticker(dataMessage.getSticker().get()) : null;
-
-        if (dataMessage.getSharedContacts().isPresent()) {
-            this.contacts = dataMessage.getSharedContacts()
-                    .get()
-                    .stream()
-                    .map(JsonSharedContact::new)
-                    .collect(Collectors.toList());
-        } else {
-            this.contacts = List.of();
-        }
-    }
-
-    public JsonDataMessage(Signal.MessageReceived messageReceived) {
-        timestamp = messageReceived.getTimestamp();
-        message = messageReceived.getMessage();
-        groupInfo = messageReceived.getGroupId().length > 0 ? new JsonGroupInfo(messageReceived.getGroupId()) : null;
-        expiresInSeconds = null;
-        viewOnce = null;
-        remoteDelete = null;
-        reaction = null;    // TODO Replace these 5 with the proper commands
-        quote = null;
-        mentions = null;
-        sticker = null;
-        contacts = null;
-        attachments = messageReceived.getAttachments().stream().map(JsonAttachment::new).collect(Collectors.toList());
-    }
-
-    public JsonDataMessage(Signal.SyncMessageReceived messageReceived) {
-        timestamp = messageReceived.getTimestamp();
-        message = messageReceived.getMessage();
-        groupInfo = messageReceived.getGroupId().length > 0 ? new JsonGroupInfo(messageReceived.getGroupId()) : null;
-        expiresInSeconds = null;
-        viewOnce = null;
-        remoteDelete = null;
-        reaction = null;    // TODO Replace these 5 with the proper commands
-        quote = null;
-        mentions = null;
-        sticker = null;
-        contacts = null;
-        attachments = messageReceived.getAttachments().stream().map(JsonAttachment::new).collect(Collectors.toList());
+        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;
+
+        return new JsonDataMessage(timestamp,
+                message,
+                expiresInSeconds,
+                viewOnce,
+                reaction,
+                quote,
+                payment,
+                mentions,
+                previews,
+                attachments,
+                sticker,
+                remoteDelete,
+                contacts,
+                textStyles,
+                groupInfo,
+                storyContext);
     }
 }