1 package org
.asamk
.signal
.manager
.actions
;
3 import org
.asamk
.signal
.manager
.api
.GroupId
;
4 import org
.asamk
.signal
.manager
.helper
.Context
;
5 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
6 import org
.signal
.libsignal
.metadata
.ProtocolException
;
7 import org
.signal
.libsignal
.protocol
.message
.CiphertextMessage
;
8 import org
.signal
.libsignal
.protocol
.message
.DecryptionErrorMessage
;
9 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
10 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
11 import org
.whispersystems
.signalservice
.internal
.push
.Envelope
;
13 import java
.util
.Optional
;
15 public class SendRetryMessageRequestAction
implements HandleAction
{
17 private final RecipientId recipientId
;
18 private final ServiceId serviceId
;
19 private final ProtocolException protocolException
;
20 private final SignalServiceEnvelope envelope
;
21 private final ServiceId accountId
;
23 public SendRetryMessageRequestAction(
24 final RecipientId recipientId
,
25 final ServiceId serviceId
,
26 final ProtocolException protocolException
,
27 final SignalServiceEnvelope envelope
,
28 final ServiceId accountId
30 this.recipientId
= recipientId
;
31 this.serviceId
= serviceId
;
32 this.protocolException
= protocolException
;
33 this.envelope
= envelope
;
34 this.accountId
= accountId
;
38 public void execute(Context context
) throws Throwable
{
39 context
.getAccount().getAccountData(accountId
).getSessionStore().archiveSessions(serviceId
);
41 int senderDevice
= protocolException
.getSenderDevice();
42 Optional
<GroupId
> groupId
= protocolException
.getGroupId().isPresent() ? Optional
.of(GroupId
.unknownVersion(
43 protocolException
.getGroupId().get())) : Optional
.empty();
45 byte[] originalContent
;
47 if (protocolException
.getUnidentifiedSenderMessageContent().isPresent()) {
48 final var messageContent
= protocolException
.getUnidentifiedSenderMessageContent().get();
49 originalContent
= messageContent
.getContent();
50 envelopeType
= messageContent
.getType();
52 originalContent
= envelope
.getContent();
53 envelopeType
= envelope
.getType() == null
54 ? CiphertextMessage
.WHISPER_TYPE
55 : envelopeTypeToCiphertextMessageType(envelope
.getType());
58 DecryptionErrorMessage decryptionErrorMessage
= DecryptionErrorMessage
.forOriginalMessage(originalContent
,
60 envelope
.getTimestamp(),
63 context
.getSendHelper().sendRetryReceipt(decryptionErrorMessage
, recipientId
, groupId
);
66 private static int envelopeTypeToCiphertextMessageType(int envelopeType
) {
67 final var type
= Envelope
.Type
.fromValue(envelopeType
);
69 return CiphertextMessage
.WHISPER_TYPE
;
71 return switch (type
) {
72 case PREKEY_BUNDLE
-> CiphertextMessage
.PREKEY_TYPE
;
73 case UNIDENTIFIED_SENDER
-> CiphertextMessage
.SENDERKEY_TYPE
;
74 case PLAINTEXT_CONTENT
-> CiphertextMessage
.PLAINTEXT_CONTENT_TYPE
;
75 default -> CiphertextMessage
.WHISPER_TYPE
;
80 public boolean equals(final Object o
) {
81 if (this == o
) return true;
82 if (o
== null || getClass() != o
.getClass()) return false;
84 final SendRetryMessageRequestAction that
= (SendRetryMessageRequestAction
) o
;
86 if (!recipientId
.equals(that
.recipientId
)) return false;
87 if (!protocolException
.equals(that
.protocolException
)) return false;
88 return envelope
.equals(that
.envelope
);
92 public int hashCode() {
93 int result
= recipientId
.hashCode();
94 result
= 31 * result
+ protocolException
.hashCode();
95 result
= 31 * result
+ envelope
.hashCode();