mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Refactor sendMessage and extract sendSelfMessage method
This commit is contained in:
parent
1aba9f370e
commit
bfb12b0872
1 changed files with 39 additions and 27 deletions
|
@ -1110,11 +1110,10 @@ public class Manager implements Closeable {
|
||||||
}
|
}
|
||||||
SignalServiceDataMessage message = null;
|
SignalServiceDataMessage message = null;
|
||||||
try {
|
try {
|
||||||
SignalServiceMessageSender messageSender = getMessageSender();
|
|
||||||
|
|
||||||
message = messageBuilder.build();
|
message = messageBuilder.build();
|
||||||
if (message.getGroupContext().isPresent()) {
|
if (message.getGroupContext().isPresent()) {
|
||||||
try {
|
try {
|
||||||
|
SignalServiceMessageSender messageSender = getMessageSender();
|
||||||
final boolean isRecipientUpdate = false;
|
final boolean isRecipientUpdate = false;
|
||||||
List<SendMessageResult> result = messageSender.sendMessage(new ArrayList<>(recipients), getAccessFor(recipients), isRecipientUpdate, message);
|
List<SendMessageResult> result = messageSender.sendMessage(new ArrayList<>(recipients), getAccessFor(recipients), isRecipientUpdate, message);
|
||||||
for (SendMessageResult r : result) {
|
for (SendMessageResult r : result) {
|
||||||
|
@ -1127,25 +1126,6 @@ public class Manager implements Closeable {
|
||||||
account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
|
account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
} else if (recipients.size() == 1 && recipients.contains(account.getSelfAddress())) {
|
|
||||||
SignalServiceAddress recipient = account.getSelfAddress();
|
|
||||||
final Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);
|
|
||||||
SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient),
|
|
||||||
message.getTimestamp(),
|
|
||||||
message,
|
|
||||||
message.getExpiresInSeconds(),
|
|
||||||
Collections.singletonMap(recipient, unidentifiedAccess.isPresent()),
|
|
||||||
false);
|
|
||||||
SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
|
|
||||||
|
|
||||||
List<SendMessageResult> results = new ArrayList<>(recipients.size());
|
|
||||||
try {
|
|
||||||
messageSender.sendMessage(syncMessage, unidentifiedAccess);
|
|
||||||
} catch (UntrustedIdentityException e) {
|
|
||||||
account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
|
|
||||||
results.add(SendMessageResult.identityFailure(recipient, e.getIdentityKey()));
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
} else {
|
} else {
|
||||||
// Send to all individually, so sync messages are sent correctly
|
// Send to all individually, so sync messages are sent correctly
|
||||||
List<SendMessageResult> results = new ArrayList<>(recipients.size());
|
List<SendMessageResult> results = new ArrayList<>(recipients.size());
|
||||||
|
@ -1159,12 +1139,10 @@ public class Manager implements Closeable {
|
||||||
messageBuilder.withProfileKey(null);
|
messageBuilder.withProfileKey(null);
|
||||||
}
|
}
|
||||||
message = messageBuilder.build();
|
message = messageBuilder.build();
|
||||||
try {
|
if (address.matches(account.getSelfAddress())) {
|
||||||
SendMessageResult result = messageSender.sendMessage(address, getAccessFor(address), message);
|
results.add(sendSelfMessage(message));
|
||||||
results.add(result);
|
} else {
|
||||||
} catch (UntrustedIdentityException e) {
|
results.add(sendMessage(address, message));
|
||||||
account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
|
|
||||||
results.add(SendMessageResult.identityFailure(address, e.getIdentityKey()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
|
@ -1179,6 +1157,40 @@ public class Manager implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SendMessageResult sendSelfMessage(SignalServiceDataMessage message) throws IOException {
|
||||||
|
SignalServiceMessageSender messageSender = getMessageSender();
|
||||||
|
|
||||||
|
SignalServiceAddress recipient = account.getSelfAddress();
|
||||||
|
|
||||||
|
final Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);
|
||||||
|
SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient),
|
||||||
|
message.getTimestamp(),
|
||||||
|
message,
|
||||||
|
message.getExpiresInSeconds(),
|
||||||
|
Collections.singletonMap(recipient, unidentifiedAccess.isPresent()),
|
||||||
|
false);
|
||||||
|
SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
|
||||||
|
|
||||||
|
try {
|
||||||
|
messageSender.sendMessage(syncMessage, unidentifiedAccess);
|
||||||
|
return SendMessageResult.success(recipient, unidentifiedAccess.isPresent(), false);
|
||||||
|
} catch (UntrustedIdentityException e) {
|
||||||
|
account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
|
||||||
|
return SendMessageResult.identityFailure(recipient, e.getIdentityKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SendMessageResult sendMessage(SignalServiceAddress address, SignalServiceDataMessage message) throws IOException {
|
||||||
|
SignalServiceMessageSender messageSender = getMessageSender();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return messageSender.sendMessage(address, getAccessFor(address), message);
|
||||||
|
} catch (UntrustedIdentityException e) {
|
||||||
|
account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
|
||||||
|
return SendMessageResult.identityFailure(address, e.getIdentityKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, org.whispersystems.libsignal.UntrustedIdentityException {
|
private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, org.whispersystems.libsignal.UntrustedIdentityException {
|
||||||
SignalServiceCipher cipher = new SignalServiceCipher(account.getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator());
|
SignalServiceCipher cipher = new SignalServiceCipher(account.getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator());
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue