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
.util
.List
;
38 import java
.util
.Optional
;
40 import java
.util
.stream
.Collectors
;
42 public record MessageEnvelope(
43 Optional
<RecipientAddress
> sourceAddress
,
46 long serverReceivedTimestamp
,
47 long serverDeliveredTimestamp
,
48 boolean isUnidentifiedSender
,
49 Optional
<Receipt
> receipt
,
50 Optional
<Typing
> typing
,
58 public record Receipt(long when, Type type
, List
<Long
> timestamps
) {
60 static Receipt
from(final SignalServiceReceiptMessage receiptMessage
) {
61 return new Receipt(receiptMessage
.getWhen(),
62 Type
.from(receiptMessage
.getType()),
63 receiptMessage
.getTimestamps());
72 static Type
from(SignalServiceReceiptMessage
.Type type
) {
73 return switch (type
) {
74 case DELIVERY
-> DELIVERY
;
76 case VIEWED
-> VIEWED
;
77 case UNKNOWN
-> UNKNOWN
;
83 public record Typing(long timestamp
, Type type
, Optional
<GroupId
> groupId
) {
85 public static Typing
from(final SignalServiceTypingMessage typingMessage
) {
86 return new Typing(typingMessage
.getTimestamp(),
87 typingMessage
.isTypingStarted() ? Type
.STARTED
: Type
.STOPPED
,
88 typingMessage
.getGroupId().map(GroupId
::unknownVersion
));
99 Optional
<GroupContext
> groupContext
,
100 Optional
<StoryContext
> storyContext
,
101 Optional
<GroupCallUpdate
> groupCallUpdate
,
102 Optional
<String
> body
,
103 int expiresInSeconds
,
104 boolean isExpirationUpdate
,
106 boolean isEndSession
,
107 boolean isProfileKeyUpdate
,
108 boolean hasProfileKey
,
109 Optional
<Reaction
> reaction
,
110 Optional
<Quote
> quote
,
111 Optional
<Payment
> payment
,
112 List
<Attachment
> attachments
,
113 Optional
<Long
> remoteDeleteId
,
114 Optional
<Sticker
> sticker
,
115 List
<SharedContact
> sharedContacts
,
116 List
<Mention
> mentions
,
117 List
<Preview
> previews
,
118 List
<TextStyle
> textStyles
122 final SignalServiceDataMessage dataMessage
,
123 RecipientResolver recipientResolver
,
124 RecipientAddressResolver addressResolver
,
125 final AttachmentFileProvider fileProvider
127 return new Data(dataMessage
.getTimestamp(),
128 dataMessage
.getGroupContext().map(GroupContext
::from
),
129 dataMessage
.getStoryContext()
130 .map((SignalServiceDataMessage
.StoryContext storyContext
) -> StoryContext
.from(storyContext
,
133 dataMessage
.getGroupCallUpdate().map(GroupCallUpdate
::from
),
134 dataMessage
.getBody(),
135 dataMessage
.getExpiresInSeconds(),
136 dataMessage
.isExpirationUpdate(),
137 dataMessage
.isViewOnce(),
138 dataMessage
.isEndSession(),
139 dataMessage
.isProfileKeyUpdate(),
140 dataMessage
.getProfileKey().isPresent(),
141 dataMessage
.getReaction().map(r
-> Reaction
.from(r
, recipientResolver
, addressResolver
)),
142 dataMessage
.getQuote().map(q
-> Quote
.from(q
, recipientResolver
, addressResolver
, fileProvider
)),
143 dataMessage
.getPayment().map(p
-> p
.getPaymentNotification().isPresent() ? Payment
.from(p
) : null),
144 dataMessage
.getAttachments()
145 .map(a
-> a
.stream().map(as -> Attachment
.from(as, fileProvider
)).toList())
147 dataMessage
.getRemoteDelete().map(SignalServiceDataMessage
.RemoteDelete
::getTargetSentTimestamp
),
148 dataMessage
.getSticker().map(Sticker
::from
),
149 dataMessage
.getSharedContacts()
151 .map(sharedContact
-> SharedContact
.from(sharedContact
, fileProvider
))
154 dataMessage
.getMentions()
155 .map(a
-> a
.stream().map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
)).toList())
157 dataMessage
.getPreviews()
158 .map(a
-> a
.stream().map(preview
-> Preview
.from(preview
, fileProvider
)).toList())
160 dataMessage
.getBodyRanges()
161 .map(a
-> a
.stream().filter(r
-> r
.style
!= null).map(TextStyle
::from
).toList())
165 public record GroupContext(GroupId groupId
, boolean isGroupUpdate
, int revision
) {
167 static GroupContext
from(SignalServiceGroupContext groupContext
) {
168 if (groupContext
.getGroupV1().isPresent()) {
169 return new GroupContext(GroupId
.v1(groupContext
.getGroupV1().get().getGroupId()),
170 groupContext
.getGroupV1Type() == SignalServiceGroup
.Type
.UPDATE
,
172 } else if (groupContext
.getGroupV2().isPresent()) {
173 final var groupV2
= groupContext
.getGroupV2().get();
174 return new GroupContext(GroupUtils
.getGroupIdV2(groupV2
.getMasterKey()),
175 groupV2
.hasSignedGroupChange(),
176 groupV2
.getRevision());
178 throw new RuntimeException("Invalid group context state");
183 public record StoryContext(RecipientAddress author
, long sentTimestamp
) {
185 static StoryContext
from(
186 SignalServiceDataMessage
.StoryContext storyContext
,
187 RecipientResolver recipientResolver
,
188 RecipientAddressResolver addressResolver
190 return new StoryContext(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
191 storyContext
.getAuthorServiceId())).toApiRecipientAddress(), storyContext
.getSentTimestamp());
195 public record GroupCallUpdate(String eraId
) {
197 static GroupCallUpdate
from(SignalServiceDataMessage
.GroupCallUpdate groupCallUpdate
) {
198 return new GroupCallUpdate(groupCallUpdate
.getEraId());
202 public record Reaction(
203 long targetSentTimestamp
, RecipientAddress targetAuthor
, String emoji
, boolean isRemove
206 static Reaction
from(
207 SignalServiceDataMessage
.Reaction reaction
,
208 RecipientResolver recipientResolver
,
209 RecipientAddressResolver addressResolver
211 return new Reaction(reaction
.getTargetSentTimestamp(),
212 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(reaction
.getTargetAuthor()))
213 .toApiRecipientAddress(),
215 reaction
.isRemove());
221 RecipientAddress author
,
222 Optional
<String
> text
,
223 List
<Mention
> mentions
,
224 List
<Attachment
> attachments
,
225 List
<TextStyle
> textStyles
229 SignalServiceDataMessage
.Quote quote
,
230 RecipientResolver recipientResolver
,
231 RecipientAddressResolver addressResolver
,
232 final AttachmentFileProvider fileProvider
234 return new Quote(quote
.getId(),
235 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(quote
.getAuthor()))
236 .toApiRecipientAddress(),
237 Optional
.ofNullable(quote
.getText()),
238 quote
.getMentions() == null
240 : quote
.getMentions()
242 .map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
))
244 quote
.getAttachments() == null
246 : quote
.getAttachments().stream().map(a
-> Attachment
.from(a
, fileProvider
)).toList(),
247 quote
.getBodyRanges() == null
249 : quote
.getBodyRanges()
251 .filter(r
-> r
.style
!= null)
252 .map(TextStyle
::from
)
257 public record Payment(String note
, byte[] receipt
) {
259 static Payment
from(SignalServiceDataMessage
.Payment payment
) {
260 return new Payment(payment
.getPaymentNotification().get().getNote(),
261 payment
.getPaymentNotification().get().getReceipt());
265 public record Mention(RecipientAddress recipient
, int start
, int length
) {
268 SignalServiceDataMessage
.Mention mention
,
269 RecipientResolver recipientResolver
,
270 RecipientAddressResolver addressResolver
272 return new Mention(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(mention
.getServiceId()))
273 .toApiRecipientAddress(), mention
.getStart(), mention
.getLength());
277 public record Attachment(
280 Optional
<String
> fileName
,
282 Optional
<Long
> uploadTimestamp
,
284 Optional
<byte[]> preview
,
285 Optional
<Attachment
> thumbnail
,
286 Optional
<String
> caption
,
287 Optional
<Integer
> width
,
288 Optional
<Integer
> height
,
294 static Attachment
from(SignalServiceAttachment attachment
, AttachmentFileProvider fileProvider
) {
295 if (attachment
.isPointer()) {
296 final var a
= attachment
.asPointer();
297 final var attachmentFile
= fileProvider
.getFile(a
);
298 return new Attachment(Optional
.of(attachmentFile
.getName()),
299 Optional
.of(attachmentFile
),
302 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
303 a
.getSize().map(Integer
::longValue
),
306 a
.getCaption().map(c
-> c
.isEmpty() ?
null : c
),
307 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
308 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
313 final var a
= attachment
.asStream();
314 return new Attachment(Optional
.empty(),
318 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
319 Optional
.of(a
.getLength()),
323 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
324 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
331 static Attachment
from(
332 SignalServiceDataMessage
.Quote
.QuotedAttachment a
, final AttachmentFileProvider fileProvider
334 return new Attachment(Optional
.empty(),
336 Optional
.ofNullable(a
.getFileName()),
341 a
.getThumbnail() == null
343 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
353 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
355 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
356 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
357 sticker
.getPackKey(),
358 sticker
.getStickerId());
362 public record SharedContact(
364 Optional
<Avatar
> avatar
,
367 List
<Address
> address
,
368 Optional
<String
> organization
371 static SharedContact
from(
372 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
373 final AttachmentFileProvider fileProvider
375 return new SharedContact(Name
.from(sharedContact
.getName()),
376 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
377 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
378 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
379 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
380 sharedContact
.getOrganization());
384 Optional
<String
> display
,
385 Optional
<String
> given
,
386 Optional
<String
> family
,
387 Optional
<String
> prefix
,
388 Optional
<String
> suffix
,
389 Optional
<String
> middle
392 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
393 return new Name(name
.getDisplay(),
402 public record Avatar(Attachment attachment
, boolean isProfile
) {
405 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
406 final AttachmentFileProvider fileProvider
408 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
413 String value
, Type type
, Optional
<String
> label
416 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
417 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
426 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
427 return switch (type
) {
430 case MOBILE
-> MOBILE
;
431 case CUSTOM
-> CUSTOM
;
438 String value
, Type type
, Optional
<String
> label
441 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
442 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
451 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
452 return switch (type
) {
455 case MOBILE
-> MOBILE
;
456 case CUSTOM
-> CUSTOM
;
462 public record Address(
464 Optional
<String
> label
,
465 Optional
<String
> street
,
466 Optional
<String
> pobox
,
467 Optional
<String
> neighborhood
,
468 Optional
<String
> city
,
469 Optional
<String
> region
,
470 Optional
<String
> postcode
,
471 Optional
<String
> country
474 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
475 return new Address(Address
.Type
.from(address
.getType()),
491 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
492 return switch (type
) {
495 case CUSTOM
-> CUSTOM
;
502 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
505 SignalServicePreview preview
, final AttachmentFileProvider fileProvider
507 return new Preview(preview
.getTitle(),
508 preview
.getDescription(),
511 preview
.getImage().map(as -> Attachment
.from(as, fileProvider
)));
517 public record Edit(long targetSentTimestamp
, Data dataMessage
) {
519 public static Edit
from(
520 final SignalServiceEditMessage editMessage
,
521 RecipientResolver recipientResolver
,
522 RecipientAddressResolver addressResolver
,
523 final AttachmentFileProvider fileProvider
525 return new Edit(editMessage
.getTargetSentTimestamp(),
526 Data
.from(editMessage
.getDataMessage(), recipientResolver
, addressResolver
, fileProvider
));
532 Optional
<Blocked
> blocked
,
535 Optional
<ViewOnceOpen
> viewOnceOpen
,
536 Optional
<Contacts
> contacts
,
537 Optional
<Groups
> groups
,
538 Optional
<MessageRequestResponse
> messageRequestResponse
541 public static Sync
from(
542 final SignalServiceSyncMessage syncMessage
,
543 RecipientResolver recipientResolver
,
544 RecipientAddressResolver addressResolver
,
545 final AttachmentFileProvider fileProvider
547 return new Sync(syncMessage
.getSent()
548 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
549 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
550 syncMessage
.getRead()
551 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
553 syncMessage
.getViewed()
555 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
558 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
559 syncMessage
.getContacts().map(Contacts
::from
),
560 syncMessage
.getGroups().map(Groups
::from
),
561 syncMessage
.getMessageRequestResponse()
562 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
567 long expirationStartTimestamp
,
568 Optional
<RecipientAddress
> destination
,
569 Set
<RecipientAddress
> recipients
,
570 Optional
<Data
> message
,
571 Optional
<Edit
> editMessage
,
572 Optional
<Story
> story
576 SentTranscriptMessage sentMessage
,
577 RecipientResolver recipientResolver
,
578 RecipientAddressResolver addressResolver
,
579 final AttachmentFileProvider fileProvider
581 return new Sent(sentMessage
.getTimestamp(),
582 sentMessage
.getExpirationStartTimestamp(),
583 sentMessage
.getDestination()
584 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
585 .toApiRecipientAddress()),
586 sentMessage
.getRecipients()
588 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
589 .toApiRecipientAddress())
590 .collect(Collectors
.toSet()),
591 sentMessage
.getDataMessage()
592 .map(message
-> Data
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
593 sentMessage
.getEditMessage()
594 .map(message
-> Edit
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
595 sentMessage
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
)));
599 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
602 BlockedListMessage blockedListMessage
,
603 RecipientResolver recipientResolver
,
604 RecipientAddressResolver addressResolver
606 return new Blocked(blockedListMessage
.getAddresses()
608 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
609 .toApiRecipientAddress())
610 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
614 public record Read(RecipientAddress sender
, long timestamp
) {
617 ReadMessage readMessage
,
618 RecipientResolver recipientResolver
,
619 RecipientAddressResolver addressResolver
621 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
622 .toApiRecipientAddress(), readMessage
.getTimestamp());
626 public record Viewed(RecipientAddress sender
, long timestamp
) {
629 ViewedMessage readMessage
,
630 RecipientResolver recipientResolver
,
631 RecipientAddressResolver addressResolver
633 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
634 .toApiRecipientAddress(), readMessage
.getTimestamp());
638 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
640 static ViewOnceOpen
from(
641 ViewOnceOpenMessage readMessage
,
642 RecipientResolver recipientResolver
,
643 RecipientAddressResolver addressResolver
645 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
646 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
650 public record Contacts(boolean isComplete
) {
652 static Contacts
from(ContactsMessage contactsMessage
) {
653 return new Contacts(contactsMessage
.isComplete());
657 public record Groups() {
659 static Groups
from(SignalServiceAttachment groupsMessage
) {
664 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
666 static MessageRequestResponse
from(
667 MessageRequestResponseMessage messageRequestResponse
,
668 RecipientResolver recipientResolver
,
669 RecipientAddressResolver addressResolver
671 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
672 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
673 messageRequestResponse
.getPerson()
674 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
675 .toApiRecipientAddress()));
686 static Type
from(MessageRequestResponseMessage
.Type type
) {
687 return switch (type
) {
688 case UNKNOWN
-> UNKNOWN
;
689 case ACCEPT
-> ACCEPT
;
690 case DELETE
-> DELETE
;
692 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
693 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
701 Optional
<Integer
> destinationDeviceId
,
702 Optional
<GroupId
> groupId
,
703 Optional
<Long
> timestamp
,
704 Optional
<Offer
> offer
,
705 Optional
<Answer
> answer
,
706 Optional
<Hangup
> hangup
,
708 List
<IceUpdate
> iceUpdate
,
709 Optional
<Opaque
> opaque
,
714 public static Call
from(final SignalServiceCallMessage callMessage
) {
715 return new Call(callMessage
.getDestinationDeviceId(),
716 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
717 callMessage
.getTimestamp(),
718 callMessage
.getOfferMessage().map(Offer
::from
),
719 callMessage
.getAnswerMessage().map(Answer
::from
),
720 callMessage
.getHangupMessage().map(Hangup
::from
),
721 callMessage
.getBusyMessage().map(Busy
::from
),
722 callMessage
.getIceUpdateMessages()
723 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
725 callMessage
.getOpaqueMessage().map(Opaque
::from
),
726 callMessage
.isMultiRing(),
727 callMessage
.isUrgent());
730 public record Offer(long id
, String sdp
, Type type
, byte[] opaque
) {
732 static Offer
from(OfferMessage offerMessage
) {
733 return new Offer(offerMessage
.getId(),
734 offerMessage
.getSdp(),
735 Type
.from(offerMessage
.getType()),
736 offerMessage
.getOpaque());
743 static Type
from(OfferMessage
.Type type
) {
744 return switch (type
) {
745 case AUDIO_CALL
-> AUDIO_CALL
;
746 case VIDEO_CALL
-> VIDEO_CALL
;
752 public record Answer(long id
, String sdp
, byte[] opaque
) {
754 static Answer
from(AnswerMessage answerMessage
) {
755 return new Answer(answerMessage
.getId(), answerMessage
.getSdp(), answerMessage
.getOpaque());
759 public record Busy(long id
) {
761 static Busy
from(BusyMessage busyMessage
) {
762 return new Busy(busyMessage
.getId());
766 public record Hangup(long id
, Type type
, int deviceId
) {
768 static Hangup
from(HangupMessage hangupMessage
) {
769 return new Hangup(hangupMessage
.getId(),
770 Type
.from(hangupMessage
.getType()),
771 hangupMessage
.getDeviceId());
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
.hasSourceServiceId()
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
);