]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/json/JsonStoryMessage.java
Small improvements
[signal-cli] / src / main / java / org / asamk / signal / json / JsonStoryMessage.java
1 package org.asamk.signal.json;
2
3 import com.fasterxml.jackson.annotation.JsonInclude;
4
5 import org.asamk.signal.manager.api.Color;
6 import org.asamk.signal.manager.api.MessageEnvelope;
7 import org.asamk.signal.manager.groups.GroupId;
8
9 import java.util.List;
10
11 record JsonStoryMessage(
12 boolean allowsReplies,
13 @JsonInclude(JsonInclude.Include.NON_NULL) String groupId,
14 @JsonInclude(JsonInclude.Include.NON_NULL) JsonAttachment fileAttachment,
15 @JsonInclude(JsonInclude.Include.NON_NULL) TextAttachment textAttachment
16 ) {
17
18 static JsonStoryMessage from(MessageEnvelope.Story storyMessage) {
19 return new JsonStoryMessage(storyMessage.allowsReplies(),
20 storyMessage.groupId().map(GroupId::toBase64).orElse(null),
21 storyMessage.fileAttachment().map(JsonAttachment::from).orElse(null),
22 storyMessage.textAttachment().map(TextAttachment::from).orElse(null));
23 }
24
25 public record TextAttachment(
26 String text,
27 @JsonInclude(JsonInclude.Include.NON_NULL) String style,
28 @JsonInclude(JsonInclude.Include.NON_NULL) String textForegroundColor,
29 @JsonInclude(JsonInclude.Include.NON_NULL) String textBackgroundColor,
30 @JsonInclude(JsonInclude.Include.NON_NULL) JsonPreview preview,
31 @JsonInclude(JsonInclude.Include.NON_NULL) Gradient backgroundGradient,
32 @JsonInclude(JsonInclude.Include.NON_NULL) String backgroundColor
33 ) {
34
35 static TextAttachment from(MessageEnvelope.Story.TextAttachment textAttachment) {
36 return new TextAttachment(textAttachment.text().orElse(null),
37 textAttachment.style().map(MessageEnvelope.Story.TextAttachment.Style::name).orElse(null),
38 textAttachment.textForegroundColor().map(Color::toHexColor).orElse(null),
39 textAttachment.textBackgroundColor().map(Color::toHexColor).orElse(null),
40 textAttachment.preview().map(JsonPreview::from).orElse(null),
41 textAttachment.backgroundGradient().map(Gradient::from).orElse(null),
42 textAttachment.backgroundColor().map(Color::toHexColor).orElse(null));
43 }
44
45 public record Gradient(
46 String startColor, String endColor, List<String> colors, List<Float> positions, Integer angle
47 ) {
48
49 static Gradient from(MessageEnvelope.Story.TextAttachment.Gradient gradient) {
50 final var isLegacyGradient = gradient.colors().size() == 2
51 && gradient.positions().size() == 2
52 && gradient.positions().get(0) == 0f
53 && gradient.positions().get(1) == 1f;
54
55 return new Gradient(isLegacyGradient ? gradient.colors().get(0).toHexColor() : null,
56 isLegacyGradient ? gradient.colors().get(1).toHexColor() : null,
57 gradient.colors().stream().map(Color::toHexColor).toList(),
58 gradient.positions(),
59 gradient.angle().orElse(null));
60 }
61 }
62 }
63 }