Improve robustness in receiving messages

This commit is contained in:
AsamK 2023-09-16 12:00:55 +02:00
parent e5aa10a730
commit a7744e837c
2 changed files with 48 additions and 36 deletions

View file

@ -137,13 +137,17 @@ public final class IncomingMessageHandler {
final Manager.ReceiveMessageHandler handler
) {
final var actions = new ArrayList<HandleAction>();
if (envelope.hasSourceServiceId()) {
// Store uuid if we don't have it already
// uuid in envelope is sent by server
account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress());
}
SignalServiceContent content = null;
Exception exception = null;
try {
if (envelope.hasSourceServiceId()) {
// Store uuid if we don't have it already
// uuid in envelope is sent by server
account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress());
}
} catch (Exception e) {
exception = e;
}
if (!envelope.isReceipt()) {
try {
final var cipherResult = dependencies.getCipher()

View file

@ -205,41 +205,49 @@ public class ReceiveHelper {
backOffCounter = 0;
if (returnOnTimeout) return;
continue;
} catch (Exception e) {
logger.error("Unknown error when receiving messages", e);
continue;
}
final var result = context.getIncomingMessageHandler().handleEnvelope(envelope, receiveConfig, handler);
for (final var h : result.first()) {
final var existingAction = queuedActions.get(h);
if (existingAction == null) {
queuedActions.put(h, h);
} else {
existingAction.mergeOther(h);
}
}
final var exception = result.second();
if (hasCaughtUpWithOldMessages) {
handleQueuedActions(queuedActions.keySet());
queuedActions.clear();
}
if (cachedMessage[0] != null) {
if (exception instanceof UntrustedIdentityException) {
logger.debug("Keeping message with untrusted identity in message cache");
final var address = ((UntrustedIdentityException) exception).getSender();
if (!envelope.hasSourceServiceId() && address.uuid().isPresent()) {
final var recipientId = account.getRecipientResolver()
.resolveRecipient(ACI.from(address.uuid().get()));
try {
cachedMessage[0] = account.getMessageCache().replaceSender(cachedMessage[0], recipientId);
} catch (IOException ioException) {
logger.warn("Failed to move cached message to recipient folder: {}",
ioException.getMessage(),
ioException);
}
try {
final var result = context.getIncomingMessageHandler().handleEnvelope(envelope, receiveConfig, handler);
for (final var h : result.first()) {
final var existingAction = queuedActions.get(h);
if (existingAction == null) {
queuedActions.put(h, h);
} else {
existingAction.mergeOther(h);
}
} else {
cachedMessage[0].delete();
}
final var exception = result.second();
if (hasCaughtUpWithOldMessages) {
handleQueuedActions(queuedActions.keySet());
queuedActions.clear();
}
if (cachedMessage[0] != null) {
if (exception instanceof UntrustedIdentityException) {
logger.debug("Keeping message with untrusted identity in message cache");
final var address = ((UntrustedIdentityException) exception).getSender();
if (!envelope.hasSourceServiceId() && address.uuid().isPresent()) {
final var recipientId = account.getRecipientResolver()
.resolveRecipient(ACI.from(address.uuid().get()));
try {
cachedMessage[0] = account.getMessageCache()
.replaceSender(cachedMessage[0], recipientId);
} catch (IOException ioException) {
logger.warn("Failed to move cached message to recipient folder: {}",
ioException.getMessage(),
ioException);
}
}
} else {
cachedMessage[0].delete();
}
}
} catch (Exception e) {
logger.error("Unknown error when handling messages", e);
}
}
}