Skip to content

Commit

Permalink
Fix processing of early messages.
Browse files Browse the repository at this point in the history
1. Eliminated any possibility of infinite recursion.
2. Handle the fact that you can have multiple 'early contents' for a
   single message.
  • Loading branch information
greyson-signal committed May 9, 2020
1 parent 618b1b5 commit a83ccc1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ public void onRun() {
if (messageState == MessageState.DECRYPTED_OK) {
SignalServiceContent content = SignalServiceContent.deserialize(serializedPlaintextContent);
handleMessage(content, optionalSmsMessageId);

Optional<List<SignalServiceContent>> earlyContent = ApplicationDependencies.getEarlyMessageCache()
.retrieve(Recipient.externalPush(context, content.getSender()).getId(),
content.getTimestamp());
if (earlyContent.isPresent()) {
Log.i(TAG, "Found " + earlyContent.get().size() + " dependent item(s) that were retrieved earlier. Processing.");

for (SignalServiceContent earlyItem : earlyContent.get()) {
handleMessage(earlyItem, Optional.absent());
}
}
} else {
//noinspection ConstantConditions
handleExceptionMessage(exceptionMetadata, optionalSmsMessageId);
Expand Down Expand Up @@ -332,14 +343,6 @@ private void handleMessage(@Nullable SignalServiceContent content, @NonNull Opti
}

resetRecipientToPush(Recipient.externalPush(context, content.getSender()));

Optional<SignalServiceContent> earlyContent = ApplicationDependencies.getEarlyMessageCache()
.retrieve(Recipient.externalPush(context, content.getSender()).getId(),
content.getTimestamp());
if (earlyContent.isPresent()) {
Log.i(TAG, "Found dependent content that was retrieved earlier. Processing.");
handleMessage(earlyContent.get(), Optional.absent());
}
} catch (StorageFailedException e) {
Log.w(TAG, e);
handleCorruptMessage(e.getSender(), e.getSenderDevice(), timestamp, smsMessageId);
Expand Down Expand Up @@ -1363,6 +1366,7 @@ private void handleReadReceipt(@NonNull SignalServiceContent content,
.incrementReadReceiptCount(id, content.getTimestamp());

if (!handled) {
Log.w(TAG, "[handleReadReceipt] Could not find matching message! timestamp: " + timestamp + " author: " + sender.getId());
ApplicationDependencies.getEarlyMessageCache().store(sender.getId(), timestamp, content);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceContent;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -15,22 +17,31 @@
*/
public final class EarlyMessageCache {

private final LRUCache<MessageId, SignalServiceContent> cache = new LRUCache<>(100);
private final LRUCache<MessageId, List<SignalServiceContent>> cache = new LRUCache<>(100);

/**
* @param targetSender The sender of the message this message depends on.
* @param targetSentTimestamp The sent timestamp of the message this message depends on.
*/
public void store(@NonNull RecipientId targetSender, long targetSentTimestamp, @NonNull SignalServiceContent content) {
cache.put(new MessageId(targetSender, targetSentTimestamp), content);
MessageId messageId = new MessageId(targetSender, targetSentTimestamp);
List<SignalServiceContent> contentList = cache.get(messageId);

if (contentList == null) {
contentList = new LinkedList<>();
}

contentList.add(content);

cache.put(messageId, contentList);
}

/**
* Returns and removes any content that is dependent on the provided message id.
* @param sender The sender of the message in question.
* @param sentTimestamp The sent timestamp of the message in question.
*/
public Optional<SignalServiceContent> retrieve(@NonNull RecipientId sender, long sentTimestamp) {
public Optional<List<SignalServiceContent>> retrieve(@NonNull RecipientId sender, long sentTimestamp) {
return Optional.fromNullable(cache.remove(new MessageId(sender, sentTimestamp)));
}

Expand Down

0 comments on commit a83ccc1

Please sign in to comment.