Only retry messages after identity was trusted

Fixes #1477
This commit is contained in:
AsamK 2024-02-27 17:52:21 +01:00
parent 189b21dbde
commit 2e4cd0eddc
3 changed files with 21 additions and 16 deletions

View file

@ -41,7 +41,6 @@ public class ReceiveHelper {
private final Context context;
private ReceiveConfig receiveConfig = new ReceiveConfig(false, false, false);
private boolean needsToRetryFailedMessages = false;
private boolean hasCaughtUpWithOldMessages = false;
private boolean isWaitingForMessage = false;
private boolean shouldStop = false;
@ -59,10 +58,6 @@ public class ReceiveHelper {
dependencies.setAllowStories(!receiveConfig.ignoreStories());
}
public void setNeedsToRetryFailedMessages(final boolean needsToRetryFailedMessages) {
this.needsToRetryFailedMessages = needsToRetryFailedMessages;
}
public void setAuthenticationFailureListener(final Callable authenticationFailureListener) {
this.authenticationFailureListener = authenticationFailureListener;
}
@ -90,7 +85,7 @@ public class ReceiveHelper {
public void receiveMessages(
Duration timeout, boolean returnOnTimeout, Integer maxMessages, Manager.ReceiveMessageHandler handler
) throws IOException {
needsToRetryFailedMessages = true;
account.setNeedsToRetryFailedMessages(true);
hasCaughtUpWithOldMessages = false;
// Use a Map here because java Set doesn't have a get method ...
@ -130,9 +125,8 @@ public class ReceiveHelper {
isWaitingForMessage = false;
while (!shouldStop && remainingMessages != 0) {
if (needsToRetryFailedMessages) {
if (account.getNeedsToRetryFailedMessages()) {
retryFailedReceivedMessages(handler);
needsToRetryFailedMessages = false;
}
SignalServiceEnvelope envelope;
final CachedMessage[] cachedMessage = {null};
@ -266,6 +260,7 @@ public class ReceiveHelper {
}
}
handleQueuedActions(queuedActions);
account.setNeedsToRetryFailedMessages(false);
}
private List<HandleAction> retryFailedReceivedMessage(
@ -282,8 +277,8 @@ public class ReceiveHelper {
final var exception = result.second();
if (exception instanceof UntrustedIdentityException) {
if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
// Envelope is more than a month old, cleaning up.
if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 14) {
// Envelope is more than two weeks old, cleaning up.
cachedMessage.delete();
return null;
}

View file

@ -409,8 +409,7 @@ public class ManagerImpl implements Manager {
@Override
public void submitRateLimitRecaptchaChallenge(
String challenge,
String captcha
String challenge, String captcha
) throws IOException, CaptchaRejectedException {
captcha = captcha == null ? null : captcha.replace("signalcaptcha://", "");
@ -1389,7 +1388,7 @@ public class ManagerImpl implements Manager {
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
final var updated = trustMethod.apply(recipientId);
if (updated && this.isReceiving()) {
context.getReceiveHelper().setNeedsToRetryFailedMessages(true);
account.setNeedsToRetryFailedMessages(true);
}
return updated;
}

View file

@ -149,6 +149,9 @@ public class SignalAccount implements Closeable {
private final KeyValueEntry<Long> lastReceiveTimestamp = new KeyValueEntry<>("last-receive-timestamp",
long.class,
0L);
private final KeyValueEntry<Boolean> needsToRetryFailedMessages = new KeyValueEntry<>("retry-failed-messages",
Boolean.class,
true);
private final KeyValueEntry<byte[]> cdsiToken = new KeyValueEntry<>("cdsi-token", byte[].class);
private final KeyValueEntry<Long> lastRecipientsRefresh = new KeyValueEntry<>("last-recipients-refresh",
long.class);
@ -297,7 +300,7 @@ public class SignalAccount implements Closeable {
this.pniAccountData.setIdentityKeyPair(pniIdentity);
this.registered = false;
this.isMultiDevice = true;
getKeyValueStore().storeEntry(lastReceiveTimestamp, 0L);
setLastReceiveTimestamp(0L);
this.pinMasterKey = masterKey;
getKeyValueStore().storeEntry(storageManifestVersion, -1L);
this.setStorageManifest(null);
@ -342,7 +345,7 @@ public class SignalAccount implements Closeable {
this.pniAccountData.setServiceId(pni);
init();
this.registrationLockPin = pin;
getKeyValueStore().storeEntry(lastReceiveTimestamp, 0L);
setLastReceiveTimestamp(0L);
save();
setPreKeys(ServiceIdType.ACI, aciPreKeys);
@ -590,7 +593,7 @@ public class SignalAccount implements Closeable {
isMultiDevice = rootNode.get("isMultiDevice").asBoolean();
}
if (rootNode.hasNonNull("lastReceiveTimestamp")) {
getKeyValueStore().storeEntry(lastReceiveTimestamp, rootNode.get("lastReceiveTimestamp").asLong());
setLastReceiveTimestamp(rootNode.get("lastReceiveTimestamp").asLong());
}
int registrationId = 0;
if (rootNode.hasNonNull("registrationId")) {
@ -1650,6 +1653,14 @@ public class SignalAccount implements Closeable {
getKeyValueStore().storeEntry(lastReceiveTimestamp, value);
}
public void setNeedsToRetryFailedMessages(final boolean value) {
getKeyValueStore().storeEntry(needsToRetryFailedMessages, value);
}
public boolean getNeedsToRetryFailedMessages() {
return getKeyValueStore().getEntry(needsToRetryFailedMessages);
}
public boolean isUnrestrictedUnidentifiedAccess() {
return Boolean.TRUE.equals(getKeyValueStore().getEntry(unrestrictedUnidentifiedAccess));
}