]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/actions/SendRetryMessageRequestAction.java
Fix inspections
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / actions / SendRetryMessageRequestAction.java
1 package org.asamk.signal.manager.actions;
2
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;
12
13 import java.util.Optional;
14
15 public class SendRetryMessageRequestAction implements HandleAction {
16
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;
22
23 public SendRetryMessageRequestAction(
24 final RecipientId recipientId,
25 final ServiceId serviceId,
26 final ProtocolException protocolException,
27 final SignalServiceEnvelope envelope,
28 final ServiceId accountId
29 ) {
30 this.recipientId = recipientId;
31 this.serviceId = serviceId;
32 this.protocolException = protocolException;
33 this.envelope = envelope;
34 this.accountId = accountId;
35 }
36
37 @Override
38 public void execute(Context context) throws Throwable {
39 context.getAccount().getAccountData(accountId).getSessionStore().archiveSessions(serviceId);
40
41 int senderDevice = protocolException.getSenderDevice();
42 Optional<GroupId> groupId = protocolException.getGroupId().isPresent() ? Optional.of(GroupId.unknownVersion(
43 protocolException.getGroupId().get())) : Optional.empty();
44
45 byte[] originalContent;
46 int envelopeType;
47 if (protocolException.getUnidentifiedSenderMessageContent().isPresent()) {
48 final var messageContent = protocolException.getUnidentifiedSenderMessageContent().get();
49 originalContent = messageContent.getContent();
50 envelopeType = messageContent.getType();
51 } else {
52 originalContent = envelope.getContent();
53 envelopeType = envelope.getType() == null
54 ? CiphertextMessage.WHISPER_TYPE
55 : envelopeTypeToCiphertextMessageType(envelope.getType());
56 }
57
58 DecryptionErrorMessage decryptionErrorMessage = DecryptionErrorMessage.forOriginalMessage(originalContent,
59 envelopeType,
60 envelope.getTimestamp(),
61 senderDevice);
62
63 context.getSendHelper().sendRetryReceipt(decryptionErrorMessage, recipientId, groupId);
64 }
65
66 private static int envelopeTypeToCiphertextMessageType(int envelopeType) {
67 final var type = Envelope.Type.fromValue(envelopeType);
68 if (type == null) {
69 return CiphertextMessage.WHISPER_TYPE;
70 }
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;
76 };
77 }
78
79 @Override
80 public boolean equals(final Object o) {
81 if (this == o) return true;
82 if (o == null || getClass() != o.getClass()) return false;
83
84 final SendRetryMessageRequestAction that = (SendRetryMessageRequestAction) o;
85
86 if (!recipientId.equals(that.recipientId)) return false;
87 if (!protocolException.equals(that.protocolException)) return false;
88 return envelope.equals(that.envelope);
89 }
90
91 @Override
92 public int hashCode() {
93 int result = recipientId.hashCode();
94 result = 31 * result + protocolException.hashCode();
95 result = 31 * result + envelope.hashCode();
96 return result;
97 }
98 }