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
)));
518 public record TextStyle(Style style
, int start
, int length
) {
528 static Style
from(BodyRange
.Style style
) {
529 return switch (style
) {
532 case ITALIC
-> ITALIC
;
533 case SPOILER
-> SPOILER
;
534 case STRIKETHROUGH
-> STRIKETHROUGH
;
535 case MONOSPACE
-> MONOSPACE
;
540 static TextStyle
from(BodyRange bodyRange
) {
541 return new TextStyle(Style
.from(bodyRange
.getStyle()), bodyRange
.getStart(), bodyRange
.getLength());
546 public record Edit(long targetSentTimestamp
, Data dataMessage
) {
548 public static Edit
from(
549 final SignalServiceEditMessage editMessage
,
550 RecipientResolver recipientResolver
,
551 RecipientAddressResolver addressResolver
,
552 final AttachmentFileProvider fileProvider
554 return new Edit(editMessage
.getTargetSentTimestamp(),
555 Data
.from(editMessage
.getDataMessage(), recipientResolver
, addressResolver
, fileProvider
));
561 Optional
<Blocked
> blocked
,
564 Optional
<ViewOnceOpen
> viewOnceOpen
,
565 Optional
<Contacts
> contacts
,
566 Optional
<Groups
> groups
,
567 Optional
<MessageRequestResponse
> messageRequestResponse
570 public static Sync
from(
571 final SignalServiceSyncMessage syncMessage
,
572 RecipientResolver recipientResolver
,
573 RecipientAddressResolver addressResolver
,
574 final AttachmentFileProvider fileProvider
576 return new Sync(syncMessage
.getSent()
577 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
578 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
579 syncMessage
.getRead()
580 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
582 syncMessage
.getViewed()
584 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
587 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
588 syncMessage
.getContacts().map(Contacts
::from
),
589 syncMessage
.getGroups().map(Groups
::from
),
590 syncMessage
.getMessageRequestResponse()
591 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
596 long expirationStartTimestamp
,
597 Optional
<RecipientAddress
> destination
,
598 Set
<RecipientAddress
> recipients
,
599 Optional
<Data
> message
,
600 Optional
<Edit
> editMessage
,
601 Optional
<Story
> story
605 SentTranscriptMessage sentMessage
,
606 RecipientResolver recipientResolver
,
607 RecipientAddressResolver addressResolver
,
608 final AttachmentFileProvider fileProvider
610 return new Sent(sentMessage
.getTimestamp(),
611 sentMessage
.getExpirationStartTimestamp(),
612 sentMessage
.getDestination()
613 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
614 .toApiRecipientAddress()),
615 sentMessage
.getRecipients()
617 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
618 .toApiRecipientAddress())
619 .collect(Collectors
.toSet()),
620 sentMessage
.getDataMessage()
621 .map(message
-> Data
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
622 sentMessage
.getEditMessage()
623 .map(message
-> Edit
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
624 sentMessage
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
)));
628 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
631 BlockedListMessage blockedListMessage
,
632 RecipientResolver recipientResolver
,
633 RecipientAddressResolver addressResolver
635 return new Blocked(blockedListMessage
.getAddresses()
637 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
638 .toApiRecipientAddress())
639 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
643 public record Read(RecipientAddress sender
, long timestamp
) {
646 ReadMessage readMessage
,
647 RecipientResolver recipientResolver
,
648 RecipientAddressResolver addressResolver
650 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
651 .toApiRecipientAddress(), readMessage
.getTimestamp());
655 public record Viewed(RecipientAddress sender
, long timestamp
) {
658 ViewedMessage readMessage
,
659 RecipientResolver recipientResolver
,
660 RecipientAddressResolver addressResolver
662 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
663 .toApiRecipientAddress(), readMessage
.getTimestamp());
667 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
669 static ViewOnceOpen
from(
670 ViewOnceOpenMessage readMessage
,
671 RecipientResolver recipientResolver
,
672 RecipientAddressResolver addressResolver
674 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
675 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
679 public record Contacts(boolean isComplete
) {
681 static Contacts
from(ContactsMessage contactsMessage
) {
682 return new Contacts(contactsMessage
.isComplete());
686 public record Groups() {
688 static Groups
from(SignalServiceAttachment groupsMessage
) {
693 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
695 static MessageRequestResponse
from(
696 MessageRequestResponseMessage messageRequestResponse
,
697 RecipientResolver recipientResolver
,
698 RecipientAddressResolver addressResolver
700 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
701 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
702 messageRequestResponse
.getPerson()
703 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
704 .toApiRecipientAddress()));
715 static Type
from(MessageRequestResponseMessage
.Type type
) {
716 return switch (type
) {
717 case UNKNOWN
-> UNKNOWN
;
718 case ACCEPT
-> ACCEPT
;
719 case DELETE
-> DELETE
;
721 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
722 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
730 Optional
<Integer
> destinationDeviceId
,
731 Optional
<GroupId
> groupId
,
732 Optional
<Long
> timestamp
,
733 Optional
<Offer
> offer
,
734 Optional
<Answer
> answer
,
735 Optional
<Hangup
> hangup
,
737 List
<IceUpdate
> iceUpdate
,
738 Optional
<Opaque
> opaque
741 public static Call
from(final SignalServiceCallMessage callMessage
) {
742 return new Call(callMessage
.getDestinationDeviceId(),
743 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
744 callMessage
.getTimestamp(),
745 callMessage
.getOfferMessage().map(Offer
::from
),
746 callMessage
.getAnswerMessage().map(Answer
::from
),
747 callMessage
.getHangupMessage().map(Hangup
::from
),
748 callMessage
.getBusyMessage().map(Busy
::from
),
749 callMessage
.getIceUpdateMessages()
750 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
752 callMessage
.getOpaqueMessage().map(Opaque
::from
));
755 public record Offer(long id
, String sdp
, Type type
, byte[] opaque
) {
757 static Offer
from(OfferMessage offerMessage
) {
758 return new Offer(offerMessage
.getId(),
759 offerMessage
.getSdp(),
760 Type
.from(offerMessage
.getType()),
761 offerMessage
.getOpaque());
768 static Type
from(OfferMessage
.Type type
) {
769 return switch (type
) {
770 case AUDIO_CALL
-> AUDIO_CALL
;
771 case VIDEO_CALL
-> VIDEO_CALL
;
777 public record Answer(long id
, String sdp
, byte[] opaque
) {
779 static Answer
from(AnswerMessage answerMessage
) {
780 return new Answer(answerMessage
.getId(), answerMessage
.getSdp(), answerMessage
.getOpaque());
784 public record Busy(long id
) {
786 static Busy
from(BusyMessage busyMessage
) {
787 return new Busy(busyMessage
.getId());
791 public record Hangup(long id
, Type type
, int deviceId
, boolean isLegacy
) {
793 static Hangup
from(HangupMessage hangupMessage
) {
794 return new Hangup(hangupMessage
.getId(),
795 Type
.from(hangupMessage
.getType()),
796 hangupMessage
.getDeviceId(),
797 hangupMessage
.isLegacy());
807 static Type
from(HangupMessage
.Type type
) {
808 return switch (type
) {
809 case NORMAL
-> NORMAL
;
810 case ACCEPTED
-> ACCEPTED
;
811 case DECLINED
-> DECLINED
;
813 case NEED_PERMISSION
-> NEED_PERMISSION
;
819 public record IceUpdate(long id
, String sdp
, byte[] opaque
) {
821 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
822 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getSdp(), iceUpdateMessage
.getOpaque());
826 public record Opaque(byte[] opaque
, Urgency urgency
) {
828 static Opaque
from(OpaqueMessage opaqueMessage
) {
829 return new Opaque(opaqueMessage
.getOpaque(), Urgency
.from(opaqueMessage
.getUrgency()));
832 public enum Urgency
{
836 static Urgency
from(OpaqueMessage
.Urgency urgency
) {
837 return switch (urgency
) {
838 case DROPPABLE
-> DROPPABLE
;
839 case HANDLE_IMMEDIATELY
-> HANDLE_IMMEDIATELY
;
847 boolean allowsReplies
,
848 Optional
<GroupId
> groupId
,
849 Optional
<Data
.Attachment
> fileAttachment
,
850 Optional
<TextAttachment
> textAttachment
853 public static Story
from(
854 SignalServiceStoryMessage storyMessage
, final AttachmentFileProvider fileProvider
856 return new Story(storyMessage
.getAllowsReplies().orElse(false),
857 storyMessage
.getGroupContext().map(c
-> GroupUtils
.getGroupIdV2(c
.getMasterKey())),
858 storyMessage
.getFileAttachment().map(f
-> Data
.Attachment
.from(f
, fileProvider
)),
859 storyMessage
.getTextAttachment().map(t
-> TextAttachment
.from(t
, fileProvider
)));
862 public record TextAttachment(
863 Optional
<String
> text
,
864 Optional
<Style
> style
,
865 Optional
<Color
> textForegroundColor
,
866 Optional
<Color
> textBackgroundColor
,
867 Optional
<Data
.Preview
> preview
,
868 Optional
<Gradient
> backgroundGradient
,
869 Optional
<Color
> backgroundColor
872 static TextAttachment
from(
873 SignalServiceTextAttachment textAttachment
, final AttachmentFileProvider fileProvider
875 return new TextAttachment(textAttachment
.getText(),
876 textAttachment
.getStyle().map(Style
::from
),
877 textAttachment
.getTextForegroundColor().map(Color
::new),
878 textAttachment
.getTextBackgroundColor().map(Color
::new),
879 textAttachment
.getPreview().map(p
-> Data
.Preview
.from(p
, fileProvider
)),
880 textAttachment
.getBackgroundGradient().map(Gradient
::from
),
881 textAttachment
.getBackgroundColor().map(Color
::new));
892 static Style
from(SignalServiceTextAttachment
.Style style
) {
893 return switch (style
) {
894 case DEFAULT
-> DEFAULT
;
895 case REGULAR
-> REGULAR
;
898 case SCRIPT
-> SCRIPT
;
899 case CONDENSED
-> CONDENSED
;
904 public record Gradient(
905 List
<Color
> colors
, List
<Float
> positions
, Optional
<Integer
> angle
908 static Gradient
from(SignalServiceTextAttachment
.Gradient gradient
) {
909 return new Gradient(gradient
.getColors().stream().map(Color
::new).toList(),
910 gradient
.getPositions(),
911 gradient
.getAngle());
917 public static MessageEnvelope
from(
918 SignalServiceEnvelope envelope
,
919 SignalServiceContent content
,
920 RecipientResolver recipientResolver
,
921 RecipientAddressResolver addressResolver
,
922 final AttachmentFileProvider fileProvider
,
925 final var source
= !envelope
.isUnidentifiedSender() && envelope
.hasSourceUuid()
926 ? recipientResolver
.resolveRecipient(envelope
.getSourceAddress())
927 : envelope
.isUnidentifiedSender() && content
!= null
928 ? recipientResolver
.resolveRecipient(content
.getSender())
929 : exception
instanceof ProtocolException e
930 ? recipientResolver
.resolveRecipient(e
.getSender())
932 final var sourceDevice
= envelope
.hasSourceDevice()
933 ? envelope
.getSourceDevice()
935 ? content
.getSenderDevice()
936 : exception
instanceof ProtocolException e ? e
.getSenderDevice() : 0;
938 Optional
<Receipt
> receipt
;
939 Optional
<Typing
> typing
;
944 Optional
<Story
> story
;
945 if (content
!= null) {
946 receipt
= content
.getReceiptMessage().map(Receipt
::from
);
947 typing
= content
.getTypingMessage().map(Typing
::from
);
948 data
= content
.getDataMessage()
949 .map(dataMessage
-> Data
.from(dataMessage
, recipientResolver
, addressResolver
, fileProvider
));
950 edit
= content
.getEditMessage().map(s
-> Edit
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
951 sync
= content
.getSyncMessage().map(s
-> Sync
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
952 call
= content
.getCallMessage().map(Call
::from
);
953 story
= content
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
));
955 receipt
= envelope
.isReceipt() ? Optional
.of(new Receipt(envelope
.getServerReceivedTimestamp(),
956 Receipt
.Type
.DELIVERY
,
957 List
.of(envelope
.getTimestamp()))) : Optional
.empty();
958 typing
= Optional
.empty();
959 data
= Optional
.empty();
960 edit
= Optional
.empty();
961 sync
= Optional
.empty();
962 call
= Optional
.empty();
963 story
= Optional
.empty();
966 return new MessageEnvelope(source
== null
968 : Optional
.of(addressResolver
.resolveRecipientAddress(source
).toApiRecipientAddress()),
970 envelope
.getTimestamp(),
971 envelope
.getServerReceivedTimestamp(),
972 envelope
.getServerDeliveredTimestamp(),
973 envelope
.isUnidentifiedSender(),
983 public interface AttachmentFileProvider
{
985 File
getFile(SignalServiceAttachmentPointer pointer
);