1 package org
.asamk
.signal
.manager
.api
;
3 import org
.asamk
.signal
.manager
.groups
.GroupUtils
;
4 import org
.asamk
.signal
.manager
.helper
.RecipientAddressResolver
;
5 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
6 import org
.signal
.libsignal
.metadata
.ProtocolException
;
7 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
8 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
9 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
10 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
11 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEditMessage
;
12 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
13 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroupContext
;
15 import org
.whispersystems
.signalservice
.api
.messages
.SignalServicePreview
;
16 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceReceiptMessage
;
17 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceStoryMessage
;
18 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTextAttachment
;
19 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceTypingMessage
;
20 import org
.whispersystems
.signalservice
.api
.messages
.calls
.AnswerMessage
;
21 import org
.whispersystems
.signalservice
.api
.messages
.calls
.BusyMessage
;
22 import org
.whispersystems
.signalservice
.api
.messages
.calls
.HangupMessage
;
23 import org
.whispersystems
.signalservice
.api
.messages
.calls
.IceUpdateMessage
;
24 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OfferMessage
;
25 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OpaqueMessage
;
26 import org
.whispersystems
.signalservice
.api
.messages
.calls
.SignalServiceCallMessage
;
27 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
28 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
29 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.MessageRequestResponseMessage
;
30 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ReadMessage
;
31 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewOnceOpenMessage
;
34 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewedMessage
;
35 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
38 import java
.io
.IOException
;
39 import java
.util
.List
;
40 import java
.util
.Optional
;
42 import java
.util
.stream
.Collectors
;
44 public record MessageEnvelope(
45 Optional
<RecipientAddress
> sourceAddress
,
48 long serverReceivedTimestamp
,
49 long serverDeliveredTimestamp
,
50 boolean isUnidentifiedSender
,
51 Optional
<Receipt
> receipt
,
52 Optional
<Typing
> typing
,
60 public record Receipt(long when, Type type
, List
<Long
> timestamps
) {
62 static Receipt
from(final SignalServiceReceiptMessage receiptMessage
) {
63 return new Receipt(receiptMessage
.getWhen(),
64 Type
.from(receiptMessage
.getType()),
65 receiptMessage
.getTimestamps());
74 static Type
from(SignalServiceReceiptMessage
.Type type
) {
75 return switch (type
) {
76 case DELIVERY
-> DELIVERY
;
78 case VIEWED
-> VIEWED
;
79 case UNKNOWN
-> UNKNOWN
;
85 public record Typing(long timestamp
, Type type
, Optional
<GroupId
> groupId
) {
87 public static Typing
from(final SignalServiceTypingMessage typingMessage
) {
88 return new Typing(typingMessage
.getTimestamp(),
89 typingMessage
.isTypingStarted() ? Type
.STARTED
: Type
.STOPPED
,
90 typingMessage
.getGroupId().map(GroupId
::unknownVersion
));
101 Optional
<GroupContext
> groupContext
,
102 Optional
<StoryContext
> storyContext
,
103 Optional
<GroupCallUpdate
> groupCallUpdate
,
104 Optional
<String
> body
,
105 int expiresInSeconds
,
106 boolean isExpirationUpdate
,
108 boolean isEndSession
,
109 boolean isProfileKeyUpdate
,
110 boolean hasProfileKey
,
111 Optional
<Reaction
> reaction
,
112 Optional
<Quote
> quote
,
113 Optional
<Payment
> payment
,
114 List
<Attachment
> attachments
,
115 Optional
<Long
> remoteDeleteId
,
116 Optional
<Sticker
> sticker
,
117 List
<SharedContact
> sharedContacts
,
118 List
<Mention
> mentions
,
119 List
<Preview
> previews
,
120 List
<TextStyle
> textStyles
124 final SignalServiceDataMessage dataMessage
,
125 RecipientResolver recipientResolver
,
126 RecipientAddressResolver addressResolver
,
127 final AttachmentFileProvider fileProvider
129 return new Data(dataMessage
.getTimestamp(),
130 dataMessage
.getGroupContext().map(GroupContext
::from
),
131 dataMessage
.getStoryContext()
132 .map((SignalServiceDataMessage
.StoryContext storyContext
) -> StoryContext
.from(storyContext
,
135 dataMessage
.getGroupCallUpdate().map(GroupCallUpdate
::from
),
136 dataMessage
.getBody(),
137 dataMessage
.getExpiresInSeconds(),
138 dataMessage
.isExpirationUpdate(),
139 dataMessage
.isViewOnce(),
140 dataMessage
.isEndSession(),
141 dataMessage
.isProfileKeyUpdate(),
142 dataMessage
.getProfileKey().isPresent(),
143 dataMessage
.getReaction().map(r
-> Reaction
.from(r
, recipientResolver
, addressResolver
)),
144 dataMessage
.getQuote()
145 .filter(q
-> q
.getAuthor() != null && q
.getAuthor().isValid())
146 .map(q
-> Quote
.from(q
, recipientResolver
, addressResolver
, fileProvider
)),
147 dataMessage
.getPayment().map(p
-> p
.getPaymentNotification().isPresent() ? Payment
.from(p
) : null),
148 dataMessage
.getAttachments()
149 .map(a
-> a
.stream().map(as -> Attachment
.from(as, fileProvider
)).toList())
151 dataMessage
.getRemoteDelete().map(SignalServiceDataMessage
.RemoteDelete
::getTargetSentTimestamp
),
152 dataMessage
.getSticker().map(Sticker
::from
),
153 dataMessage
.getSharedContacts()
155 .map(sharedContact
-> SharedContact
.from(sharedContact
, fileProvider
))
158 dataMessage
.getMentions()
159 .map(a
-> a
.stream().map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
)).toList())
161 dataMessage
.getPreviews()
162 .map(a
-> a
.stream().map(preview
-> Preview
.from(preview
, fileProvider
)).toList())
164 dataMessage
.getBodyRanges()
165 .map(a
-> a
.stream().filter(r
-> r
.style
!= null).map(TextStyle
::from
).toList())
169 public record GroupContext(GroupId groupId
, boolean isGroupUpdate
, int revision
) {
171 static GroupContext
from(SignalServiceGroupContext groupContext
) {
172 if (groupContext
.getGroupV1().isPresent()) {
173 return new GroupContext(GroupId
.v1(groupContext
.getGroupV1().get().getGroupId()),
174 groupContext
.getGroupV1Type() == SignalServiceGroup
.Type
.UPDATE
,
176 } else if (groupContext
.getGroupV2().isPresent()) {
177 final var groupV2
= groupContext
.getGroupV2().get();
178 return new GroupContext(GroupUtils
.getGroupIdV2(groupV2
.getMasterKey()),
179 groupV2
.hasSignedGroupChange(),
180 groupV2
.getRevision());
182 throw new RuntimeException("Invalid group context state");
187 public record StoryContext(RecipientAddress author
, long sentTimestamp
) {
189 static StoryContext
from(
190 SignalServiceDataMessage
.StoryContext storyContext
,
191 RecipientResolver recipientResolver
,
192 RecipientAddressResolver addressResolver
194 return new StoryContext(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
195 storyContext
.getAuthorServiceId())).toApiRecipientAddress(), storyContext
.getSentTimestamp());
199 public record GroupCallUpdate(String eraId
) {
201 static GroupCallUpdate
from(SignalServiceDataMessage
.GroupCallUpdate groupCallUpdate
) {
202 return new GroupCallUpdate(groupCallUpdate
.getEraId());
206 public record Reaction(
207 long targetSentTimestamp
, RecipientAddress targetAuthor
, String emoji
, boolean isRemove
210 static Reaction
from(
211 SignalServiceDataMessage
.Reaction reaction
,
212 RecipientResolver recipientResolver
,
213 RecipientAddressResolver addressResolver
215 return new Reaction(reaction
.getTargetSentTimestamp(),
216 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(reaction
.getTargetAuthor()))
217 .toApiRecipientAddress(),
219 reaction
.isRemove());
225 RecipientAddress author
,
226 Optional
<String
> text
,
227 List
<Mention
> mentions
,
228 List
<Attachment
> attachments
,
229 List
<TextStyle
> textStyles
233 SignalServiceDataMessage
.Quote quote
,
234 RecipientResolver recipientResolver
,
235 RecipientAddressResolver addressResolver
,
236 final AttachmentFileProvider fileProvider
238 return new Quote(quote
.getId(),
239 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(quote
.getAuthor()))
240 .toApiRecipientAddress(),
241 Optional
.of(quote
.getText()),
242 quote
.getMentions() == null
244 : quote
.getMentions()
246 .map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
))
248 quote
.getAttachments() == null
250 : quote
.getAttachments().stream().map(a
-> Attachment
.from(a
, fileProvider
)).toList(),
251 quote
.getBodyRanges() == null
253 : quote
.getBodyRanges()
255 .filter(r
-> r
.style
!= null)
256 .map(TextStyle
::from
)
261 public record Payment(String note
, byte[] receipt
) {
263 static Payment
from(SignalServiceDataMessage
.Payment payment
) {
264 return new Payment(payment
.getPaymentNotification().get().getNote(),
265 payment
.getPaymentNotification().get().getReceipt());
269 public record Mention(RecipientAddress recipient
, int start
, int length
) {
272 SignalServiceDataMessage
.Mention mention
,
273 RecipientResolver recipientResolver
,
274 RecipientAddressResolver addressResolver
276 return new Mention(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(mention
.getServiceId()))
277 .toApiRecipientAddress(), mention
.getStart(), mention
.getLength());
281 public record Attachment(
284 Optional
<String
> fileName
,
286 Optional
<Long
> uploadTimestamp
,
288 Optional
<byte[]> preview
,
289 Optional
<Attachment
> thumbnail
,
290 Optional
<String
> caption
,
291 Optional
<Integer
> width
,
292 Optional
<Integer
> height
,
298 static Attachment
from(SignalServiceAttachment signalAttachment
, AttachmentFileProvider fileProvider
) {
299 if (signalAttachment
.isPointer()) {
300 final var a
= signalAttachment
.asPointer();
301 final var attachmentFile
= fileProvider
.getFile(a
);
302 return new Attachment(Optional
.of(attachmentFile
.getName()),
303 Optional
.of(attachmentFile
),
306 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
307 a
.getSize().map(Integer
::longValue
),
310 a
.getCaption().map(c
-> c
.isEmpty() ?
null : c
),
311 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
312 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
317 Attachment attachment
= null;
318 try (final var a
= signalAttachment
.asStream()) {
319 attachment
= new Attachment(Optional
.empty(),
323 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
324 Optional
.of(a
.getLength()),
328 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
329 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
334 } catch (IOException e
) {
340 static Attachment
from(
341 SignalServiceDataMessage
.Quote
.QuotedAttachment a
,
342 final AttachmentFileProvider fileProvider
344 return new Attachment(Optional
.empty(),
346 Optional
.ofNullable(a
.getFileName()),
351 a
.getThumbnail() == null
353 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
363 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
365 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
366 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
367 sticker
.getPackKey(),
368 sticker
.getStickerId());
372 public record SharedContact(
374 Optional
<Avatar
> avatar
,
377 List
<Address
> address
,
378 Optional
<String
> organization
381 static SharedContact
from(
382 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
383 final AttachmentFileProvider fileProvider
385 return new SharedContact(Name
.from(sharedContact
.getName()),
386 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
387 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
388 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
389 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
390 sharedContact
.getOrganization());
394 Optional
<String
> nickname
,
395 Optional
<String
> given
,
396 Optional
<String
> family
,
397 Optional
<String
> prefix
,
398 Optional
<String
> suffix
,
399 Optional
<String
> middle
402 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
403 return new Name(name
.getNickname(),
412 public record Avatar(Attachment attachment
, boolean isProfile
) {
415 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
416 final AttachmentFileProvider fileProvider
418 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
423 String value
, Type type
, Optional
<String
> label
426 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
427 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
436 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
437 return switch (type
) {
440 case MOBILE
-> MOBILE
;
441 case CUSTOM
-> CUSTOM
;
448 String value
, Type type
, Optional
<String
> label
451 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
452 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
461 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
462 return switch (type
) {
465 case MOBILE
-> MOBILE
;
466 case CUSTOM
-> CUSTOM
;
472 public record Address(
474 Optional
<String
> label
,
475 Optional
<String
> street
,
476 Optional
<String
> pobox
,
477 Optional
<String
> neighborhood
,
478 Optional
<String
> city
,
479 Optional
<String
> region
,
480 Optional
<String
> postcode
,
481 Optional
<String
> country
484 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
485 return new Address(Type
.from(address
.getType()),
489 address
.getNeighborhood(),
492 address
.getPostcode(),
493 address
.getCountry());
501 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
502 return switch (type
) {
505 case CUSTOM
-> CUSTOM
;
512 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
514 static Preview
from(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
.individuals
.stream()
615 .map(d
-> new RecipientAddress(d
.getAci() == null ?
null : d
.getAci().toString(),
619 .toList(), blockedListMessage
.groupIds
.stream().map(GroupId
::unknownVersion
).toList());
623 public record Read(RecipientAddress sender
, long timestamp
) {
626 ReadMessage readMessage
,
627 RecipientResolver recipientResolver
,
628 RecipientAddressResolver addressResolver
630 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
631 .toApiRecipientAddress(), readMessage
.getTimestamp());
635 public record Viewed(RecipientAddress sender
, long timestamp
) {
638 ViewedMessage readMessage
,
639 RecipientResolver recipientResolver
,
640 RecipientAddressResolver addressResolver
642 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
643 .toApiRecipientAddress(), readMessage
.getTimestamp());
647 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
649 static ViewOnceOpen
from(
650 ViewOnceOpenMessage readMessage
,
651 RecipientResolver recipientResolver
,
652 RecipientAddressResolver addressResolver
654 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
655 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
659 public record Contacts(boolean isComplete
) {
661 static Contacts
from(ContactsMessage contactsMessage
) {
662 return new Contacts(contactsMessage
.isComplete());
666 public record Groups() {
668 static Groups
from(SignalServiceAttachment groupsMessage
) {
673 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
675 static MessageRequestResponse
from(
676 MessageRequestResponseMessage messageRequestResponse
,
677 RecipientResolver recipientResolver
,
678 RecipientAddressResolver addressResolver
680 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
681 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
682 messageRequestResponse
.getPerson()
683 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
684 .toApiRecipientAddress()));
697 static Type
from(MessageRequestResponseMessage
.Type type
) {
698 return switch (type
) {
699 case UNKNOWN
-> UNKNOWN
;
700 case ACCEPT
-> ACCEPT
;
701 case DELETE
-> DELETE
;
703 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
704 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
706 case BLOCK_AND_SPAM
-> BLOCK_AND_SPAM
;
714 Optional
<Integer
> destinationDeviceId
,
715 Optional
<GroupId
> groupId
,
716 Optional
<Long
> timestamp
,
717 Optional
<Offer
> offer
,
718 Optional
<Answer
> answer
,
719 Optional
<Hangup
> hangup
,
721 List
<IceUpdate
> iceUpdate
,
722 Optional
<Opaque
> opaque
,
726 public static Call
from(final SignalServiceCallMessage callMessage
) {
727 return new Call(callMessage
.getDestinationDeviceId(),
728 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
729 callMessage
.getTimestamp(),
730 callMessage
.getOfferMessage().map(Offer
::from
),
731 callMessage
.getAnswerMessage().map(Answer
::from
),
732 callMessage
.getHangupMessage().map(Hangup
::from
),
733 callMessage
.getBusyMessage().map(Busy
::from
),
734 callMessage
.getIceUpdateMessages()
735 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
737 callMessage
.getOpaqueMessage().map(Opaque
::from
),
738 callMessage
.isUrgent());
741 public record Offer(long id
, Type type
, byte[] opaque
) {
743 static Offer
from(OfferMessage offerMessage
) {
744 return new Offer(offerMessage
.getId(), Type
.from(offerMessage
.getType()), offerMessage
.getOpaque());
751 static Type
from(OfferMessage
.Type type
) {
752 return switch (type
) {
753 case AUDIO_CALL
-> AUDIO_CALL
;
754 case VIDEO_CALL
-> VIDEO_CALL
;
760 public record Answer(long id
, byte[] opaque
) {
762 static Answer
from(AnswerMessage answerMessage
) {
763 return new Answer(answerMessage
.getId(), answerMessage
.getOpaque());
767 public record Busy(long id
) {
769 static Busy
from(BusyMessage busyMessage
) {
770 return new Busy(busyMessage
.getId());
774 public record Hangup(long id
, Type type
, int deviceId
) {
776 static Hangup
from(HangupMessage hangupMessage
) {
777 return new Hangup(hangupMessage
.getId(),
778 Type
.from(hangupMessage
.getType()),
779 hangupMessage
.getDeviceId());
789 static Type
from(HangupMessage
.Type type
) {
790 return switch (type
) {
791 case NORMAL
-> NORMAL
;
792 case ACCEPTED
-> ACCEPTED
;
793 case DECLINED
-> DECLINED
;
795 case NEED_PERMISSION
-> NEED_PERMISSION
;
801 public record IceUpdate(long id
, byte[] opaque
) {
803 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
804 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getOpaque());
808 public record Opaque(byte[] opaque
, Urgency urgency
) {
810 static Opaque
from(OpaqueMessage opaqueMessage
) {
811 return new Opaque(opaqueMessage
.getOpaque(), Urgency
.from(opaqueMessage
.getUrgency()));
814 public enum Urgency
{
818 static Urgency
from(OpaqueMessage
.Urgency urgency
) {
819 return switch (urgency
) {
820 case DROPPABLE
-> DROPPABLE
;
821 case HANDLE_IMMEDIATELY
-> HANDLE_IMMEDIATELY
;
829 boolean allowsReplies
,
830 Optional
<GroupId
> groupId
,
831 Optional
<Data
.Attachment
> fileAttachment
,
832 Optional
<TextAttachment
> textAttachment
835 public static Story
from(SignalServiceStoryMessage storyMessage
, final AttachmentFileProvider fileProvider
) {
836 return new Story(storyMessage
.getAllowsReplies().orElse(false),
837 storyMessage
.getGroupContext().map(c
-> GroupUtils
.getGroupIdV2(c
.getMasterKey())),
838 storyMessage
.getFileAttachment().map(f
-> Data
.Attachment
.from(f
, fileProvider
)),
839 storyMessage
.getTextAttachment().map(t
-> TextAttachment
.from(t
, fileProvider
)));
842 public record TextAttachment(
843 Optional
<String
> text
,
844 Optional
<Style
> style
,
845 Optional
<Color
> textForegroundColor
,
846 Optional
<Color
> textBackgroundColor
,
847 Optional
<Data
.Preview
> preview
,
848 Optional
<Gradient
> backgroundGradient
,
849 Optional
<Color
> backgroundColor
852 static TextAttachment
from(
853 SignalServiceTextAttachment textAttachment
,
854 final AttachmentFileProvider fileProvider
856 return new TextAttachment(textAttachment
.getText(),
857 textAttachment
.getStyle().map(Style
::from
),
858 textAttachment
.getTextForegroundColor().map(Color
::new),
859 textAttachment
.getTextBackgroundColor().map(Color
::new),
860 textAttachment
.getPreview().map(p
-> Data
.Preview
.from(p
, fileProvider
)),
861 textAttachment
.getBackgroundGradient().map(Gradient
::from
),
862 textAttachment
.getBackgroundColor().map(Color
::new));
873 static Style
from(SignalServiceTextAttachment
.Style style
) {
874 return switch (style
) {
875 case DEFAULT
-> DEFAULT
;
876 case REGULAR
-> REGULAR
;
879 case SCRIPT
-> SCRIPT
;
880 case CONDENSED
-> CONDENSED
;
885 public record Gradient(
886 List
<Color
> colors
, List
<Float
> positions
, Optional
<Integer
> angle
889 static Gradient
from(SignalServiceTextAttachment
.Gradient gradient
) {
890 return new Gradient(gradient
.getColors().stream().map(Color
::new).toList(),
891 gradient
.getPositions(),
892 gradient
.getAngle());
898 public static MessageEnvelope
from(
899 SignalServiceEnvelope envelope
,
900 SignalServiceContent content
,
901 RecipientResolver recipientResolver
,
902 RecipientAddressResolver addressResolver
,
903 final AttachmentFileProvider fileProvider
,
906 final var serviceId
= envelope
.getSourceServiceId().map(ServiceId
::parseOrNull
).orElse(null);
907 final var source
= !envelope
.isUnidentifiedSender() && serviceId
!= null
908 ? recipientResolver
.resolveRecipient(serviceId
)
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
);