]> nmode's Git Repositories - signal-cli/commitdiff
Print text styles in plain text output
authorAsamK <asamk@gmx.de>
Fri, 27 Jan 2023 20:46:39 +0000 (21:46 +0100)
committerAsamK <asamk@gmx.de>
Fri, 27 Jan 2023 20:46:39 +0000 (21:46 +0100)
lib/src/main/java/org/asamk/signal/manager/api/MessageEnvelope.java
src/main/java/org/asamk/signal/ReceiveMessageHandler.java
src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java

index 9b6a476e50e16f56ea0837cdf0ef5ef0f3dbca1b..c1726c1af61cef67834c4e4d0947f1fa01a328e1 100644 (file)
@@ -39,6 +39,8 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+import static org.whispersystems.signalservice.internal.push.SignalServiceProtos.BodyRange;
+
 public record MessageEnvelope(
         Optional<RecipientAddress> sourceAddress,
         int sourceDevice,
@@ -113,7 +115,8 @@ public record MessageEnvelope(
             Optional<Sticker> sticker,
             List<SharedContact> sharedContacts,
             List<Mention> mentions,
-            List<Preview> previews
+            List<Preview> previews,
+            List<TextStyle> textStyles
     ) {
 
         static Data from(
@@ -154,6 +157,9 @@ public record MessageEnvelope(
                             .orElse(List.of()),
                     dataMessage.getPreviews()
                             .map(a -> a.stream().map(preview -> Preview.from(preview, fileProvider)).toList())
+                            .orElse(List.of()),
+                    dataMessage.getBodyRanges()
+                            .map(a -> a.stream().filter(BodyRange::hasStyle).map(TextStyle::from).toList())
                             .orElse(List.of()));
         }
 
@@ -216,7 +222,8 @@ public record MessageEnvelope(
                 RecipientAddress author,
                 Optional<String> text,
                 List<Mention> mentions,
-                List<Attachment> attachments
+                List<Attachment> attachments,
+                List<TextStyle> textStyles
         ) {
 
             static Quote from(
@@ -237,7 +244,14 @@ public record MessageEnvelope(
                                         .toList(),
                         quote.getAttachments() == null
                                 ? List.of()
-                                : quote.getAttachments().stream().map(a -> Attachment.from(a, fileProvider)).toList());
+                                : quote.getAttachments().stream().map(a -> Attachment.from(a, fileProvider)).toList(),
+                        quote.getBodyRanges() == null
+                                ? List.of()
+                                : quote.getBodyRanges()
+                                        .stream()
+                                        .filter(BodyRange::hasStyle)
+                                        .map(TextStyle::from)
+                                        .toList());
             }
         }
 
@@ -498,6 +512,33 @@ public record MessageEnvelope(
                         preview.getImage().map(as -> Attachment.from(as, fileProvider)));
             }
         }
+
+        public record TextStyle(Style style, int start, int length) {
+
+            public enum Style {
+                NONE,
+                BOLD,
+                ITALIC,
+                SPOILER,
+                STRIKETHROUGH,
+                MONOSPACE;
+
+                static Style from(BodyRange.Style style) {
+                    return switch (style) {
+                        case NONE -> NONE;
+                        case BOLD -> BOLD;
+                        case ITALIC -> ITALIC;
+                        case SPOILER -> SPOILER;
+                        case STRIKETHROUGH -> STRIKETHROUGH;
+                        case MONOSPACE -> MONOSPACE;
+                    };
+                }
+            }
+
+            static TextStyle from(BodyRange bodyRange) {
+                return new TextStyle(Style.from(bodyRange.getStyle()), bodyRange.getStart(), bodyRange.getLength());
+            }
+        }
     }
 
     public record Sync(
index 87753b056bf779ed255683eb62a7889e396fd065..c52700304250fdaaed5d3e5f00b71da5699daeb4 100644 (file)
@@ -177,6 +177,12 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
                 printMention(writer, mention);
             }
         }
+        if (message.textStyles().size() > 0) {
+            writer.println("Text styles:");
+            for (var textStyle : message.textStyles()) {
+                printTextStyle(writer, textStyle);
+            }
+        }
         if (message.attachments().size() > 0) {
             writer.println("Attachments:");
             for (var attachment : message.attachments()) {
@@ -555,6 +561,12 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
         writer.println("- {}: {} (length: {})", formatContact(mention.recipient()), mention.start(), mention.length());
     }
 
+    private void printTextStyle(
+            PlainTextWriter writer, MessageEnvelope.Data.TextStyle textStyle
+    ) {
+        writer.println("- {}: {} (length: {})", textStyle.style().name(), textStyle.start(), textStyle.length());
+    }
+
     private void printAttachment(PlainTextWriter writer, MessageEnvelope.Data.Attachment attachment) {
         writer.println("Content-Type: {}", attachment.contentType());
         writer.println("Type: {}", attachment.id().isPresent() ? "Pointer" : "Stream");
index d2a081310516e980b0f384d5bf362174bb0d3118..0ccff7351415d8c12825cfd67c380c41d7af4886 100644 (file)
@@ -778,6 +778,7 @@ public class DbusManagerImpl implements Manager {
                                 Optional.empty(),
                                 List.of(),
                                 List.of(),
+                                List.of(),
                                 List.of())),
                         Optional.empty(),
                         Optional.empty(),
@@ -852,6 +853,7 @@ public class DbusManagerImpl implements Manager {
                                         Optional.empty(),
                                         List.of(),
                                         List.of(),
+                                        List.of(),
                                         List.of())),
                                 Optional.empty())),
                                 Optional.empty(),