1 package org
.asamk
.signal
.manager
.api
;
3 import org
.asamk
.signal
.manager
.groups
.GroupId
;
4 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
5 import org
.asamk
.signal
.manager
.helper
.RecipientAddressResolver
;
6 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
7 import org
.signal
.libsignal
.metadata
.ProtocolException
;
8 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
9 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
10 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
11 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
12 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEditMessage
;
13 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
15 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupContext
;
16 import org
.whispersystems
.signalservice
.api
.messages
.SignalServicePreview
;
17 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
18 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceStoryMessage
;
19 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTextAttachment
;
20 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTypingMessage
;
21 import org
.whispersystems
.signalservice
.api
.messages
.calls
.AnswerMessage
;
22 import org
.whispersystems
.signalservice
.api
.messages
.calls
.BusyMessage
;
23 import org
.whispersystems
.signalservice
.api
.messages
.calls
.HangupMessage
;
24 import org
.whispersystems
.signalservice
.api
.messages
.calls
.IceUpdateMessage
;
25 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OfferMessage
;
26 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OpaqueMessage
;
27 import org
.whispersystems
.signalservice
.api
.messages
.calls
.SignalServiceCallMessage
;
28 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
29 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
30 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.MessageRequestResponseMessage
;
31 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ReadMessage
;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
34 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewOnceOpenMessage
;
35 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewedMessage
;
38 import java
.util
.List
;
39 import java
.util
.Optional
;
41 import java
.util
.stream
.Collectors
;
43 import static org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
.BodyRange
;
45 public record MessageEnvelope(
46 Optional
<RecipientAddress
> sourceAddress
,
49 long serverReceivedTimestamp
,
50 long serverDeliveredTimestamp
,
51 boolean isUnidentifiedSender
,
52 Optional
<Receipt
> receipt
,
53 Optional
<Typing
> typing
,
61 public record Receipt(long when, Type type
, List
<Long
> timestamps
) {
63 static Receipt
from(final SignalServiceReceiptMessage receiptMessage
) {
64 return new Receipt(receiptMessage
.getWhen(),
65 Type
.from(receiptMessage
.getType()),
66 receiptMessage
.getTimestamps());
75 static Type
from(SignalServiceReceiptMessage
.Type type
) {
76 return switch (type
) {
77 case DELIVERY
-> DELIVERY
;
79 case VIEWED
-> VIEWED
;
80 case UNKNOWN
-> UNKNOWN
;
86 public record Typing(long timestamp
, Type type
, Optional
<GroupId
> groupId
) {
88 public static Typing
from(final SignalServiceTypingMessage typingMessage
) {
89 return new Typing(typingMessage
.getTimestamp(),
90 typingMessage
.isTypingStarted() ? Type
.STARTED
: Type
.STOPPED
,
91 typingMessage
.getGroupId().map(GroupId
::unknownVersion
));
102 Optional
<GroupContext
> groupContext
,
103 Optional
<StoryContext
> storyContext
,
104 Optional
<GroupCallUpdate
> groupCallUpdate
,
105 Optional
<String
> body
,
106 int expiresInSeconds
,
107 boolean isExpirationUpdate
,
109 boolean isEndSession
,
110 boolean isProfileKeyUpdate
,
111 boolean hasProfileKey
,
112 Optional
<Reaction
> reaction
,
113 Optional
<Quote
> quote
,
114 Optional
<Payment
> payment
,
115 List
<Attachment
> attachments
,
116 Optional
<Long
> remoteDeleteId
,
117 Optional
<Sticker
> sticker
,
118 List
<SharedContact
> sharedContacts
,
119 List
<Mention
> mentions
,
120 List
<Preview
> previews
,
121 List
<TextStyle
> textStyles
125 final SignalServiceDataMessage dataMessage
,
126 RecipientResolver recipientResolver
,
127 RecipientAddressResolver addressResolver
,
128 final AttachmentFileProvider fileProvider
130 return new Data(dataMessage
.getTimestamp(),
131 dataMessage
.getGroupContext().map(GroupContext
::from
),
132 dataMessage
.getStoryContext()
133 .map((SignalServiceDataMessage
.StoryContext storyContext
) -> StoryContext
.from(storyContext
,
136 dataMessage
.getGroupCallUpdate().map(GroupCallUpdate
::from
),
137 dataMessage
.getBody(),
138 dataMessage
.getExpiresInSeconds(),
139 dataMessage
.isExpirationUpdate(),
140 dataMessage
.isViewOnce(),
141 dataMessage
.isEndSession(),
142 dataMessage
.isProfileKeyUpdate(),
143 dataMessage
.getProfileKey().isPresent(),
144 dataMessage
.getReaction().map(r
-> Reaction
.from(r
, recipientResolver
, addressResolver
)),
145 dataMessage
.getQuote().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(BodyRange
::hasStyle
).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
.ofNullable(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(BodyRange
::hasStyle
)
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 attachment
, AttachmentFileProvider fileProvider
) {
298 if (attachment
.isPointer()) {
299 final var a
= attachment
.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 final var a
= attachment
.asStream();
317 return new Attachment(Optional
.empty(),
321 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
322 Optional
.of(a
.getLength()),
326 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
327 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
334 static Attachment
from(
335 SignalServiceDataMessage
.Quote
.QuotedAttachment a
, final AttachmentFileProvider fileProvider
337 return new Attachment(Optional
.empty(),
339 Optional
.ofNullable(a
.getFileName()),
344 a
.getThumbnail() == null
346 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
356 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
358 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
359 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
360 sticker
.getPackKey(),
361 sticker
.getStickerId());
365 public record SharedContact(
367 Optional
<Avatar
> avatar
,
370 List
<Address
> address
,
371 Optional
<String
> organization
374 static SharedContact
from(
375 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
376 final AttachmentFileProvider fileProvider
378 return new SharedContact(Name
.from(sharedContact
.getName()),
379 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
380 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
381 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
382 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
383 sharedContact
.getOrganization());
387 Optional
<String
> display
,
388 Optional
<String
> given
,
389 Optional
<String
> family
,
390 Optional
<String
> prefix
,
391 Optional
<String
> suffix
,
392 Optional
<String
> middle
395 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
396 return new Name(name
.getDisplay(),
405 public record Avatar(Attachment attachment
, boolean isProfile
) {
408 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
409 final AttachmentFileProvider fileProvider
411 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
416 String value
, Type type
, Optional
<String
> label
419 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
420 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
429 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
430 return switch (type
) {
433 case MOBILE
-> MOBILE
;
434 case CUSTOM
-> CUSTOM
;
441 String value
, Type type
, Optional
<String
> label
444 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
445 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
454 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
455 return switch (type
) {
458 case MOBILE
-> MOBILE
;
459 case CUSTOM
-> CUSTOM
;
465 public record Address(
467 Optional
<String
> label
,
468 Optional
<String
> street
,
469 Optional
<String
> pobox
,
470 Optional
<String
> neighborhood
,
471 Optional
<String
> city
,
472 Optional
<String
> region
,
473 Optional
<String
> postcode
,
474 Optional
<String
> country
477 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
478 return new Address(Address
.Type
.from(address
.getType()),
494 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
495 return switch (type
) {
498 case CUSTOM
-> CUSTOM
;
505 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
508 SignalServicePreview preview
, final AttachmentFileProvider fileProvider
510 return new Preview(preview
.getTitle(),
511 preview
.getDescription(),
514 preview
.getImage().map(as -> Attachment
.from(as, fileProvider
)));
520 public record Edit(long targetSentTimestamp
, Data dataMessage
) {
522 public static Edit
from(
523 final SignalServiceEditMessage editMessage
,
524 RecipientResolver recipientResolver
,
525 RecipientAddressResolver addressResolver
,
526 final AttachmentFileProvider fileProvider
528 return new Edit(editMessage
.getTargetSentTimestamp(),
529 Data
.from(editMessage
.getDataMessage(), recipientResolver
, addressResolver
, fileProvider
));
535 Optional
<Blocked
> blocked
,
538 Optional
<ViewOnceOpen
> viewOnceOpen
,
539 Optional
<Contacts
> contacts
,
540 Optional
<Groups
> groups
,
541 Optional
<MessageRequestResponse
> messageRequestResponse
544 public static Sync
from(
545 final SignalServiceSyncMessage syncMessage
,
546 RecipientResolver recipientResolver
,
547 RecipientAddressResolver addressResolver
,
548 final AttachmentFileProvider fileProvider
550 return new Sync(syncMessage
.getSent()
551 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
552 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
553 syncMessage
.getRead()
554 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
556 syncMessage
.getViewed()
558 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
561 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
562 syncMessage
.getContacts().map(Contacts
::from
),
563 syncMessage
.getGroups().map(Groups
::from
),
564 syncMessage
.getMessageRequestResponse()
565 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
570 long expirationStartTimestamp
,
571 Optional
<RecipientAddress
> destination
,
572 Set
<RecipientAddress
> recipients
,
573 Optional
<Data
> message
,
574 Optional
<Edit
> editMessage
,
575 Optional
<Story
> story
579 SentTranscriptMessage sentMessage
,
580 RecipientResolver recipientResolver
,
581 RecipientAddressResolver addressResolver
,
582 final AttachmentFileProvider fileProvider
584 return new Sent(sentMessage
.getTimestamp(),
585 sentMessage
.getExpirationStartTimestamp(),
586 sentMessage
.getDestination()
587 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
588 .toApiRecipientAddress()),
589 sentMessage
.getRecipients()
591 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
592 .toApiRecipientAddress())
593 .collect(Collectors
.toSet()),
594 sentMessage
.getDataMessage()
595 .map(message
-> Data
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
596 sentMessage
.getEditMessage()
597 .map(message
-> Edit
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
598 sentMessage
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
)));
602 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
605 BlockedListMessage blockedListMessage
,
606 RecipientResolver recipientResolver
,
607 RecipientAddressResolver addressResolver
609 return new Blocked(blockedListMessage
.getAddresses()
611 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
612 .toApiRecipientAddress())
613 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
617 public record Read(RecipientAddress sender
, long timestamp
) {
620 ReadMessage readMessage
,
621 RecipientResolver recipientResolver
,
622 RecipientAddressResolver addressResolver
624 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
625 .toApiRecipientAddress(), readMessage
.getTimestamp());
629 public record Viewed(RecipientAddress sender
, long timestamp
) {
632 ViewedMessage readMessage
,
633 RecipientResolver recipientResolver
,
634 RecipientAddressResolver addressResolver
636 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
637 .toApiRecipientAddress(), readMessage
.getTimestamp());
641 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
643 static ViewOnceOpen
from(
644 ViewOnceOpenMessage readMessage
,
645 RecipientResolver recipientResolver
,
646 RecipientAddressResolver addressResolver
648 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
649 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
653 public record Contacts(boolean isComplete
) {
655 static Contacts
from(ContactsMessage contactsMessage
) {
656 return new Contacts(contactsMessage
.isComplete());
660 public record Groups() {
662 static Groups
from(SignalServiceAttachment groupsMessage
) {
667 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
669 static MessageRequestResponse
from(
670 MessageRequestResponseMessage messageRequestResponse
,
671 RecipientResolver recipientResolver
,
672 RecipientAddressResolver addressResolver
674 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
675 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
676 messageRequestResponse
.getPerson()
677 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
678 .toApiRecipientAddress()));
689 static Type
from(MessageRequestResponseMessage
.Type type
) {
690 return switch (type
) {
691 case UNKNOWN
-> UNKNOWN
;
692 case ACCEPT
-> ACCEPT
;
693 case DELETE
-> DELETE
;
695 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
696 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
704 Optional
<Integer
> destinationDeviceId
,
705 Optional
<GroupId
> groupId
,
706 Optional
<Long
> timestamp
,
707 Optional
<Offer
> offer
,
708 Optional
<Answer
> answer
,
709 Optional
<Hangup
> hangup
,
711 List
<IceUpdate
> iceUpdate
,
712 Optional
<Opaque
> opaque
715 public static Call
from(final SignalServiceCallMessage callMessage
) {
716 return new Call(callMessage
.getDestinationDeviceId(),
717 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
718 callMessage
.getTimestamp(),
719 callMessage
.getOfferMessage().map(Offer
::from
),
720 callMessage
.getAnswerMessage().map(Answer
::from
),
721 callMessage
.getHangupMessage().map(Hangup
::from
),
722 callMessage
.getBusyMessage().map(Busy
::from
),
723 callMessage
.getIceUpdateMessages()
724 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
726 callMessage
.getOpaqueMessage().map(Opaque
::from
));
729 public record Offer(long id
, String sdp
, Type type
, byte[] opaque
) {
731 static Offer
from(OfferMessage offerMessage
) {
732 return new Offer(offerMessage
.getId(),
733 offerMessage
.getSdp(),
734 Type
.from(offerMessage
.getType()),
735 offerMessage
.getOpaque());
742 static Type
from(OfferMessage
.Type type
) {
743 return switch (type
) {
744 case AUDIO_CALL
-> AUDIO_CALL
;
745 case VIDEO_CALL
-> VIDEO_CALL
;
751 public record Answer(long id
, String sdp
, byte[] opaque
) {
753 static Answer
from(AnswerMessage answerMessage
) {
754 return new Answer(answerMessage
.getId(), answerMessage
.getSdp(), answerMessage
.getOpaque());
758 public record Busy(long id
) {
760 static Busy
from(BusyMessage busyMessage
) {
761 return new Busy(busyMessage
.getId());
765 public record Hangup(long id
, Type type
, int deviceId
, boolean isLegacy
) {
767 static Hangup
from(HangupMessage hangupMessage
) {
768 return new Hangup(hangupMessage
.getId(),
769 Type
.from(hangupMessage
.getType()),
770 hangupMessage
.getDeviceId(),
771 hangupMessage
.isLegacy());
781 static Type
from(HangupMessage
.Type type
) {
782 return switch (type
) {
783 case NORMAL
-> NORMAL
;
784 case ACCEPTED
-> ACCEPTED
;
785 case DECLINED
-> DECLINED
;
787 case NEED_PERMISSION
-> NEED_PERMISSION
;
793 public record IceUpdate(long id
, String sdp
, byte[] opaque
) {
795 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
796 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getSdp(), iceUpdateMessage
.getOpaque());
800 public record Opaque(byte[] opaque
, Urgency urgency
) {
802 static Opaque
from(OpaqueMessage opaqueMessage
) {
803 return new Opaque(opaqueMessage
.getOpaque(), Urgency
.from(opaqueMessage
.getUrgency()));
806 public enum Urgency
{
810 static Urgency
from(OpaqueMessage
.Urgency urgency
) {
811 return switch (urgency
) {
812 case DROPPABLE
-> DROPPABLE
;
813 case HANDLE_IMMEDIATELY
-> HANDLE_IMMEDIATELY
;
821 boolean allowsReplies
,
822 Optional
<GroupId
> groupId
,
823 Optional
<Data
.Attachment
> fileAttachment
,
824 Optional
<TextAttachment
> textAttachment
827 public static Story
from(
828 SignalServiceStoryMessage storyMessage
, final AttachmentFileProvider fileProvider
830 return new Story(storyMessage
.getAllowsReplies().orElse(false),
831 storyMessage
.getGroupContext().map(c
-> GroupUtils
.getGroupIdV2(c
.getMasterKey())),
832 storyMessage
.getFileAttachment().map(f
-> Data
.Attachment
.from(f
, fileProvider
)),
833 storyMessage
.getTextAttachment().map(t
-> TextAttachment
.from(t
, fileProvider
)));
836 public record TextAttachment(
837 Optional
<String
> text
,
838 Optional
<Style
> style
,
839 Optional
<Color
> textForegroundColor
,
840 Optional
<Color
> textBackgroundColor
,
841 Optional
<Data
.Preview
> preview
,
842 Optional
<Gradient
> backgroundGradient
,
843 Optional
<Color
> backgroundColor
846 static TextAttachment
from(
847 SignalServiceTextAttachment textAttachment
, final AttachmentFileProvider fileProvider
849 return new TextAttachment(textAttachment
.getText(),
850 textAttachment
.getStyle().map(Style
::from
),
851 textAttachment
.getTextForegroundColor().map(Color
::new),
852 textAttachment
.getTextBackgroundColor().map(Color
::new),
853 textAttachment
.getPreview().map(p
-> Data
.Preview
.from(p
, fileProvider
)),
854 textAttachment
.getBackgroundGradient().map(Gradient
::from
),
855 textAttachment
.getBackgroundColor().map(Color
::new));
866 static Style
from(SignalServiceTextAttachment
.Style style
) {
867 return switch (style
) {
868 case DEFAULT
-> DEFAULT
;
869 case REGULAR
-> REGULAR
;
872 case SCRIPT
-> SCRIPT
;
873 case CONDENSED
-> CONDENSED
;
878 public record Gradient(
879 List
<Color
> colors
, List
<Float
> positions
, Optional
<Integer
> angle
882 static Gradient
from(SignalServiceTextAttachment
.Gradient gradient
) {
883 return new Gradient(gradient
.getColors().stream().map(Color
::new).toList(),
884 gradient
.getPositions(),
885 gradient
.getAngle());
891 public static MessageEnvelope
from(
892 SignalServiceEnvelope envelope
,
893 SignalServiceContent content
,
894 RecipientResolver recipientResolver
,
895 RecipientAddressResolver addressResolver
,
896 final AttachmentFileProvider fileProvider
,
899 final var source
= !envelope
.isUnidentifiedSender() && envelope
.hasSourceUuid()
900 ? recipientResolver
.resolveRecipient(envelope
.getSourceAddress())
901 : envelope
.isUnidentifiedSender() && content
!= null
902 ? recipientResolver
.resolveRecipient(content
.getSender())
903 : exception
instanceof ProtocolException e
904 ? recipientResolver
.resolveRecipient(e
.getSender())
906 final var sourceDevice
= envelope
.hasSourceDevice()
907 ? envelope
.getSourceDevice()
909 ? content
.getSenderDevice()
910 : exception
instanceof ProtocolException e ? e
.getSenderDevice() : 0;
912 Optional
<Receipt
> receipt
;
913 Optional
<Typing
> typing
;
918 Optional
<Story
> story
;
919 if (content
!= null) {
920 receipt
= content
.getReceiptMessage().map(Receipt
::from
);
921 typing
= content
.getTypingMessage().map(Typing
::from
);
922 data
= content
.getDataMessage()
923 .map(dataMessage
-> Data
.from(dataMessage
, recipientResolver
, addressResolver
, fileProvider
));
924 edit
= content
.getEditMessage().map(s
-> Edit
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
925 sync
= content
.getSyncMessage().map(s
-> Sync
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
926 call
= content
.getCallMessage().map(Call
::from
);
927 story
= content
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
));
929 receipt
= envelope
.isReceipt() ? Optional
.of(new Receipt(envelope
.getServerReceivedTimestamp(),
930 Receipt
.Type
.DELIVERY
,
931 List
.of(envelope
.getTimestamp()))) : Optional
.empty();
932 typing
= Optional
.empty();
933 data
= Optional
.empty();
934 edit
= Optional
.empty();
935 sync
= Optional
.empty();
936 call
= Optional
.empty();
937 story
= Optional
.empty();
940 return new MessageEnvelope(source
== null
942 : Optional
.of(addressResolver
.resolveRecipientAddress(source
).toApiRecipientAddress()),
944 envelope
.getTimestamp(),
945 envelope
.getServerReceivedTimestamp(),
946 envelope
.getServerDeliveredTimestamp(),
947 envelope
.isUnidentifiedSender(),
957 public interface AttachmentFileProvider
{
959 File
getFile(SignalServiceAttachmentPointer pointer
);