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
;
37 import java
.io
.IOException
;
38 import java
.util
.List
;
39 import java
.util
.Optional
;
41 import java
.util
.stream
.Collectors
;
43 public record MessageEnvelope(
44 Optional
<RecipientAddress
> sourceAddress
,
47 long serverReceivedTimestamp
,
48 long serverDeliveredTimestamp
,
49 boolean isUnidentifiedSender
,
50 Optional
<Receipt
> receipt
,
51 Optional
<Typing
> typing
,
59 public record Receipt(long when, Type type
, List
<Long
> timestamps
) {
61 static Receipt
from(final SignalServiceReceiptMessage receiptMessage
) {
62 return new Receipt(receiptMessage
.getWhen(),
63 Type
.from(receiptMessage
.getType()),
64 receiptMessage
.getTimestamps());
73 static Type
from(SignalServiceReceiptMessage
.Type type
) {
74 return switch (type
) {
75 case DELIVERY
-> DELIVERY
;
77 case VIEWED
-> VIEWED
;
78 case UNKNOWN
-> UNKNOWN
;
84 public record Typing(long timestamp
, Type type
, Optional
<GroupId
> groupId
) {
86 public static Typing
from(final SignalServiceTypingMessage typingMessage
) {
87 return new Typing(typingMessage
.getTimestamp(),
88 typingMessage
.isTypingStarted() ? Type
.STARTED
: Type
.STOPPED
,
89 typingMessage
.getGroupId().map(GroupId
::unknownVersion
));
100 Optional
<GroupContext
> groupContext
,
101 Optional
<StoryContext
> storyContext
,
102 Optional
<GroupCallUpdate
> groupCallUpdate
,
103 Optional
<String
> body
,
104 int expiresInSeconds
,
105 boolean isExpirationUpdate
,
107 boolean isEndSession
,
108 boolean isProfileKeyUpdate
,
109 boolean hasProfileKey
,
110 Optional
<Reaction
> reaction
,
111 Optional
<Quote
> quote
,
112 Optional
<Payment
> payment
,
113 List
<Attachment
> attachments
,
114 Optional
<Long
> remoteDeleteId
,
115 Optional
<Sticker
> sticker
,
116 List
<SharedContact
> sharedContacts
,
117 List
<Mention
> mentions
,
118 List
<Preview
> previews
,
119 List
<TextStyle
> textStyles
123 final SignalServiceDataMessage dataMessage
,
124 RecipientResolver recipientResolver
,
125 RecipientAddressResolver addressResolver
,
126 final AttachmentFileProvider fileProvider
128 return new Data(dataMessage
.getTimestamp(),
129 dataMessage
.getGroupContext().map(GroupContext
::from
),
130 dataMessage
.getStoryContext()
131 .map((SignalServiceDataMessage
.StoryContext storyContext
) -> StoryContext
.from(storyContext
,
134 dataMessage
.getGroupCallUpdate().map(GroupCallUpdate
::from
),
135 dataMessage
.getBody(),
136 dataMessage
.getExpiresInSeconds(),
137 dataMessage
.isExpirationUpdate(),
138 dataMessage
.isViewOnce(),
139 dataMessage
.isEndSession(),
140 dataMessage
.isProfileKeyUpdate(),
141 dataMessage
.getProfileKey().isPresent(),
142 dataMessage
.getReaction().map(r
-> Reaction
.from(r
, recipientResolver
, addressResolver
)),
143 dataMessage
.getQuote()
144 .filter(q
-> q
.getAuthor() != null && q
.getAuthor().isValid())
145 .map(q
-> Quote
.from(q
, recipientResolver
, addressResolver
, fileProvider
)),
146 dataMessage
.getPayment().map(p
-> p
.getPaymentNotification().isPresent() ? Payment
.from(p
) : null),
147 dataMessage
.getAttachments()
148 .map(a
-> a
.stream().map(as -> Attachment
.from(as, fileProvider
)).toList())
150 dataMessage
.getRemoteDelete().map(SignalServiceDataMessage
.RemoteDelete
::getTargetSentTimestamp
),
151 dataMessage
.getSticker().map(Sticker
::from
),
152 dataMessage
.getSharedContacts()
154 .map(sharedContact
-> SharedContact
.from(sharedContact
, fileProvider
))
157 dataMessage
.getMentions()
158 .map(a
-> a
.stream().map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
)).toList())
160 dataMessage
.getPreviews()
161 .map(a
-> a
.stream().map(preview
-> Preview
.from(preview
, fileProvider
)).toList())
163 dataMessage
.getBodyRanges()
164 .map(a
-> a
.stream().filter(r
-> r
.style
!= null).map(TextStyle
::from
).toList())
168 public record GroupContext(GroupId groupId
, boolean isGroupUpdate
, int revision
) {
170 static GroupContext
from(SignalServiceGroupContext groupContext
) {
171 if (groupContext
.getGroupV1().isPresent()) {
172 return new GroupContext(GroupId
.v1(groupContext
.getGroupV1().get().getGroupId()),
173 groupContext
.getGroupV1Type() == SignalServiceGroup
.Type
.UPDATE
,
175 } else if (groupContext
.getGroupV2().isPresent()) {
176 final var groupV2
= groupContext
.getGroupV2().get();
177 return new GroupContext(GroupUtils
.getGroupIdV2(groupV2
.getMasterKey()),
178 groupV2
.hasSignedGroupChange(),
179 groupV2
.getRevision());
181 throw new RuntimeException("Invalid group context state");
186 public record StoryContext(RecipientAddress author
, long sentTimestamp
) {
188 static StoryContext
from(
189 SignalServiceDataMessage
.StoryContext storyContext
,
190 RecipientResolver recipientResolver
,
191 RecipientAddressResolver addressResolver
193 return new StoryContext(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
194 storyContext
.getAuthorServiceId())).toApiRecipientAddress(), storyContext
.getSentTimestamp());
198 public record GroupCallUpdate(String eraId
) {
200 static GroupCallUpdate
from(SignalServiceDataMessage
.GroupCallUpdate groupCallUpdate
) {
201 return new GroupCallUpdate(groupCallUpdate
.getEraId());
205 public record Reaction(
206 long targetSentTimestamp
, RecipientAddress targetAuthor
, String emoji
, boolean isRemove
209 static Reaction
from(
210 SignalServiceDataMessage
.Reaction reaction
,
211 RecipientResolver recipientResolver
,
212 RecipientAddressResolver addressResolver
214 return new Reaction(reaction
.getTargetSentTimestamp(),
215 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(reaction
.getTargetAuthor()))
216 .toApiRecipientAddress(),
218 reaction
.isRemove());
224 RecipientAddress author
,
225 Optional
<String
> text
,
226 List
<Mention
> mentions
,
227 List
<Attachment
> attachments
,
228 List
<TextStyle
> textStyles
232 SignalServiceDataMessage
.Quote quote
,
233 RecipientResolver recipientResolver
,
234 RecipientAddressResolver addressResolver
,
235 final AttachmentFileProvider fileProvider
237 return new Quote(quote
.getId(),
238 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(quote
.getAuthor()))
239 .toApiRecipientAddress(),
240 Optional
.of(quote
.getText()),
241 quote
.getMentions() == null
243 : quote
.getMentions()
245 .map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
))
247 quote
.getAttachments() == null
249 : quote
.getAttachments().stream().map(a
-> Attachment
.from(a
, fileProvider
)).toList(),
250 quote
.getBodyRanges() == null
252 : quote
.getBodyRanges()
254 .filter(r
-> r
.style
!= null)
255 .map(TextStyle
::from
)
260 public record Payment(String note
, byte[] receipt
) {
262 static Payment
from(SignalServiceDataMessage
.Payment payment
) {
263 return new Payment(payment
.getPaymentNotification().get().getNote(),
264 payment
.getPaymentNotification().get().getReceipt());
268 public record Mention(RecipientAddress recipient
, int start
, int length
) {
271 SignalServiceDataMessage
.Mention mention
,
272 RecipientResolver recipientResolver
,
273 RecipientAddressResolver addressResolver
275 return new Mention(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(mention
.getServiceId()))
276 .toApiRecipientAddress(), mention
.getStart(), mention
.getLength());
280 public record Attachment(
283 Optional
<String
> fileName
,
285 Optional
<Long
> uploadTimestamp
,
287 Optional
<byte[]> preview
,
288 Optional
<Attachment
> thumbnail
,
289 Optional
<String
> caption
,
290 Optional
<Integer
> width
,
291 Optional
<Integer
> height
,
297 static Attachment
from(SignalServiceAttachment signalAttachment
, AttachmentFileProvider fileProvider
) {
298 if (signalAttachment
.isPointer()) {
299 final var a
= signalAttachment
.asPointer();
300 final var attachmentFile
= fileProvider
.getFile(a
);
301 return new Attachment(Optional
.of(attachmentFile
.getName()),
302 Optional
.of(attachmentFile
),
305 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
306 a
.getSize().map(Integer
::longValue
),
309 a
.getCaption().map(c
-> c
.isEmpty() ?
null : c
),
310 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
311 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
316 Attachment attachment
= null;
317 try (final var a
= signalAttachment
.asStream()) {
318 attachment
= new Attachment(Optional
.empty(),
322 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
323 Optional
.of(a
.getLength()),
327 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
328 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
333 } catch (IOException e
) {
339 static Attachment
from(
340 SignalServiceDataMessage
.Quote
.QuotedAttachment a
, final AttachmentFileProvider fileProvider
342 return new Attachment(Optional
.empty(),
344 Optional
.ofNullable(a
.getFileName()),
349 a
.getThumbnail() == null
351 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
361 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
363 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
364 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
365 sticker
.getPackKey(),
366 sticker
.getStickerId());
370 public record SharedContact(
372 Optional
<Avatar
> avatar
,
375 List
<Address
> address
,
376 Optional
<String
> organization
379 static SharedContact
from(
380 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
381 final AttachmentFileProvider fileProvider
383 return new SharedContact(Name
.from(sharedContact
.getName()),
384 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
385 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
386 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
387 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
388 sharedContact
.getOrganization());
392 Optional
<String
> display
,
393 Optional
<String
> given
,
394 Optional
<String
> family
,
395 Optional
<String
> prefix
,
396 Optional
<String
> suffix
,
397 Optional
<String
> middle
400 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
401 return new Name(name
.getDisplay(),
410 public record Avatar(Attachment attachment
, boolean isProfile
) {
413 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
414 final AttachmentFileProvider fileProvider
416 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
421 String value
, Type type
, Optional
<String
> label
424 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
425 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
434 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
435 return switch (type
) {
438 case MOBILE
-> MOBILE
;
439 case CUSTOM
-> CUSTOM
;
446 String value
, Type type
, Optional
<String
> label
449 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
450 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
459 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
460 return switch (type
) {
463 case MOBILE
-> MOBILE
;
464 case CUSTOM
-> CUSTOM
;
470 public record Address(
472 Optional
<String
> label
,
473 Optional
<String
> street
,
474 Optional
<String
> pobox
,
475 Optional
<String
> neighborhood
,
476 Optional
<String
> city
,
477 Optional
<String
> region
,
478 Optional
<String
> postcode
,
479 Optional
<String
> country
482 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
483 return new Address(Address
.Type
.from(address
.getType()),
487 address
.getNeighborhood(),
490 address
.getPostcode(),
491 address
.getCountry());
499 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
500 return switch (type
) {
503 case CUSTOM
-> CUSTOM
;
510 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
513 SignalServicePreview preview
, final AttachmentFileProvider fileProvider
515 return new Preview(preview
.getTitle(),
516 preview
.getDescription(),
519 preview
.getImage().map(as -> Attachment
.from(as, fileProvider
)));
525 public record Edit(long targetSentTimestamp
, Data dataMessage
) {
527 public static Edit
from(
528 final SignalServiceEditMessage editMessage
,
529 RecipientResolver recipientResolver
,
530 RecipientAddressResolver addressResolver
,
531 final AttachmentFileProvider fileProvider
533 return new Edit(editMessage
.getTargetSentTimestamp(),
534 Data
.from(editMessage
.getDataMessage(), recipientResolver
, addressResolver
, fileProvider
));
540 Optional
<Blocked
> blocked
,
543 Optional
<ViewOnceOpen
> viewOnceOpen
,
544 Optional
<Contacts
> contacts
,
545 Optional
<Groups
> groups
,
546 Optional
<MessageRequestResponse
> messageRequestResponse
549 public static Sync
from(
550 final SignalServiceSyncMessage syncMessage
,
551 RecipientResolver recipientResolver
,
552 RecipientAddressResolver addressResolver
,
553 final AttachmentFileProvider fileProvider
555 return new Sync(syncMessage
.getSent()
556 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
557 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
558 syncMessage
.getRead()
559 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
561 syncMessage
.getViewed()
563 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
566 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
567 syncMessage
.getContacts().map(Contacts
::from
),
568 syncMessage
.getGroups().map(Groups
::from
),
569 syncMessage
.getMessageRequestResponse()
570 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
575 long expirationStartTimestamp
,
576 Optional
<RecipientAddress
> destination
,
577 Set
<RecipientAddress
> recipients
,
578 Optional
<Data
> message
,
579 Optional
<Edit
> editMessage
,
580 Optional
<Story
> story
584 SentTranscriptMessage sentMessage
,
585 RecipientResolver recipientResolver
,
586 RecipientAddressResolver addressResolver
,
587 final AttachmentFileProvider fileProvider
589 return new Sent(sentMessage
.getTimestamp(),
590 sentMessage
.getExpirationStartTimestamp(),
591 sentMessage
.getDestination()
592 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
593 .toApiRecipientAddress()),
594 sentMessage
.getRecipients()
596 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
597 .toApiRecipientAddress())
598 .collect(Collectors
.toSet()),
599 sentMessage
.getDataMessage()
600 .map(message
-> Data
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
601 sentMessage
.getEditMessage()
602 .map(message
-> Edit
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
603 sentMessage
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
)));
607 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
610 BlockedListMessage blockedListMessage
,
611 RecipientResolver recipientResolver
,
612 RecipientAddressResolver addressResolver
614 return new Blocked(blockedListMessage
.getAddresses()
616 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
617 .toApiRecipientAddress())
618 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
622 public record Read(RecipientAddress sender
, long timestamp
) {
625 ReadMessage readMessage
,
626 RecipientResolver recipientResolver
,
627 RecipientAddressResolver addressResolver
629 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
630 .toApiRecipientAddress(), readMessage
.getTimestamp());
634 public record Viewed(RecipientAddress sender
, long timestamp
) {
637 ViewedMessage readMessage
,
638 RecipientResolver recipientResolver
,
639 RecipientAddressResolver addressResolver
641 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
642 .toApiRecipientAddress(), readMessage
.getTimestamp());
646 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
648 static ViewOnceOpen
from(
649 ViewOnceOpenMessage readMessage
,
650 RecipientResolver recipientResolver
,
651 RecipientAddressResolver addressResolver
653 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
654 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
658 public record Contacts(boolean isComplete
) {
660 static Contacts
from(ContactsMessage contactsMessage
) {
661 return new Contacts(contactsMessage
.isComplete());
665 public record Groups() {
667 static Groups
from(SignalServiceAttachment groupsMessage
) {
672 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
674 static MessageRequestResponse
from(
675 MessageRequestResponseMessage messageRequestResponse
,
676 RecipientResolver recipientResolver
,
677 RecipientAddressResolver addressResolver
679 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
680 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
681 messageRequestResponse
.getPerson()
682 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
683 .toApiRecipientAddress()));
694 static Type
from(MessageRequestResponseMessage
.Type type
) {
695 return switch (type
) {
696 case UNKNOWN
-> UNKNOWN
;
697 case ACCEPT
-> ACCEPT
;
698 case DELETE
-> DELETE
;
700 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
701 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
709 Optional
<Integer
> destinationDeviceId
,
710 Optional
<GroupId
> groupId
,
711 Optional
<Long
> timestamp
,
712 Optional
<Offer
> offer
,
713 Optional
<Answer
> answer
,
714 Optional
<Hangup
> hangup
,
716 List
<IceUpdate
> iceUpdate
,
717 Optional
<Opaque
> opaque
,
722 public static Call
from(final SignalServiceCallMessage callMessage
) {
723 return new Call(callMessage
.getDestinationDeviceId(),
724 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
725 callMessage
.getTimestamp(),
726 callMessage
.getOfferMessage().map(Offer
::from
),
727 callMessage
.getAnswerMessage().map(Answer
::from
),
728 callMessage
.getHangupMessage().map(Hangup
::from
),
729 callMessage
.getBusyMessage().map(Busy
::from
),
730 callMessage
.getIceUpdateMessages()
731 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
733 callMessage
.getOpaqueMessage().map(Opaque
::from
),
734 callMessage
.isMultiRing(),
735 callMessage
.isUrgent());
738 public record Offer(long id
, String sdp
, Type type
, byte[] opaque
) {
740 static Offer
from(OfferMessage offerMessage
) {
741 return new Offer(offerMessage
.getId(),
742 offerMessage
.getSdp(),
743 Type
.from(offerMessage
.getType()),
744 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
, String sdp
, byte[] opaque
) {
762 static Answer
from(AnswerMessage answerMessage
) {
763 return new Answer(answerMessage
.getId(), answerMessage
.getSdp(), 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
, String sdp
, byte[] opaque
) {
803 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
804 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getSdp(), 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 source
= !envelope
.isUnidentifiedSender() && envelope
.hasSourceServiceId()
908 ? recipientResolver
.resolveRecipient(envelope
.getSourceAddress())
909 : envelope
.isUnidentifiedSender() && content
!= null
910 ? recipientResolver
.resolveRecipient(content
.getSender())
911 : exception
instanceof ProtocolException e
912 ? recipientResolver
.resolveRecipient(e
.getSender())
914 final var sourceDevice
= envelope
.hasSourceDevice()
915 ? envelope
.getSourceDevice()
917 ? content
.getSenderDevice()
918 : exception
instanceof ProtocolException e ? e
.getSenderDevice() : 0;
920 Optional
<Receipt
> receipt
;
921 Optional
<Typing
> typing
;
926 Optional
<Story
> story
;
927 if (content
!= null) {
928 receipt
= content
.getReceiptMessage().map(Receipt
::from
);
929 typing
= content
.getTypingMessage().map(Typing
::from
);
930 data
= content
.getDataMessage()
931 .map(dataMessage
-> Data
.from(dataMessage
, recipientResolver
, addressResolver
, fileProvider
));
932 edit
= content
.getEditMessage().map(s
-> Edit
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
933 sync
= content
.getSyncMessage().map(s
-> Sync
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
934 call
= content
.getCallMessage().map(Call
::from
);
935 story
= content
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
));
937 receipt
= envelope
.isReceipt() ? Optional
.of(new Receipt(envelope
.getServerReceivedTimestamp(),
938 Receipt
.Type
.DELIVERY
,
939 List
.of(envelope
.getTimestamp()))) : Optional
.empty();
940 typing
= Optional
.empty();
941 data
= Optional
.empty();
942 edit
= Optional
.empty();
943 sync
= Optional
.empty();
944 call
= Optional
.empty();
945 story
= Optional
.empty();
948 return new MessageEnvelope(source
== null
950 : Optional
.of(addressResolver
.resolveRecipientAddress(source
).toApiRecipientAddress()),
952 envelope
.getTimestamp(),
953 envelope
.getServerReceivedTimestamp(),
954 envelope
.getServerDeliveredTimestamp(),
955 envelope
.isUnidentifiedSender(),
965 public interface AttachmentFileProvider
{
967 File
getFile(SignalServiceAttachmentPointer pointer
);