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
.RecipientAddress
;
7 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientResolver
;
8 import org
.signal
.libsignal
.metadata
.ProtocolException
;
9 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
10 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
11 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
12 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
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
.SignalServiceTypingMessage
;
19 import org
.whispersystems
.signalservice
.api
.messages
.calls
.AnswerMessage
;
20 import org
.whispersystems
.signalservice
.api
.messages
.calls
.BusyMessage
;
21 import org
.whispersystems
.signalservice
.api
.messages
.calls
.HangupMessage
;
22 import org
.whispersystems
.signalservice
.api
.messages
.calls
.IceUpdateMessage
;
23 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OfferMessage
;
24 import org
.whispersystems
.signalservice
.api
.messages
.calls
.OpaqueMessage
;
25 import org
.whispersystems
.signalservice
.api
.messages
.calls
.SignalServiceCallMessage
;
26 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
27 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
28 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.MessageRequestResponseMessage
;
29 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ReadMessage
;
30 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
31 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewOnceOpenMessage
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ViewedMessage
;
36 import java
.util
.List
;
37 import java
.util
.Optional
;
39 import java
.util
.stream
.Collectors
;
41 public record MessageEnvelope(
42 Optional
<RecipientAddress
> sourceAddress
,
45 long serverReceivedTimestamp
,
46 long serverDeliveredTimestamp
,
47 boolean isUnidentifiedSender
,
48 Optional
<Receipt
> receipt
,
49 Optional
<Typing
> typing
,
55 public record Receipt(long when, Type type
, List
<Long
> timestamps
) {
57 static Receipt
from(final SignalServiceReceiptMessage receiptMessage
) {
58 return new Receipt(receiptMessage
.getWhen(),
59 Type
.from(receiptMessage
.getType()),
60 receiptMessage
.getTimestamps());
69 static Type
from(SignalServiceReceiptMessage
.Type type
) {
70 return switch (type
) {
71 case DELIVERY
-> DELIVERY
;
73 case VIEWED
-> VIEWED
;
74 case UNKNOWN
-> UNKNOWN
;
80 public record Typing(long timestamp
, Type type
, Optional
<GroupId
> groupId
) {
82 public static Typing
from(final SignalServiceTypingMessage typingMessage
) {
83 return new Typing(typingMessage
.getTimestamp(),
84 typingMessage
.isTypingStarted() ? Type
.STARTED
: Type
.STOPPED
,
85 typingMessage
.getGroupId().map(GroupId
::unknownVersion
));
96 Optional
<GroupContext
> groupContext
,
97 Optional
<GroupCallUpdate
> groupCallUpdate
,
98 Optional
<String
> body
,
100 boolean isExpirationUpdate
,
102 boolean isEndSession
,
103 boolean hasProfileKey
,
104 Optional
<Reaction
> reaction
,
105 Optional
<Quote
> quote
,
106 Optional
<Payment
> payment
,
107 List
<Attachment
> attachments
,
108 Optional
<Long
> remoteDeleteId
,
109 Optional
<Sticker
> sticker
,
110 List
<SharedContact
> sharedContacts
,
111 List
<Mention
> mentions
,
112 List
<Preview
> previews
116 final SignalServiceDataMessage dataMessage
,
117 RecipientResolver recipientResolver
,
118 RecipientAddressResolver addressResolver
,
119 final AttachmentFileProvider fileProvider
121 return new Data(dataMessage
.getTimestamp(),
122 dataMessage
.getGroupContext().map(GroupContext
::from
),
123 dataMessage
.getGroupCallUpdate().map(GroupCallUpdate
::from
),
124 dataMessage
.getBody(),
125 dataMessage
.getExpiresInSeconds(),
126 dataMessage
.isExpirationUpdate(),
127 dataMessage
.isViewOnce(),
128 dataMessage
.isEndSession(),
129 dataMessage
.getProfileKey().isPresent(),
130 dataMessage
.getReaction().map(r
-> Reaction
.from(r
, recipientResolver
, addressResolver
)),
131 dataMessage
.getQuote().map(q
-> Quote
.from(q
, recipientResolver
, addressResolver
, fileProvider
)),
132 dataMessage
.getPayment().map(p
-> p
.getPaymentNotification().isPresent() ? Payment
.from(p
) : null),
133 dataMessage
.getAttachments()
134 .map(a
-> a
.stream().map(as -> Attachment
.from(as, fileProvider
)).toList())
136 dataMessage
.getRemoteDelete().map(SignalServiceDataMessage
.RemoteDelete
::getTargetSentTimestamp
),
137 dataMessage
.getSticker().map(Sticker
::from
),
138 dataMessage
.getSharedContacts()
140 .map(sharedContact
-> SharedContact
.from(sharedContact
, fileProvider
))
143 dataMessage
.getMentions()
144 .map(a
-> a
.stream().map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
)).toList())
146 dataMessage
.getPreviews()
147 .map(a
-> a
.stream().map(preview
-> Preview
.from(preview
, fileProvider
)).toList())
151 public record GroupContext(GroupId groupId
, boolean isGroupUpdate
, int revision
) {
153 static GroupContext
from(SignalServiceGroupContext groupContext
) {
154 if (groupContext
.getGroupV1().isPresent()) {
155 return new GroupContext(GroupId
.v1(groupContext
.getGroupV1().get().getGroupId()),
156 groupContext
.getGroupV1Type() == SignalServiceGroup
.Type
.UPDATE
,
158 } else if (groupContext
.getGroupV2().isPresent()) {
159 final var groupV2
= groupContext
.getGroupV2().get();
160 return new GroupContext(GroupUtils
.getGroupIdV2(groupV2
.getMasterKey()),
161 groupV2
.hasSignedGroupChange(),
162 groupV2
.getRevision());
164 throw new RuntimeException("Invalid group context state");
169 public record GroupCallUpdate(String eraId
) {
171 static GroupCallUpdate
from(SignalServiceDataMessage
.GroupCallUpdate groupCallUpdate
) {
172 return new GroupCallUpdate(groupCallUpdate
.getEraId());
176 public record Reaction(
177 long targetSentTimestamp
, RecipientAddress targetAuthor
, String emoji
, boolean isRemove
180 static Reaction
from(
181 SignalServiceDataMessage
.Reaction reaction
,
182 RecipientResolver recipientResolver
,
183 RecipientAddressResolver addressResolver
185 return new Reaction(reaction
.getTargetSentTimestamp(),
186 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(reaction
.getTargetAuthor())),
188 reaction
.isRemove());
194 RecipientAddress author
,
195 Optional
<String
> text
,
196 List
<Mention
> mentions
,
197 List
<Attachment
> attachments
201 SignalServiceDataMessage
.Quote quote
,
202 RecipientResolver recipientResolver
,
203 RecipientAddressResolver addressResolver
,
204 final AttachmentFileProvider fileProvider
206 return new Quote(quote
.getId(),
207 addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(quote
.getAuthor())),
208 Optional
.ofNullable(quote
.getText()),
209 quote
.getMentions() == null
211 : quote
.getMentions()
213 .map(m
-> Mention
.from(m
, recipientResolver
, addressResolver
))
215 quote
.getAttachments() == null
217 : quote
.getAttachments().stream().map(a
-> Attachment
.from(a
, fileProvider
)).toList());
221 public record Payment(String note
, byte[] receipt
) {
223 static Payment
from(SignalServiceDataMessage
.Payment payment
) {
224 return new Payment(payment
.getPaymentNotification().get().getNote(),
225 payment
.getPaymentNotification().get().getReceipt());
229 public record Mention(RecipientAddress recipient
, int start
, int length
) {
232 SignalServiceDataMessage
.Mention mention
,
233 RecipientResolver recipientResolver
,
234 RecipientAddressResolver addressResolver
236 return new Mention(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(mention
.getServiceId())),
238 mention
.getLength());
242 public record Attachment(
245 Optional
<String
> fileName
,
247 Optional
<Long
> uploadTimestamp
,
249 Optional
<byte[]> preview
,
250 Optional
<Attachment
> thumbnail
,
251 Optional
<String
> caption
,
252 Optional
<Integer
> width
,
253 Optional
<Integer
> height
,
259 static Attachment
from(SignalServiceAttachment attachment
, AttachmentFileProvider fileProvider
) {
260 if (attachment
.isPointer()) {
261 final var a
= attachment
.asPointer();
262 return new Attachment(Optional
.of(a
.getRemoteId().toString()),
263 Optional
.of(fileProvider
.getFile(a
.getRemoteId())),
266 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
267 a
.getSize().map(Integer
::longValue
),
271 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
272 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
277 final var a
= attachment
.asStream();
278 return new Attachment(Optional
.empty(),
282 a
.getUploadTimestamp() == 0 ? Optional
.empty() : Optional
.of(a
.getUploadTimestamp()),
283 Optional
.of(a
.getLength()),
287 a
.getWidth() == 0 ? Optional
.empty() : Optional
.of(a
.getWidth()),
288 a
.getHeight() == 0 ? Optional
.empty() : Optional
.of(a
.getHeight()),
295 static Attachment
from(
296 SignalServiceDataMessage
.Quote
.QuotedAttachment a
, final AttachmentFileProvider fileProvider
298 return new Attachment(Optional
.empty(),
300 Optional
.ofNullable(a
.getFileName()),
305 a
.getThumbnail() == null
307 : Optional
.of(Attachment
.from(a
.getThumbnail(), fileProvider
)),
317 public record Sticker(StickerPackId packId
, byte[] packKey
, int stickerId
) {
319 static Sticker
from(SignalServiceDataMessage
.Sticker sticker
) {
320 return new Sticker(StickerPackId
.deserialize(sticker
.getPackId()),
321 sticker
.getPackKey(),
322 sticker
.getStickerId());
326 public record SharedContact(
328 Optional
<Avatar
> avatar
,
331 List
<Address
> address
,
332 Optional
<String
> organization
335 static SharedContact
from(
336 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact sharedContact
,
337 final AttachmentFileProvider fileProvider
339 return new SharedContact(Name
.from(sharedContact
.getName()),
340 sharedContact
.getAvatar().map(avatar1
-> Avatar
.from(avatar1
, fileProvider
)),
341 sharedContact
.getPhone().map(p
-> p
.stream().map(Phone
::from
).toList()).orElse(List
.of()),
342 sharedContact
.getEmail().map(p
-> p
.stream().map(Email
::from
).toList()).orElse(List
.of()),
343 sharedContact
.getAddress().map(p
-> p
.stream().map(Address
::from
).toList()).orElse(List
.of()),
344 sharedContact
.getOrganization());
348 Optional
<String
> display
,
349 Optional
<String
> given
,
350 Optional
<String
> family
,
351 Optional
<String
> prefix
,
352 Optional
<String
> suffix
,
353 Optional
<String
> middle
356 static Name
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Name name
) {
357 return new Name(name
.getDisplay(),
366 public record Avatar(Attachment attachment
, boolean isProfile
) {
369 org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Avatar avatar
,
370 final AttachmentFileProvider fileProvider
372 return new Avatar(Attachment
.from(avatar
.getAttachment(), fileProvider
), avatar
.isProfile());
377 String value
, Type type
, Optional
<String
> label
380 static Phone
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone phone
) {
381 return new Phone(phone
.getValue(), Type
.from(phone
.getType()), phone
.getLabel());
390 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Phone
.Type type
) {
391 return switch (type
) {
394 case MOBILE
-> MOBILE
;
395 case CUSTOM
-> CUSTOM
;
402 String value
, Type type
, Optional
<String
> label
405 static Email
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email email
) {
406 return new Email(email
.getValue(), Type
.from(email
.getType()), email
.getLabel());
415 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.Email
.Type type
) {
416 return switch (type
) {
419 case MOBILE
-> MOBILE
;
420 case CUSTOM
-> CUSTOM
;
426 public record Address(
428 Optional
<String
> label
,
429 Optional
<String
> street
,
430 Optional
<String
> pobox
,
431 Optional
<String
> neighborhood
,
432 Optional
<String
> city
,
433 Optional
<String
> region
,
434 Optional
<String
> postcode
,
435 Optional
<String
> country
438 static Address
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress address
) {
439 return new Address(Address
.Type
.from(address
.getType()),
455 static Type
from(org
.whispersystems
.signalservice
.api
.messages
.shared
.SharedContact
.PostalAddress
.Type type
) {
456 return switch (type
) {
459 case CUSTOM
-> CUSTOM
;
466 public record Preview(String title
, String description
, long date
, String url
, Optional
<Attachment
> image
) {
469 SignalServicePreview preview
, final AttachmentFileProvider fileProvider
471 return new Preview(preview
.getTitle(),
472 preview
.getDescription(),
475 preview
.getImage().map(as -> Attachment
.from(as, fileProvider
)));
482 Optional
<Blocked
> blocked
,
485 Optional
<ViewOnceOpen
> viewOnceOpen
,
486 Optional
<Contacts
> contacts
,
487 Optional
<Groups
> groups
,
488 Optional
<MessageRequestResponse
> messageRequestResponse
491 public static Sync
from(
492 final SignalServiceSyncMessage syncMessage
,
493 RecipientResolver recipientResolver
,
494 RecipientAddressResolver addressResolver
,
495 final AttachmentFileProvider fileProvider
497 return new Sync(syncMessage
.getSent()
498 .map(s
-> Sent
.from(s
, recipientResolver
, addressResolver
, fileProvider
)),
499 syncMessage
.getBlockedList().map(b
-> Blocked
.from(b
, recipientResolver
, addressResolver
)),
500 syncMessage
.getRead()
501 .map(r
-> r
.stream().map(rm
-> Read
.from(rm
, recipientResolver
, addressResolver
)).toList())
503 syncMessage
.getViewed()
505 .map(rm
-> Viewed
.from(rm
, recipientResolver
, addressResolver
))
508 syncMessage
.getViewOnceOpen().map(rm
-> ViewOnceOpen
.from(rm
, recipientResolver
, addressResolver
)),
509 syncMessage
.getContacts().map(Contacts
::from
),
510 syncMessage
.getGroups().map(Groups
::from
),
511 syncMessage
.getMessageRequestResponse()
512 .map(m
-> MessageRequestResponse
.from(m
, recipientResolver
, addressResolver
)));
517 long expirationStartTimestamp
,
518 Optional
<RecipientAddress
> destination
,
519 Set
<RecipientAddress
> recipients
,
524 SentTranscriptMessage sentMessage
,
525 RecipientResolver recipientResolver
,
526 RecipientAddressResolver addressResolver
,
527 final AttachmentFileProvider fileProvider
529 return new Sent(sentMessage
.getTimestamp(),
530 sentMessage
.getExpirationStartTimestamp(),
531 sentMessage
.getDestination()
532 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
))),
533 sentMessage
.getRecipients()
535 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
)))
536 .collect(Collectors
.toSet()),
537 Data
.from(sentMessage
.getMessage(), recipientResolver
, addressResolver
, fileProvider
));
541 public record Blocked(List
<RecipientAddress
> recipients
, List
<GroupId
> groupIds
) {
544 BlockedListMessage blockedListMessage
,
545 RecipientResolver recipientResolver
,
546 RecipientAddressResolver addressResolver
548 return new Blocked(blockedListMessage
.getAddresses()
550 .map(d
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(d
)))
551 .toList(), blockedListMessage
.getGroupIds().stream().map(GroupId
::unknownVersion
).toList());
555 public record Read(RecipientAddress sender
, long timestamp
) {
558 ReadMessage readMessage
,
559 RecipientResolver recipientResolver
,
560 RecipientAddressResolver addressResolver
562 return new Read(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender())),
563 readMessage
.getTimestamp());
567 public record Viewed(RecipientAddress sender
, long timestamp
) {
570 ViewedMessage readMessage
,
571 RecipientResolver recipientResolver
,
572 RecipientAddressResolver addressResolver
574 return new Viewed(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(readMessage
.getSender())),
575 readMessage
.getTimestamp());
579 public record ViewOnceOpen(RecipientAddress sender
, long timestamp
) {
581 static ViewOnceOpen
from(
582 ViewOnceOpenMessage readMessage
,
583 RecipientResolver recipientResolver
,
584 RecipientAddressResolver addressResolver
586 return new ViewOnceOpen(addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(
587 readMessage
.getSender())), readMessage
.getTimestamp());
591 public record Contacts(boolean isComplete
) {
593 static Contacts
from(ContactsMessage contactsMessage
) {
594 return new Contacts(contactsMessage
.isComplete());
598 public record Groups() {
600 static Groups
from(SignalServiceAttachment groupsMessage
) {
605 public record MessageRequestResponse(Type type
, Optional
<GroupId
> groupId
, Optional
<RecipientAddress
> person
) {
607 static MessageRequestResponse
from(
608 MessageRequestResponseMessage messageRequestResponse
,
609 RecipientResolver recipientResolver
,
610 RecipientAddressResolver addressResolver
612 return new MessageRequestResponse(Type
.from(messageRequestResponse
.getType()),
613 messageRequestResponse
.getGroupId().map(GroupId
::unknownVersion
),
614 messageRequestResponse
.getPerson()
615 .map(p
-> addressResolver
.resolveRecipientAddress(recipientResolver
.resolveRecipient(p
))));
626 static Type
from(MessageRequestResponseMessage
.Type type
) {
627 return switch (type
) {
628 case UNKNOWN
-> UNKNOWN
;
629 case ACCEPT
-> ACCEPT
;
630 case DELETE
-> DELETE
;
632 case BLOCK_AND_DELETE
-> BLOCK_AND_DELETE
;
633 case UNBLOCK_AND_ACCEPT
-> UNBLOCK_AND_ACCEPT
;
641 Optional
<Integer
> destinationDeviceId
,
642 Optional
<GroupId
> groupId
,
643 Optional
<Long
> timestamp
,
644 Optional
<Offer
> offer
,
645 Optional
<Answer
> answer
,
646 Optional
<Hangup
> hangup
,
648 List
<IceUpdate
> iceUpdate
,
649 Optional
<Opaque
> opaque
652 public static Call
from(final SignalServiceCallMessage callMessage
) {
653 return new Call(callMessage
.getDestinationDeviceId(),
654 callMessage
.getGroupId().map(GroupId
::unknownVersion
),
655 callMessage
.getTimestamp(),
656 callMessage
.getOfferMessage().map(Offer
::from
),
657 callMessage
.getAnswerMessage().map(Answer
::from
),
658 callMessage
.getHangupMessage().map(Hangup
::from
),
659 callMessage
.getBusyMessage().map(Busy
::from
),
660 callMessage
.getIceUpdateMessages()
661 .map(m
-> m
.stream().map(IceUpdate
::from
).toList())
663 callMessage
.getOpaqueMessage().map(Opaque
::from
));
666 public record Offer(long id
, String sdp
, Type type
, byte[] opaque
) {
668 static Offer
from(OfferMessage offerMessage
) {
669 return new Offer(offerMessage
.getId(),
670 offerMessage
.getSdp(),
671 Type
.from(offerMessage
.getType()),
672 offerMessage
.getOpaque());
679 static Type
from(OfferMessage
.Type type
) {
680 return switch (type
) {
681 case AUDIO_CALL
-> AUDIO_CALL
;
682 case VIDEO_CALL
-> VIDEO_CALL
;
688 public record Answer(long id
, String sdp
, byte[] opaque
) {
690 static Answer
from(AnswerMessage answerMessage
) {
691 return new Answer(answerMessage
.getId(), answerMessage
.getSdp(), answerMessage
.getOpaque());
695 public record Busy(long id
) {
697 static Busy
from(BusyMessage busyMessage
) {
698 return new Busy(busyMessage
.getId());
702 public record Hangup(long id
, Type type
, int deviceId
, boolean isLegacy
) {
704 static Hangup
from(HangupMessage hangupMessage
) {
705 return new Hangup(hangupMessage
.getId(),
706 Type
.from(hangupMessage
.getType()),
707 hangupMessage
.getDeviceId(),
708 hangupMessage
.isLegacy());
718 static Type
from(HangupMessage
.Type type
) {
719 return switch (type
) {
720 case NORMAL
-> NORMAL
;
721 case ACCEPTED
-> ACCEPTED
;
722 case DECLINED
-> DECLINED
;
724 case NEED_PERMISSION
-> NEED_PERMISSION
;
730 public record IceUpdate(long id
, String sdp
, byte[] opaque
) {
732 static IceUpdate
from(IceUpdateMessage iceUpdateMessage
) {
733 return new IceUpdate(iceUpdateMessage
.getId(), iceUpdateMessage
.getSdp(), iceUpdateMessage
.getOpaque());
737 public record Opaque(byte[] opaque
, Urgency urgency
) {
739 static Opaque
from(OpaqueMessage opaqueMessage
) {
740 return new Opaque(opaqueMessage
.getOpaque(), Urgency
.from(opaqueMessage
.getUrgency()));
743 public enum Urgency
{
747 static Urgency
from(OpaqueMessage
.Urgency urgency
) {
748 return switch (urgency
) {
749 case DROPPABLE
-> DROPPABLE
;
750 case HANDLE_IMMEDIATELY
-> HANDLE_IMMEDIATELY
;
757 public static MessageEnvelope
from(
758 SignalServiceEnvelope envelope
,
759 SignalServiceContent content
,
760 RecipientResolver recipientResolver
,
761 RecipientAddressResolver addressResolver
,
762 final AttachmentFileProvider fileProvider
,
765 final var source
= !envelope
.isUnidentifiedSender() && envelope
.hasSourceUuid()
766 ? recipientResolver
.resolveRecipient(envelope
.getSourceAddress())
767 : envelope
.isUnidentifiedSender() && content
!= null
768 ? recipientResolver
.resolveRecipient(content
.getSender())
769 : exception
instanceof ProtocolException e
770 ? recipientResolver
.resolveRecipient(e
.getSender())
772 final var sourceDevice
= envelope
.hasSourceDevice()
773 ? envelope
.getSourceDevice()
775 ? content
.getSenderDevice()
776 : exception
instanceof ProtocolException e ? e
.getSenderDevice() : 0;
778 Optional
<Receipt
> receipt
;
779 Optional
<Typing
> typing
;
783 if (content
!= null) {
784 receipt
= content
.getReceiptMessage().map(Receipt
::from
);
785 typing
= content
.getTypingMessage().map(Typing
::from
);
786 data
= content
.getDataMessage()
787 .map(dataMessage
-> Data
.from(dataMessage
, recipientResolver
, addressResolver
, fileProvider
));
788 sync
= content
.getSyncMessage().map(s
-> Sync
.from(s
, recipientResolver
, addressResolver
, fileProvider
));
789 call
= content
.getCallMessage().map(Call
::from
);
791 receipt
= envelope
.isReceipt() ? Optional
.of(new Receipt(envelope
.getServerReceivedTimestamp(),
792 Receipt
.Type
.DELIVERY
,
793 List
.of(envelope
.getTimestamp()))) : Optional
.empty();
794 typing
= Optional
.empty();
795 data
= Optional
.empty();
796 sync
= Optional
.empty();
797 call
= Optional
.empty();
800 return new MessageEnvelope(source
== null
802 : Optional
.of(addressResolver
.resolveRecipientAddress(source
)),
804 envelope
.getTimestamp(),
805 envelope
.getServerReceivedTimestamp(),
806 envelope
.getServerDeliveredTimestamp(),
807 envelope
.isUnidentifiedSender(),
815 public interface AttachmentFileProvider
{
817 File
getFile(SignalServiceAttachmentRemoteId attachmentRemoteId
);