1 package org
.asamk
.signal
.json
;
3 import com
.fasterxml
.jackson
.annotation
.JsonInclude
;
5 import org
.asamk
.Signal
;
6 import org
.asamk
.signal
.manager
.Manager
;
7 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
10 import java
.util
.stream
.Collectors
;
12 record JsonDataMessage(
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
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
);
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(),
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()
55 .map(mention
-> JsonMention
.from(mention
, m
))
56 .collect(Collectors
.toList());
60 final var remoteDelete
= dataMessage
.getRemoteDelete().isPresent()
61 ? JsonRemoteDelete
.from(dataMessage
.getRemoteDelete().get())
63 final List
<JsonAttachment
> attachments
;
64 if (dataMessage
.getAttachments().isPresent()) {
65 attachments
= dataMessage
.getAttachments()
68 .map(JsonAttachment
::from
)
69 .collect(Collectors
.toList());
71 attachments
= List
.of();
73 final var sticker
= dataMessage
.getSticker().isPresent()
74 ? JsonSticker
.from(dataMessage
.getSticker().get())
77 final List
<JsonSharedContact
> contacts
;
78 if (dataMessage
.getSharedContacts().isPresent()) {
79 contacts
= dataMessage
.getSharedContacts()
82 .map(JsonSharedContact
::from
)
83 .collect(Collectors
.toList());
87 return new JsonDataMessage(timestamp
,
101 static JsonDataMessage
from(Signal
.MessageReceived messageReceived
) {
102 return new JsonDataMessage(messageReceived
.getTimestamp(),
103 messageReceived
.getMessage(),
104 // TODO Replace these with the proper commands
110 messageReceived
.getAttachments().stream().map(JsonAttachment
::from
).collect(Collectors
.toList()),
114 messageReceived
.getGroupId().length
> 0 ? JsonGroupInfo
.from(messageReceived
.getGroupId()) : null);
117 static JsonDataMessage
from(Signal
.SyncMessageReceived messageReceived
) {
118 return new JsonDataMessage(messageReceived
.getTimestamp(),
119 messageReceived
.getMessage(),
120 // TODO Replace these with the proper commands
126 messageReceived
.getAttachments().stream().map(JsonAttachment
::from
).collect(Collectors
.toList()),
130 messageReceived
.getGroupId().length
> 0 ? JsonGroupInfo
.from(messageReceived
.getGroupId()) : null);