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