]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/json/JsonQuote.java
Use record classes
[signal-cli] / src / main / java / org / asamk / signal / json / JsonQuote.java
index 10cd0bf4c31d3d7d50446364d6447086227a7bd8..f0d1831c5b9a7e0a6a78573e39356f5fc2ed32c1 100644 (file)
@@ -1,5 +1,7 @@
 package org.asamk.signal.json;
 
+import com.fasterxml.jackson.annotation.JsonInclude;
+
 import org.asamk.signal.manager.Manager;
 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
 
@@ -7,34 +9,43 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 
-public class JsonQuote {
-
-    long id;
-    String author;
-    String text;
-
-    List<JsonMention> mentions;
-    List<JsonQuotedAttachment> attachments;
-
-    JsonQuote(SignalServiceDataMessage.Quote quote, Manager m) {
-        this.id = quote.getId();
-        this.author = m.resolveSignalServiceAddress(quote.getAuthor()).getLegacyIdentifier();
-        this.text = quote.getText();
-
+import static org.asamk.signal.util.Util.getLegacyIdentifier;
+
+public record JsonQuote(
+        long id,
+        @Deprecated String author,
+        String authorNumber,
+        String authorUuid,
+        String text,
+        @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonMention> mentions,
+        List<JsonQuotedAttachment> attachments
+) {
+
+    static JsonQuote from(SignalServiceDataMessage.Quote quote, Manager m) {
+        final var id = quote.getId();
+        final var address = m.resolveSignalServiceAddress(quote.getAuthor());
+        final var author = getLegacyIdentifier(address);
+        final var authorNumber = address.getNumber().orNull();
+        final var authorUuid = address.getUuid().toString();
+        final var text = quote.getText();
+
+        final List<JsonMention> mentions;
         if (quote.getMentions() != null && quote.getMentions().size() > 0) {
-            this.mentions = quote.getMentions()
+            mentions = quote.getMentions()
                     .stream()
-                    .map(quotedMention -> new JsonMention(quotedMention, m))
+                    .map(quotedMention -> JsonMention.from(quotedMention, m))
                     .collect(Collectors.toList());
+        } else {
+            mentions = null;
         }
 
+        final List<JsonQuotedAttachment> attachments;
         if (quote.getAttachments().size() > 0) {
-            this.attachments = quote.getAttachments()
-                    .stream()
-                    .map(JsonQuotedAttachment::new)
-                    .collect(Collectors.toList());
+            attachments = quote.getAttachments().stream().map(JsonQuotedAttachment::from).collect(Collectors.toList());
         } else {
-            this.attachments = new ArrayList<>();
+            attachments = new ArrayList<>();
         }
+
+        return new JsonQuote(id, author, authorNumber, authorUuid, text, mentions, attachments);
     }
 }