1 package org
.asamk
.signal
.manager
.api
;
3 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
4 import org
.asamk
.signal
.manager
.helper
.RecipientAddressResolver
;
5 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
6 import org
.signal
.libsignal
.metadata
.ProtocolException
;
7 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
8 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
9 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
10 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
11 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEditMessage
;
12 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
13 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupContext
;
15 import org
.whispersystems
.signalservice
.api
.messages
.SignalServicePreview
;
16 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
17 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceStoryMessage
;
18 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTextAttachment
;
19 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTypingMessage
;
20 import org
.whispersystems
.signalservice
.api
.messages
.calls
.AnswerMessage
;
21 import org
.whispersystems
.signalservice
.api
.messages
.calls
.BusyMessage
;
22 import org
.whispersystems
.signalservice
.api
.messages
.calls
.HangupMessage
;
23 import org
.whispersystems
.signalservice
.api
.messages
.calls
.IceUpdateMessage
;
24 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OfferMessage
;
25 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OpaqueMessage
;
26 import org
.whispersystems
.signalservice
.api
.messages
.calls
.SignalServiceCallMessage
;
27 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
28 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
29 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.MessageRequestResponseMessage
;
30 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ReadMessage
;
31 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewOnceOpenMessage
;
34 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewedMessage
;
35 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
38 import java
.io
.IOException
;
39 import java
.util
.List
;
40 import java
.util
.Optional
;
42 import java
.util
.stream
.Collectors
;
44 public record MessageEnvelope(
45 Optional
<RecipientAddress
> sourceAddress
,
48 long serverReceivedTimestamp
,
49 long serverDeliveredTimestamp
,
50 boolean isUnidentifiedSender
,
51 Optional
<Receipt
> receipt
,
52 Optional
<Typing
> typing
,
60 public record Receipt(long when, Type type
, List
<Long
> timestamps
) {
62 static Receipt
from(final SignalServiceReceiptMessage receiptMessage
) {
63 return new Receipt(receiptMessage
.getWhen(),
64 Type
.from(receiptMessage
.getType()),
65 receiptMessage
.getTimestamps());
74 static Type
from(SignalServiceReceiptMessage
.Type type
) {
75 return switch (type
) {
76 case DELIVERY
-> DELIVERY
;
78 case VIEWED
-> VIEWED
;
79 case UNKNOWN
-> UNKNOWN
;
85 public record Typing(long timestamp
, Type type
, Optional
<GroupId
> groupId
) {
87 public static Typing
from(final SignalServiceTypingMessage typingMessage
) {
88 return new Typing(typingMessage
.getTimestamp(),
89 typingMessage
.isTypingStarted() ? Type
.STARTED
: Type
.STOPPED
,
90 typingMessage
.getGroupId().map(GroupId
::unknownVersion
));
101 Optional
<GroupContext
> groupContext
,
102 Optional
<StoryContext
> storyContext
,
103 Optional
<GroupCallUpdate
> groupCallUpdate
,
104 Optional
<String
> body
,
105 int expiresInSeconds
,
106 boolean isExpirationUpdate
,
108 boolean isEndSession
,
109 boolean isProfileKeyUpdate
,
110 boolean hasProfileKey
,
111 Optional
<Reaction
> reaction
,
112 Optional
<Quote
> quote
,
113 Optional
<Payment
> payment
,
114 List
<Attachment
> attachments
,
115 Optional
<Long
> remoteDeleteId
,
116 Optional
<Sticker
> sticker
,
117 List
<SharedContact
> sharedContacts
,
118 List
<Mention
> mentions
,
119 List
<Preview
> previews
,
120 List
<TextStyle
> textStyles
124 final SignalServiceDataMessage dataMessage
,
125 RecipientResolver recipientResolver
,
126 RecipientAddressResolver addressResolver
,
127 final AttachmentFileProvider fileProvider
129 return new Data(dataMessage
.getTimestamp(),
130 dataMessage
.getGroupContext().map(GroupContext
::from
),
131 dataMessage
.getStoryContext()
132 .map((SignalServiceDataMessage
.StoryContext storyContext
) -> StoryContext
.from(storyContext
,
135 dataMessage
.getGroupCallUpdate().map(GroupCallUpdate
::from
),
136 dataMessage
.getBody(),
137 dataMessage
.getExpiresInSeconds(),
138 dataMessage
.isExpirationUpdate(),
139 dataMessage
.isViewOnce(),
140 dataMessage
.isEndSession(),
141 dataMessage
.isProfileKeyUpdate(),
142 dataMessage
.getProfileKey().isPresent(),
143 dataMessage
.getReaction().map(r
-> Reaction
.from(r
, recipientResolver
, addressResolver
)),
144 dataMessage
.getQuote()
145 .filter(q
-> q
.getAuthor() != null && q
.getAuthor().isValid())
146 .map(q
-> Quote
.from(q
, recipientResolver
, addressResolver
, fileProvider
)),
147 dataMessage
.getPayment().map(p
-> p
.getPaymentNotification().isPresent() ? Payment
.from(p
) : null),
148 dataMessage
.getAttachments()
149 .map(a
-> a
.stream().map(as -> Attachment
.from(as, fileProvider
)).toList())
151 dataMessage
.getRemoteDelete().map(SignalServiceDataMessage
.RemoteDelete
::getTargetSentTimestamp
),
152 dataMessage
.getSticker().map(Sticker
::from
),
153 dataMessage
.getSharedContacts()
155 .map(sharedContact
-> SharedContact
.from(sharedContact
, fileProvider
))
158 dataMessage
.getMentions()
159 .map(a
-> a
.stream().map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
)).toList())
161 dataMessage
.getPreviews()
162 .map(a
-> a
.stream().map(preview
-> Preview
.from(preview
, fileProvider
)).toList())
164 dataMessage
.getBodyRanges()
165 .map(a
-> a
.stream().filter(r
-> r
.style
!= null).map(TextStyle
::from
).toList())
169 public record GroupContext(GroupId groupId
, boolean isGroupUpdate
, int revision
) {
171 static GroupContext
from(SignalServiceGroupContext groupContext
) {
172 if (groupContext
.getGroupV1().isPresent()) {
173 return new GroupContext(GroupId
.v1(groupContext
.getGroupV1().get().getGroupId()),
174 groupContext
.getGroupV1Type() == SignalServiceGroup
.Type
.UPDATE
,
176 } else if (groupContext
.getGroupV2().isPresent()) {
177 final var groupV2
= groupContext
.getGroupV2().get();
178 return new GroupContext(GroupUtils
.getGroupIdV2(groupV2
.getMasterKey()),
179 groupV2
.hasSignedGroupChange(),
180 groupV2
.getRevision());
182 throw new RuntimeException("Invalid group context state");
187 public record StoryContext(RecipientAddress author
, long sentTimestamp
) {
189 static StoryContext
from(
190 SignalServiceDataMessage
.StoryContext storyContext
,
191 RecipientResolver recipientResolver
,
192 RecipientAddressResolver addressResolver
194 return new StoryContext(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
195 storyContext
.getAuthorServiceId())).toApiRecipientAddress(), storyContext
.getSentTimestamp());
199 public record GroupCallUpdate(String eraId
) {
201 static GroupCallUpdate
from(SignalServiceDataMessage
.GroupCallUpdate groupCallUpdate
) {
202 return new GroupCallUpdate(groupCallUpdate
.getEraId());
206 public record Reaction(
207 long targetSentTimestamp
, RecipientAddress targetAuthor
, String emoji
, boolean isRemove
210 static Reaction
from(
211 SignalServiceDataMessage
.Reaction reaction
,
212 RecipientResolver recipientResolver
,
213 RecipientAddressResolver addressResolver
215 return new Reaction(reaction
.getTargetSentTimestamp(),
216 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(reaction
.getTargetAuthor()))
217 .toApiRecipientAddress(),
219 reaction
.isRemove());
225 RecipientAddress author
,
226 Optional
<String
> text
,
227 List
<Mention
> mentions
,
228 List
<Attachment
> attachments
,
229 List
<TextStyle
> textStyles
233 SignalServiceDataMessage
.Quote quote
,
234 RecipientResolver recipientResolver
,
235 RecipientAddressResolver addressResolver
,
236 final AttachmentFileProvider fileProvider
238 return new Quote(quote
.getId(),
239 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(quote
.getAuthor()))
240 .toApiRecipientAddress(),
241 Optional
.of(quote
.getText()),
242 quote
.getMentions() == null
244 : quote
.getMentions()
246 .map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
))
248 quote
.getAttachments() == null
250 : quote
.getAttachments().stream().map(a
-> Attachment
.from(a
, fileProvider
)).toList(),
251 quote
.getBodyRanges() == null
253 : quote
.getBodyRanges()
255 .filter(r
-> r
.style
!= null)
256 .map(TextStyle
::from
)
261 public record Payment(String note
, byte[] receipt
) {
263 static Payment
from(SignalServiceDataMessage
.Payment payment
) {
264 return new Payment(payment
.getPaymentNotification().get().getNote(),
265 payment
.getPaymentNotification().get().getReceipt());
269 public record Mention(RecipientAddress recipient
, int start
, int length
) {
272 SignalServiceDataMessage
.Mention mention
,
273 RecipientResolver recipientResolver
,
274 RecipientAddressResolver addressResolver
276 return new Mention(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(mention
.getServiceId()))
277 .toApiRecipientAddress(), mention
.getStart(), mention
.getLength());
281 public record Attachment(
284 Optional
<String
> fileName
,
286 Optional
<Long
> uploadTimestamp
,
288 Optional
<byte[]> preview
,
289 Optional
<Attachment
> thumbnail
,
290 Optional
<String
> caption
,
291 Optional
<Integer
> width
,
292 Optional
<Integer
> height
,
298 static Attachment
from(SignalServiceAttachment signalAttachment
, AttachmentFileProvider fileProvider
) {
299 if (signalAttachment
.isPointer()) {
300 final var a
= signalAttachment
.asPointer();
301 final var attachmentFile
= fileProvider
.getFile(a
);
302 return new Attachment(Optional
.of(attachmentFile
.getName()),
303 Optional
.of(attachmentFile
),
306 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
307 a
.getSize().map(Integer
::longValue
),
310 a
.getCaption().map(c
-> c
.isEmpty() ?
null : c
),
311 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
312 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
317 Attachment attachment
= null;
318 try (final var a
= signalAttachment
.asStream()) {
319 attachment
= new Attachment(Optional
.empty(),
323 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
324 Optional
.of(a
.getLength()),
328 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
329 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
334 } catch (IOException e
) {
340 static Attachment
from(
341 SignalServiceDataMessage
.Quote
.QuotedAttachment a
, final AttachmentFileProvider fileProvider
343 return new Attachment(Optional
.empty(),
345 Optional
.ofNullable(a
.getFileName()),
350 a
.getThumbnail() == null
352 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
362 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
364 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
365 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
366 sticker
.getPackKey(),
367 sticker
.getStickerId());
371 public record SharedContact(
373 Optional
<Avatar
> avatar
,
376 List
<Address
> address
,
377 Optional
<String
> organization
380 static SharedContact
from(
381 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
382 final AttachmentFileProvider fileProvider
384 return new SharedContact(Name
.from(sharedContact
.getName()),
385 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
386 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
387 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
388 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
389 sharedContact
.getOrganization());
393 Optional
<String
> display
,
394 Optional
<String
> given
,
395 Optional
<String
> family
,
396 Optional
<String
> prefix
,
397 Optional
<String
> suffix
,
398 Optional
<String
> middle
401 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
402 return new Name(name
.getDisplay(),
411 public record Avatar(Attachment attachment
, boolean isProfile
) {
414 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
415 final AttachmentFileProvider fileProvider
417 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
422 String value
, Type type
, Optional
<String
> label
425 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
426 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
435 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
436 return switch (type
) {
439 case MOBILE
-> MOBILE
;
440 case CUSTOM
-> CUSTOM
;
447 String value
, Type type
, Optional
<String
> label
450 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
451 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
460 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
461 return switch (type
) {
464 case MOBILE
-> MOBILE
;
465 case CUSTOM
-> CUSTOM
;
471 public record Address(
473 Optional
<String
> label
,
474 Optional
<String
> street
,
475 Optional
<String
> pobox
,
476 Optional
<String
> neighborhood
,
477 Optional
<String
> city
,
478 Optional
<String
> region
,
479 Optional
<String
> postcode
,
480 Optional
<String
> country
483 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
484 return new Address(Type
.from(address
.getType()),
488 address
.getNeighborhood(),
491 address
.getPostcode(),
492 address
.getCountry());
500 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
501 return switch (type
) {
504 case CUSTOM
-> CUSTOM
;
511 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
514 SignalServicePreview preview
, final AttachmentFileProvider fileProvider
516 return new Preview(preview
.getTitle(),
517 preview
.getDescription(),
520 preview
.getImage().map(as -> Attachment
.from(as, fileProvider
)));
526 public record Edit(long targetSentTimestamp
, Data dataMessage
) {
528 public static Edit
from(
529 final SignalServiceEditMessage editMessage
,
530 RecipientResolver recipientResolver
,
531 RecipientAddressResolver addressResolver
,
532 final AttachmentFileProvider fileProvider
534 return new Edit(editMessage
.getTargetSentTimestamp(),
535 Data
.from(editMessage
.getDataMessage(), recipientResolver
, addressResolver
, fileProvider
));
541 Optional
<Blocked
> blocked
,
544 Optional
<ViewOnceOpen
> viewOnceOpen
,
545 Optional
<Contacts
> contacts
,
546 Optional
<Groups
> groups
,
547 Optional
<MessageRequestResponse
> messageRequestResponse
550 public static Sync
from(
551 final SignalServiceSyncMessage syncMessage
,
552 RecipientResolver recipientResolver
,
553 RecipientAddressResolver addressResolver
,
554 final AttachmentFileProvider fileProvider
556 return new Sync(syncMessage
.getSent()
557 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
558 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
559 syncMessage
.getRead()
560 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
562 syncMessage
.getViewed()
564 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
567 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
568 syncMessage
.getContacts().map(Contacts
::from
),
569 syncMessage
.getGroups().map(Groups
::from
),
570 syncMessage
.getMessageRequestResponse()
571 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
576 long expirationStartTimestamp
,
577 Optional
<RecipientAddress
> destination
,
578 Set
<RecipientAddress
> recipients
,
579 Optional
<Data
> message
,
580 Optional
<Edit
> editMessage
,
581 Optional
<Story
> story
585 SentTranscriptMessage sentMessage
,
586 RecipientResolver recipientResolver
,
587 RecipientAddressResolver addressResolver
,
588 final AttachmentFileProvider fileProvider
590 return new Sent(sentMessage
.getTimestamp(),
591 sentMessage
.getExpirationStartTimestamp(),
592 sentMessage
.getDestination()
593 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
594 .toApiRecipientAddress()),
595 sentMessage
.getRecipients()
597 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
598 .toApiRecipientAddress())
599 .collect(Collectors
.toSet()),
600 sentMessage
.getDataMessage()
601 .map(message
-> Data
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
602 sentMessage
.getEditMessage()
603 .map(message
-> Edit
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
604 sentMessage
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
)));
608 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
611 BlockedListMessage blockedListMessage
,
612 RecipientResolver recipientResolver
,
613 RecipientAddressResolver addressResolver
615 return new Blocked(blockedListMessage
.getAddresses()
617 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
618 .toApiRecipientAddress())
619 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
623 public record Read(RecipientAddress sender
, long timestamp
) {
626 ReadMessage readMessage
,
627 RecipientResolver recipientResolver
,
628 RecipientAddressResolver addressResolver
630 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
631 .toApiRecipientAddress(), readMessage
.getTimestamp());
635 public record Viewed(RecipientAddress sender
, long timestamp
) {
638 ViewedMessage readMessage
,
639 RecipientResolver recipientResolver
,
640 RecipientAddressResolver addressResolver
642 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
643 .toApiRecipientAddress(), readMessage
.getTimestamp());
647 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
649 static ViewOnceOpen
from(
650 ViewOnceOpenMessage readMessage
,
651 RecipientResolver recipientResolver
,
652 RecipientAddressResolver addressResolver
654 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
655 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
659 public record Contacts(boolean isComplete
) {
661 static Contacts
from(ContactsMessage contactsMessage
) {
662 return new Contacts(contactsMessage
.isComplete());
666 public record Groups() {
668 static Groups
from(SignalServiceAttachment groupsMessage
) {
673 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
675 static MessageRequestResponse
from(
676 MessageRequestResponseMessage messageRequestResponse
,
677 RecipientResolver recipientResolver
,
678 RecipientAddressResolver addressResolver
680 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
681 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
682 messageRequestResponse
.getPerson()
683 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
684 .toApiRecipientAddress()));
697 static Type
from(MessageRequestResponseMessage
.Type type
) {
698 return switch (type
) {
699 case UNKNOWN
-> UNKNOWN
;
700 case ACCEPT
-> ACCEPT
;
701 case DELETE
-> DELETE
;
703 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
704 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
706 case BLOCK_AND_SPAM
-> BLOCK_AND_SPAM
;
714 Optional
<Integer
> destinationDeviceId
,
715 Optional
<GroupId
> groupId
,
716 Optional
<Long
> timestamp
,
717 Optional
<Offer
> offer
,
718 Optional
<Answer
> answer
,
719 Optional
<Hangup
> hangup
,
721 List
<IceUpdate
> iceUpdate
,
722 Optional
<Opaque
> opaque
,
726 public static Call
from(final SignalServiceCallMessage callMessage
) {
727 return new Call(callMessage
.getDestinationDeviceId(),
728 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
729 callMessage
.getTimestamp(),
730 callMessage
.getOfferMessage().map(Offer
::from
),
731 callMessage
.getAnswerMessage().map(Answer
::from
),
732 callMessage
.getHangupMessage().map(Hangup
::from
),
733 callMessage
.getBusyMessage().map(Busy
::from
),
734 callMessage
.getIceUpdateMessages()
735 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
737 callMessage
.getOpaqueMessage().map(Opaque
::from
),
738 callMessage
.isUrgent());
741 public record Offer(long id
, Type type
, byte[] opaque
) {
743 static Offer
from(OfferMessage offerMessage
) {
744 return new Offer(offerMessage
.getId(), Type
.from(offerMessage
.getType()), offerMessage
.getOpaque());
751 static Type
from(OfferMessage
.Type type
) {
752 return switch (type
) {
753 case AUDIO_CALL
-> AUDIO_CALL
;
754 case VIDEO_CALL
-> VIDEO_CALL
;
760 public record Answer(long id
, byte[] opaque
) {
762 static Answer
from(AnswerMessage answerMessage
) {
763 return new Answer(answerMessage
.getId(), answerMessage
.getOpaque());
767 public record Busy(long id
) {
769 static Busy
from(BusyMessage busyMessage
) {
770 return new Busy(busyMessage
.getId());
774 public record Hangup(long id
, Type type
, int deviceId
) {
776 static Hangup
from(HangupMessage hangupMessage
) {
777 return new Hangup(hangupMessage
.getId(),
778 Type
.from(hangupMessage
.getType()),
779 hangupMessage
.getDeviceId());
789 static Type
from(HangupMessage
.Type type
) {
790 return switch (type
) {
791 case NORMAL
-> NORMAL
;
792 case ACCEPTED
-> ACCEPTED
;
793 case DECLINED
-> DECLINED
;
795 case NEED_PERMISSION
-> NEED_PERMISSION
;
801 public record IceUpdate(long id
, byte[] opaque
) {
803 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
804 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getOpaque());
808 public record Opaque(byte[] opaque
, Urgency urgency
) {
810 static Opaque
from(OpaqueMessage opaqueMessage
) {
811 return new Opaque(opaqueMessage
.getOpaque(), Urgency
.from(opaqueMessage
.getUrgency()));
814 public enum Urgency
{
818 static Urgency
from(OpaqueMessage
.Urgency urgency
) {
819 return switch (urgency
) {
820 case DROPPABLE
-> DROPPABLE
;
821 case HANDLE_IMMEDIATELY
-> HANDLE_IMMEDIATELY
;
829 boolean allowsReplies
,
830 Optional
<GroupId
> groupId
,
831 Optional
<Data
.Attachment
> fileAttachment
,
832 Optional
<TextAttachment
> textAttachment
835 public static Story
from(
836 SignalServiceStoryMessage storyMessage
, final AttachmentFileProvider fileProvider
838 return new Story(storyMessage
.getAllowsReplies().orElse(false),
839 storyMessage
.getGroupContext().map(c
-> GroupUtils
.getGroupIdV2(c
.getMasterKey())),
840 storyMessage
.getFileAttachment().map(f
-> Data
.Attachment
.from(f
, fileProvider
)),
841 storyMessage
.getTextAttachment().map(t
-> TextAttachment
.from(t
, fileProvider
)));
844 public record TextAttachment(
845 Optional
<String
> text
,
846 Optional
<Style
> style
,
847 Optional
<Color
> textForegroundColor
,
848 Optional
<Color
> textBackgroundColor
,
849 Optional
<Data
.Preview
> preview
,
850 Optional
<Gradient
> backgroundGradient
,
851 Optional
<Color
> backgroundColor
854 static TextAttachment
from(
855 SignalServiceTextAttachment textAttachment
, final AttachmentFileProvider fileProvider
857 return new TextAttachment(textAttachment
.getText(),
858 textAttachment
.getStyle().map(Style
::from
),
859 textAttachment
.getTextForegroundColor().map(Color
::new),
860 textAttachment
.getTextBackgroundColor().map(Color
::new),
861 textAttachment
.getPreview().map(p
-> Data
.Preview
.from(p
, fileProvider
)),
862 textAttachment
.getBackgroundGradient().map(Gradient
::from
),
863 textAttachment
.getBackgroundColor().map(Color
::new));
874 static Style
from(SignalServiceTextAttachment
.Style style
) {
875 return switch (style
) {
876 case DEFAULT
-> DEFAULT
;
877 case REGULAR
-> REGULAR
;
880 case SCRIPT
-> SCRIPT
;
881 case CONDENSED
-> CONDENSED
;
886 public record Gradient(
887 List
<Color
> colors
, List
<Float
> positions
, Optional
<Integer
> angle
890 static Gradient
from(SignalServiceTextAttachment
.Gradient gradient
) {
891 return new Gradient(gradient
.getColors().stream().map(Color
::new).toList(),
892 gradient
.getPositions(),
893 gradient
.getAngle());
899 public static MessageEnvelope
from(
900 SignalServiceEnvelope envelope
,
901 SignalServiceContent content
,
902 RecipientResolver recipientResolver
,
903 RecipientAddressResolver addressResolver
,
904 final AttachmentFileProvider fileProvider
,
907 final var serviceId
= envelope
.getSourceServiceId().map(ServiceId
::parseOrNull
).orElse(null);
908 final var source
= !envelope
.isUnidentifiedSender() && serviceId
!= null
909 ? recipientResolver
.resolveRecipient(serviceId
)
910 : envelope
.isUnidentifiedSender() && content
!= null
911 ? recipientResolver
.resolveRecipient(content
.getSender())
912 : exception
instanceof ProtocolException e
913 ? recipientResolver
.resolveRecipient(e
.getSender())
915 final var sourceDevice
= envelope
.hasSourceDevice()
916 ? envelope
.getSourceDevice()
918 ? content
.getSenderDevice()
919 : exception
instanceof ProtocolException e ? e
.getSenderDevice() : 0;
921 Optional
<Receipt
> receipt
;
922 Optional
<Typing
> typing
;
927 Optional
<Story
> story
;
928 if (content
!= null) {
929 receipt
= content
.getReceiptMessage().map(Receipt
::from
);
930 typing
= content
.getTypingMessage().map(Typing
::from
);
931 data
= content
.getDataMessage()
932 .map(dataMessage
-> Data
.from(dataMessage
, recipientResolver
, addressResolver
, fileProvider
));
933 edit
= content
.getEditMessage().map(s
-> Edit
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
934 sync
= content
.getSyncMessage().map(s
-> Sync
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
935 call
= content
.getCallMessage().map(Call
::from
);
936 story
= content
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
));
938 receipt
= envelope
.isReceipt() ? Optional
.of(new Receipt(envelope
.getServerReceivedTimestamp(),
939 Receipt
.Type
.DELIVERY
,
940 List
.of(envelope
.getTimestamp()))) : Optional
.empty();
941 typing
= Optional
.empty();
942 data
= Optional
.empty();
943 edit
= Optional
.empty();
944 sync
= Optional
.empty();
945 call
= Optional
.empty();
946 story
= Optional
.empty();
949 return new MessageEnvelope(source
== null
951 : Optional
.of(addressResolver
.resolveRecipientAddress(source
).toApiRecipientAddress()),
953 envelope
.getTimestamp(),
954 envelope
.getServerReceivedTimestamp(),
955 envelope
.getServerDeliveredTimestamp(),
956 envelope
.isUnidentifiedSender(),
966 public interface AttachmentFileProvider
{
968 File
getFile(SignalServiceAttachmentPointer pointer
);