]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/json/JsonDataMessage.java
Use record classes
[signal-cli] / src / main / java / org / asamk / signal / json / JsonDataMessage.java
1 package org.asamk.signal.json;
2
3 import com.fasterxml.jackson.annotation.JsonInclude;
4
5 import org.asamk.Signal;
6 import org.asamk.signal.manager.Manager;
7 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
8
9 import java.util.List;
10 import java.util.stream.Collectors;
11
12 record JsonDataMessage(
13 long timestamp,
14 String message,
15 Integer expiresInSeconds,
16 @JsonInclude(JsonInclude.Include.NON_NULL) Boolean viewOnce,
17 @JsonInclude(JsonInclude.Include.NON_NULL) JsonReaction reaction,
18 @JsonInclude(JsonInclude.Include.NON_NULL) JsonQuote quote,
19 @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonMention> mentions,
20 @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonAttachment> attachments,
21 @JsonInclude(JsonInclude.Include.NON_NULL) JsonSticker sticker,
22 @JsonInclude(JsonInclude.Include.NON_NULL) JsonRemoteDelete remoteDelete,
23 @JsonInclude(JsonInclude.Include.NON_NULL) List<JsonSharedContact> contacts,
24 @JsonInclude(JsonInclude.Include.NON_NULL) JsonGroupInfo groupInfo
25 ) {
26
27 static JsonDataMessage from(SignalServiceDataMessage dataMessage, Manager m) {
28 final var timestamp = dataMessage.getTimestamp();
29 final JsonGroupInfo groupInfo;
30 if (dataMessage.getGroupContext().isPresent()) {
31 final var groupContext = dataMessage.getGroupContext().get();
32 if (groupContext.getGroupV1().isPresent()) {
33 var group = groupContext.getGroupV1().get();
34 groupInfo = JsonGroupInfo.from(group);
35 } else if (groupContext.getGroupV2().isPresent()) {
36 var group = groupContext.getGroupV2().get();
37 groupInfo = JsonGroupInfo.from(group);
38 } else {
39 groupInfo = null;
40 }
41 } else {
42 groupInfo = null;
43 }
44 final var message = dataMessage.getBody().orNull();
45 final var expiresInSeconds = dataMessage.getExpiresInSeconds();
46 final var viewOnce = dataMessage.isViewOnce();
47 final var reaction = dataMessage.getReaction().isPresent() ? JsonReaction.from(dataMessage.getReaction().get(),
48 m) : null;
49 final var quote = dataMessage.getQuote().isPresent() ? JsonQuote.from(dataMessage.getQuote().get(), m) : null;
50 final List<JsonMention> mentions;
51 if (dataMessage.getMentions().isPresent()) {
52 mentions = dataMessage.getMentions()
53 .get()
54 .stream()
55 .map(mention -> JsonMention.from(mention, m))
56 .collect(Collectors.toList());
57 } else {
58 mentions = List.of();
59 }
60 final var remoteDelete = dataMessage.getRemoteDelete().isPresent()
61 ? JsonRemoteDelete.from(dataMessage.getRemoteDelete().get())
62 : null;
63 final List<JsonAttachment> attachments;
64 if (dataMessage.getAttachments().isPresent()) {
65 attachments = dataMessage.getAttachments()
66 .get()
67 .stream()
68 .map(JsonAttachment::from)
69 .collect(Collectors.toList());
70 } else {
71 attachments = List.of();
72 }
73 final var sticker = dataMessage.getSticker().isPresent()
74 ? JsonSticker.from(dataMessage.getSticker().get())
75 : null;
76
77 final List<JsonSharedContact> contacts;
78 if (dataMessage.getSharedContacts().isPresent()) {
79 contacts = dataMessage.getSharedContacts()
80 .get()
81 .stream()
82 .map(JsonSharedContact::from)
83 .collect(Collectors.toList());
84 } else {
85 contacts = List.of();
86 }
87 return new JsonDataMessage(timestamp,
88 message,
89 expiresInSeconds,
90 viewOnce,
91 reaction,
92 quote,
93 mentions,
94 attachments,
95 sticker,
96 remoteDelete,
97 contacts,
98 groupInfo);
99 }
100
101 static JsonDataMessage from(Signal.MessageReceived messageReceived) {
102 return new JsonDataMessage(messageReceived.getTimestamp(),
103 messageReceived.getMessage(),
104 // TODO Replace these with the proper commands
105 null,
106 null,
107 null,
108 null,
109 null,
110 messageReceived.getAttachments().stream().map(JsonAttachment::from).collect(Collectors.toList()),
111 null,
112 null,
113 null,
114 messageReceived.getGroupId().length > 0 ? JsonGroupInfo.from(messageReceived.getGroupId()) : null);
115 }
116
117 static JsonDataMessage from(Signal.SyncMessageReceived messageReceived) {
118 return new JsonDataMessage(messageReceived.getTimestamp(),
119 messageReceived.getMessage(),
120 // TODO Replace these with the proper commands
121 null,
122 null,
123 null,
124 null,
125 null,
126 messageReceived.getAttachments().stream().map(JsonAttachment::from).collect(Collectors.toList()),
127 null,
128 null,
129 null,
130 messageReceived.getGroupId().length > 0 ? JsonGroupInfo.from(messageReceived.getGroupId()) : null);
131 }
132 }