]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/json/JsonDataMessage.java
add JsonPayment (#808)
[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.api.MessageEnvelope;
7
8 import java.util.List;
9 import java.util.stream.Collectors;
10
11 record JsonDataMessage(
12 long timestamp,
13 String message,
14 Integer expiresInSeconds,
15 @JsonInclude(JsonInclude.Include.NON_NULL) Boolean viewOnce,
16 @JsonInclude(JsonInclude.Include.NON_NULL) JsonReaction reaction,
17 @JsonInclude(JsonInclude.Include.NON_NULL) JsonQuote quote,
18 @JsonInclude(JsonInclude.Include.NON_NULL) JsonPayment payment,
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(MessageEnvelope.Data dataMessage) {
28 final var timestamp = dataMessage.timestamp();
29 final var groupInfo = dataMessage.groupContext().isPresent() ? JsonGroupInfo.from(dataMessage.groupContext()
30 .get()) : null;
31 final var message = dataMessage.body().orElse(null);
32 final var expiresInSeconds = dataMessage.expiresInSeconds();
33 final var viewOnce = dataMessage.isViewOnce();
34 final var reaction = dataMessage.reaction().map(JsonReaction::from).orElse(null);
35 final var quote = dataMessage.quote().isPresent() ? JsonQuote.from(dataMessage.quote().get()) : null;
36 final var payment = dataMessage.payment().isPresent() ? JsonPayment.from(dataMessage.payment().get()) : null;
37 final var mentions = dataMessage.mentions().size() > 0 ? dataMessage.mentions()
38 .stream()
39 .map(JsonMention::from)
40 .collect(Collectors.toList()) : null;
41 final var remoteDelete = dataMessage.remoteDeleteId().isPresent()
42 ? new JsonRemoteDelete(dataMessage.remoteDeleteId().get())
43 : null;
44 final var attachments = dataMessage.attachments().size() > 0 ? dataMessage.attachments()
45 .stream()
46 .map(JsonAttachment::from)
47 .collect(Collectors.toList()) : null;
48 final var sticker = dataMessage.sticker().isPresent() ? JsonSticker.from(dataMessage.sticker().get()) : null;
49
50 final var contacts = dataMessage.sharedContacts().size() > 0 ? dataMessage.sharedContacts()
51 .stream()
52 .map(JsonSharedContact::from)
53 .collect(Collectors.toList()) : null;
54 return new JsonDataMessage(timestamp,
55 message,
56 expiresInSeconds,
57 viewOnce,
58 reaction,
59 quote,
60 payment,
61 mentions,
62 attachments,
63 sticker,
64 remoteDelete,
65 contacts,
66 groupInfo);
67 }
68
69 static JsonDataMessage from(Signal.MessageReceived messageReceived) {
70 return new JsonDataMessage(messageReceived.getTimestamp(),
71 messageReceived.getMessage(),
72 // TODO Replace these with the proper commands
73 null,
74 null,
75 null,
76 null,
77 null,
78 null,
79 messageReceived.getAttachments().stream().map(JsonAttachment::from).collect(Collectors.toList()),
80 null,
81 null,
82 null,
83 messageReceived.getGroupId().length > 0 ? JsonGroupInfo.from(messageReceived.getGroupId()) : null);
84 }
85
86 static JsonDataMessage from(Signal.SyncMessageReceived messageReceived) {
87 return new JsonDataMessage(messageReceived.getTimestamp(),
88 messageReceived.getMessage(),
89 // TODO Replace these with the proper commands
90 null,
91 null,
92 null,
93 null,
94 null,
95 null,
96 messageReceived.getAttachments().stream().map(JsonAttachment::from).collect(Collectors.toList()),
97 null,
98 null,
99 null,
100 messageReceived.getGroupId().length > 0 ? JsonGroupInfo.from(messageReceived.getGroupId()) : null);
101 }
102 }