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 import static org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
.BodyRange
;
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().map(q
-> Quote
.from(q
, recipientResolver
, addressResolver
, fileProvider
)),
145 dataMessage
.getPayment().map(p
-> p
.getPaymentNotification().isPresent() ? Payment
.from(p
) : null),
146 dataMessage
.getAttachments()
147 .map(a
-> a
.stream().map(as -> Attachment
.from(as, fileProvider
)).toList())
149 dataMessage
.getRemoteDelete().map(SignalServiceDataMessage
.RemoteDelete
::getTargetSentTimestamp
),
150 dataMessage
.getSticker().map(Sticker
::from
),
151 dataMessage
.getSharedContacts()
153 .map(sharedContact
-> SharedContact
.from(sharedContact
, fileProvider
))
156 dataMessage
.getMentions()
157 .map(a
-> a
.stream().map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
)).toList())
159 dataMessage
.getPreviews()
160 .map(a
-> a
.stream().map(preview
-> Preview
.from(preview
, fileProvider
)).toList())
162 dataMessage
.getBodyRanges()
163 .map(a
-> a
.stream().filter(BodyRange
::hasStyle
).map(TextStyle
::from
).toList())
167 public record GroupContext(GroupId groupId
, boolean isGroupUpdate
, int revision
) {
169 static GroupContext
from(SignalServiceGroupContext groupContext
) {
170 if (groupContext
.getGroupV1().isPresent()) {
171 return new GroupContext(GroupId
.v1(groupContext
.getGroupV1().get().getGroupId()),
172 groupContext
.getGroupV1Type() == SignalServiceGroup
.Type
.UPDATE
,
174 } else if (groupContext
.getGroupV2().isPresent()) {
175 final var groupV2
= groupContext
.getGroupV2().get();
176 return new GroupContext(GroupUtils
.getGroupIdV2(groupV2
.getMasterKey()),
177 groupV2
.hasSignedGroupChange(),
178 groupV2
.getRevision());
180 throw new RuntimeException("Invalid group context state");
185 public record StoryContext(RecipientAddress author
, long sentTimestamp
) {
187 static StoryContext
from(
188 SignalServiceDataMessage
.StoryContext storyContext
,
189 RecipientResolver recipientResolver
,
190 RecipientAddressResolver addressResolver
192 return new StoryContext(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
193 storyContext
.getAuthorServiceId())).toApiRecipientAddress(), storyContext
.getSentTimestamp());
197 public record GroupCallUpdate(String eraId
) {
199 static GroupCallUpdate
from(SignalServiceDataMessage
.GroupCallUpdate groupCallUpdate
) {
200 return new GroupCallUpdate(groupCallUpdate
.getEraId());
204 public record Reaction(
205 long targetSentTimestamp
, RecipientAddress targetAuthor
, String emoji
, boolean isRemove
208 static Reaction
from(
209 SignalServiceDataMessage
.Reaction reaction
,
210 RecipientResolver recipientResolver
,
211 RecipientAddressResolver addressResolver
213 return new Reaction(reaction
.getTargetSentTimestamp(),
214 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(reaction
.getTargetAuthor()))
215 .toApiRecipientAddress(),
217 reaction
.isRemove());
223 RecipientAddress author
,
224 Optional
<String
> text
,
225 List
<Mention
> mentions
,
226 List
<Attachment
> attachments
,
227 List
<TextStyle
> textStyles
231 SignalServiceDataMessage
.Quote quote
,
232 RecipientResolver recipientResolver
,
233 RecipientAddressResolver addressResolver
,
234 final AttachmentFileProvider fileProvider
236 return new Quote(quote
.getId(),
237 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(quote
.getAuthor()))
238 .toApiRecipientAddress(),
239 Optional
.ofNullable(quote
.getText()),
240 quote
.getMentions() == null
242 : quote
.getMentions()
244 .map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
))
246 quote
.getAttachments() == null
248 : quote
.getAttachments().stream().map(a
-> Attachment
.from(a
, fileProvider
)).toList(),
249 quote
.getBodyRanges() == null
251 : quote
.getBodyRanges()
253 .filter(BodyRange
::hasStyle
)
254 .map(TextStyle
::from
)
259 public record Payment(String note
, byte[] receipt
) {
261 static Payment
from(SignalServiceDataMessage
.Payment payment
) {
262 return new Payment(payment
.getPaymentNotification().get().getNote(),
263 payment
.getPaymentNotification().get().getReceipt());
267 public record Mention(RecipientAddress recipient
, int start
, int length
) {
270 SignalServiceDataMessage
.Mention mention
,
271 RecipientResolver recipientResolver
,
272 RecipientAddressResolver addressResolver
274 return new Mention(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(mention
.getServiceId()))
275 .toApiRecipientAddress(), mention
.getStart(), mention
.getLength());
279 public record Attachment(
282 Optional
<String
> fileName
,
284 Optional
<Long
> uploadTimestamp
,
286 Optional
<byte[]> preview
,
287 Optional
<Attachment
> thumbnail
,
288 Optional
<String
> caption
,
289 Optional
<Integer
> width
,
290 Optional
<Integer
> height
,
296 static Attachment
from(SignalServiceAttachment attachment
, AttachmentFileProvider fileProvider
) {
297 if (attachment
.isPointer()) {
298 final var a
= attachment
.asPointer();
299 final var attachmentFile
= fileProvider
.getFile(a
);
300 return new Attachment(Optional
.of(attachmentFile
.getName()),
301 Optional
.of(attachmentFile
),
304 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
305 a
.getSize().map(Integer
::longValue
),
308 a
.getCaption().map(c
-> c
.isEmpty() ?
null : c
),
309 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
310 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
315 final var a
= attachment
.asStream();
316 return new Attachment(Optional
.empty(),
320 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
321 Optional
.of(a
.getLength()),
325 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
326 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
333 static Attachment
from(
334 SignalServiceDataMessage
.Quote
.QuotedAttachment a
, final AttachmentFileProvider fileProvider
336 return new Attachment(Optional
.empty(),
338 Optional
.ofNullable(a
.getFileName()),
343 a
.getThumbnail() == null
345 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
355 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
357 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
358 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
359 sticker
.getPackKey(),
360 sticker
.getStickerId());
364 public record SharedContact(
366 Optional
<Avatar
> avatar
,
369 List
<Address
> address
,
370 Optional
<String
> organization
373 static SharedContact
from(
374 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
375 final AttachmentFileProvider fileProvider
377 return new SharedContact(Name
.from(sharedContact
.getName()),
378 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
379 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
380 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
381 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
382 sharedContact
.getOrganization());
386 Optional
<String
> display
,
387 Optional
<String
> given
,
388 Optional
<String
> family
,
389 Optional
<String
> prefix
,
390 Optional
<String
> suffix
,
391 Optional
<String
> middle
394 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
395 return new Name(name
.getDisplay(),
404 public record Avatar(Attachment attachment
, boolean isProfile
) {
407 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
408 final AttachmentFileProvider fileProvider
410 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
415 String value
, Type type
, Optional
<String
> label
418 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
419 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
428 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
429 return switch (type
) {
432 case MOBILE
-> MOBILE
;
433 case CUSTOM
-> CUSTOM
;
440 String value
, Type type
, Optional
<String
> label
443 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
444 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
453 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
454 return switch (type
) {
457 case MOBILE
-> MOBILE
;
458 case CUSTOM
-> CUSTOM
;
464 public record Address(
466 Optional
<String
> label
,
467 Optional
<String
> street
,
468 Optional
<String
> pobox
,
469 Optional
<String
> neighborhood
,
470 Optional
<String
> city
,
471 Optional
<String
> region
,
472 Optional
<String
> postcode
,
473 Optional
<String
> country
476 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
477 return new Address(Address
.Type
.from(address
.getType()),
493 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
494 return switch (type
) {
497 case CUSTOM
-> CUSTOM
;
504 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
507 SignalServicePreview preview
, final AttachmentFileProvider fileProvider
509 return new Preview(preview
.getTitle(),
510 preview
.getDescription(),
513 preview
.getImage().map(as -> Attachment
.from(as, fileProvider
)));
519 public record Edit(long targetSentTimestamp
, Data dataMessage
) {
521 public static Edit
from(
522 final SignalServiceEditMessage editMessage
,
523 RecipientResolver recipientResolver
,
524 RecipientAddressResolver addressResolver
,
525 final AttachmentFileProvider fileProvider
527 return new Edit(editMessage
.getTargetSentTimestamp(),
528 Data
.from(editMessage
.getDataMessage(), recipientResolver
, addressResolver
, fileProvider
));
534 Optional
<Blocked
> blocked
,
537 Optional
<ViewOnceOpen
> viewOnceOpen
,
538 Optional
<Contacts
> contacts
,
539 Optional
<Groups
> groups
,
540 Optional
<MessageRequestResponse
> messageRequestResponse
543 public static Sync
from(
544 final SignalServiceSyncMessage syncMessage
,
545 RecipientResolver recipientResolver
,
546 RecipientAddressResolver addressResolver
,
547 final AttachmentFileProvider fileProvider
549 return new Sync(syncMessage
.getSent()
550 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
551 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
552 syncMessage
.getRead()
553 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
555 syncMessage
.getViewed()
557 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
560 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
561 syncMessage
.getContacts().map(Contacts
::from
),
562 syncMessage
.getGroups().map(Groups
::from
),
563 syncMessage
.getMessageRequestResponse()
564 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
569 long expirationStartTimestamp
,
570 Optional
<RecipientAddress
> destination
,
571 Set
<RecipientAddress
> recipients
,
572 Optional
<Data
> message
,
573 Optional
<Edit
> editMessage
,
574 Optional
<Story
> story
578 SentTranscriptMessage sentMessage
,
579 RecipientResolver recipientResolver
,
580 RecipientAddressResolver addressResolver
,
581 final AttachmentFileProvider fileProvider
583 return new Sent(sentMessage
.getTimestamp(),
584 sentMessage
.getExpirationStartTimestamp(),
585 sentMessage
.getDestination()
586 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
587 .toApiRecipientAddress()),
588 sentMessage
.getRecipients()
590 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
591 .toApiRecipientAddress())
592 .collect(Collectors
.toSet()),
593 sentMessage
.getDataMessage()
594 .map(message
-> Data
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
595 sentMessage
.getEditMessage()
596 .map(message
-> Edit
.from(message
, recipientResolver
, addressResolver
, fileProvider
)),
597 sentMessage
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
)));
601 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
604 BlockedListMessage blockedListMessage
,
605 RecipientResolver recipientResolver
,
606 RecipientAddressResolver addressResolver
608 return new Blocked(blockedListMessage
.getAddresses()
610 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))
611 .toApiRecipientAddress())
612 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
616 public record Read(RecipientAddress sender
, long timestamp
) {
619 ReadMessage readMessage
,
620 RecipientResolver recipientResolver
,
621 RecipientAddressResolver addressResolver
623 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
624 .toApiRecipientAddress(), readMessage
.getTimestamp());
628 public record Viewed(RecipientAddress sender
, long timestamp
) {
631 ViewedMessage readMessage
,
632 RecipientResolver recipientResolver
,
633 RecipientAddressResolver addressResolver
635 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender()))
636 .toApiRecipientAddress(), readMessage
.getTimestamp());
640 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
642 static ViewOnceOpen
from(
643 ViewOnceOpenMessage readMessage
,
644 RecipientResolver recipientResolver
,
645 RecipientAddressResolver addressResolver
647 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
648 readMessage
.getSender())).toApiRecipientAddress(), readMessage
.getTimestamp());
652 public record Contacts(boolean isComplete
) {
654 static Contacts
from(ContactsMessage contactsMessage
) {
655 return new Contacts(contactsMessage
.isComplete());
659 public record Groups() {
661 static Groups
from(SignalServiceAttachment groupsMessage
) {
666 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
668 static MessageRequestResponse
from(
669 MessageRequestResponseMessage messageRequestResponse
,
670 RecipientResolver recipientResolver
,
671 RecipientAddressResolver addressResolver
673 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
674 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
675 messageRequestResponse
.getPerson()
676 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))
677 .toApiRecipientAddress()));
688 static Type
from(MessageRequestResponseMessage
.Type type
) {
689 return switch (type
) {
690 case UNKNOWN
-> UNKNOWN
;
691 case ACCEPT
-> ACCEPT
;
692 case DELETE
-> DELETE
;
694 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
695 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
703 Optional
<Integer
> destinationDeviceId
,
704 Optional
<GroupId
> groupId
,
705 Optional
<Long
> timestamp
,
706 Optional
<Offer
> offer
,
707 Optional
<Answer
> answer
,
708 Optional
<Hangup
> hangup
,
710 List
<IceUpdate
> iceUpdate
,
711 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
));
728 public record Offer(long id
, String sdp
, Type type
, byte[] opaque
) {
730 static Offer
from(OfferMessage offerMessage
) {
731 return new Offer(offerMessage
.getId(),
732 offerMessage
.getSdp(),
733 Type
.from(offerMessage
.getType()),
734 offerMessage
.getOpaque());
741 static Type
from(OfferMessage
.Type type
) {
742 return switch (type
) {
743 case AUDIO_CALL
-> AUDIO_CALL
;
744 case VIDEO_CALL
-> VIDEO_CALL
;
750 public record Answer(long id
, String sdp
, byte[] opaque
) {
752 static Answer
from(AnswerMessage answerMessage
) {
753 return new Answer(answerMessage
.getId(), answerMessage
.getSdp(), answerMessage
.getOpaque());
757 public record Busy(long id
) {
759 static Busy
from(BusyMessage busyMessage
) {
760 return new Busy(busyMessage
.getId());
764 public record Hangup(long id
, Type type
, int deviceId
, boolean isLegacy
) {
766 static Hangup
from(HangupMessage hangupMessage
) {
767 return new Hangup(hangupMessage
.getId(),
768 Type
.from(hangupMessage
.getType()),
769 hangupMessage
.getDeviceId(),
770 hangupMessage
.isLegacy());
780 static Type
from(HangupMessage
.Type type
) {
781 return switch (type
) {
782 case NORMAL
-> NORMAL
;
783 case ACCEPTED
-> ACCEPTED
;
784 case DECLINED
-> DECLINED
;
786 case NEED_PERMISSION
-> NEED_PERMISSION
;
792 public record IceUpdate(long id
, String sdp
, byte[] opaque
) {
794 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
795 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getSdp(), iceUpdateMessage
.getOpaque());
799 public record Opaque(byte[] opaque
, Urgency urgency
) {
801 static Opaque
from(OpaqueMessage opaqueMessage
) {
802 return new Opaque(opaqueMessage
.getOpaque(), Urgency
.from(opaqueMessage
.getUrgency()));
805 public enum Urgency
{
809 static Urgency
from(OpaqueMessage
.Urgency urgency
) {
810 return switch (urgency
) {
811 case DROPPABLE
-> DROPPABLE
;
812 case HANDLE_IMMEDIATELY
-> HANDLE_IMMEDIATELY
;
820 boolean allowsReplies
,
821 Optional
<GroupId
> groupId
,
822 Optional
<Data
.Attachment
> fileAttachment
,
823 Optional
<TextAttachment
> textAttachment
826 public static Story
from(
827 SignalServiceStoryMessage storyMessage
, final AttachmentFileProvider fileProvider
829 return new Story(storyMessage
.getAllowsReplies().orElse(false),
830 storyMessage
.getGroupContext().map(c
-> GroupUtils
.getGroupIdV2(c
.getMasterKey())),
831 storyMessage
.getFileAttachment().map(f
-> Data
.Attachment
.from(f
, fileProvider
)),
832 storyMessage
.getTextAttachment().map(t
-> TextAttachment
.from(t
, fileProvider
)));
835 public record TextAttachment(
836 Optional
<String
> text
,
837 Optional
<Style
> style
,
838 Optional
<Color
> textForegroundColor
,
839 Optional
<Color
> textBackgroundColor
,
840 Optional
<Data
.Preview
> preview
,
841 Optional
<Gradient
> backgroundGradient
,
842 Optional
<Color
> backgroundColor
845 static TextAttachment
from(
846 SignalServiceTextAttachment textAttachment
, final AttachmentFileProvider fileProvider
848 return new TextAttachment(textAttachment
.getText(),
849 textAttachment
.getStyle().map(Style
::from
),
850 textAttachment
.getTextForegroundColor().map(Color
::new),
851 textAttachment
.getTextBackgroundColor().map(Color
::new),
852 textAttachment
.getPreview().map(p
-> Data
.Preview
.from(p
, fileProvider
)),
853 textAttachment
.getBackgroundGradient().map(Gradient
::from
),
854 textAttachment
.getBackgroundColor().map(Color
::new));
865 static Style
from(SignalServiceTextAttachment
.Style style
) {
866 return switch (style
) {
867 case DEFAULT
-> DEFAULT
;
868 case REGULAR
-> REGULAR
;
871 case SCRIPT
-> SCRIPT
;
872 case CONDENSED
-> CONDENSED
;
877 public record Gradient(
878 List
<Color
> colors
, List
<Float
> positions
, Optional
<Integer
> angle
881 static Gradient
from(SignalServiceTextAttachment
.Gradient gradient
) {
882 return new Gradient(gradient
.getColors().stream().map(Color
::new).toList(),
883 gradient
.getPositions(),
884 gradient
.getAngle());
890 public static MessageEnvelope
from(
891 SignalServiceEnvelope envelope
,
892 SignalServiceContent content
,
893 RecipientResolver recipientResolver
,
894 RecipientAddressResolver addressResolver
,
895 final AttachmentFileProvider fileProvider
,
898 final var source
= !envelope
.isUnidentifiedSender() && envelope
.hasSourceUuid()
899 ? recipientResolver
.resolveRecipient(envelope
.getSourceAddress())
900 : envelope
.isUnidentifiedSender() && content
!= null
901 ? recipientResolver
.resolveRecipient(content
.getSender())
902 : exception
instanceof ProtocolException e
903 ? recipientResolver
.resolveRecipient(e
.getSender())
905 final var sourceDevice
= envelope
.hasSourceDevice()
906 ? envelope
.getSourceDevice()
908 ? content
.getSenderDevice()
909 : exception
instanceof ProtocolException e ? e
.getSenderDevice() : 0;
911 Optional
<Receipt
> receipt
;
912 Optional
<Typing
> typing
;
917 Optional
<Story
> story
;
918 if (content
!= null) {
919 receipt
= content
.getReceiptMessage().map(Receipt
::from
);
920 typing
= content
.getTypingMessage().map(Typing
::from
);
921 data
= content
.getDataMessage()
922 .map(dataMessage
-> Data
.from(dataMessage
, recipientResolver
, addressResolver
, fileProvider
));
923 edit
= content
.getEditMessage().map(s
-> Edit
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
924 sync
= content
.getSyncMessage().map(s
-> Sync
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
925 call
= content
.getCallMessage().map(Call
::from
);
926 story
= content
.getStoryMessage().map(s
-> Story
.from(s
, fileProvider
));
928 receipt
= envelope
.isReceipt() ? Optional
.of(new Receipt(envelope
.getServerReceivedTimestamp(),
929 Receipt
.Type
.DELIVERY
,
930 List
.of(envelope
.getTimestamp()))) : Optional
.empty();
931 typing
= Optional
.empty();
932 data
= Optional
.empty();
933 edit
= Optional
.empty();
934 sync
= Optional
.empty();
935 call
= Optional
.empty();
936 story
= Optional
.empty();
939 return new MessageEnvelope(source
== null
941 : Optional
.of(addressResolver
.resolveRecipientAddress(source
).toApiRecipientAddress()),
943 envelope
.getTimestamp(),
944 envelope
.getServerReceivedTimestamp(),
945 envelope
.getServerDeliveredTimestamp(),
946 envelope
.isUnidentifiedSender(),
956 public interface AttachmentFileProvider
{
958 File
getFile(SignalServiceAttachmentPointer pointer
);