-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a more generic system for handling early messages.
- Loading branch information
1 parent
2afb939
commit 83f6640
Showing
8 changed files
with
123 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/org/thoughtcrime/securesms/util/EarlyMessageCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.thoughtcrime.securesms.util; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.thoughtcrime.securesms.recipients.RecipientId; | ||
import org.whispersystems.libsignal.util.guava.Optional; | ||
import org.whispersystems.signalservice.api.messages.SignalServiceContent; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Sometimes a message that is referencing another message can arrive out of order. In these cases, | ||
* we want to temporarily hold on (i.e. keep a memory cache) to these messages and apply them after | ||
* we receive the referenced message. | ||
*/ | ||
public final class EarlyMessageCache { | ||
|
||
private final LRUCache<MessageId, 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); | ||
} | ||
|
||
/** | ||
* 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) { | ||
return Optional.fromNullable(cache.remove(new MessageId(sender, sentTimestamp))); | ||
} | ||
|
||
private static final class MessageId { | ||
private final RecipientId sender; | ||
private final long sentTimestamp; | ||
|
||
private MessageId(@NonNull RecipientId sender, long sentTimestamp) { | ||
this.sender = sender; | ||
this.sentTimestamp = sentTimestamp; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
MessageId messageId = (MessageId) o; | ||
return sentTimestamp == messageId.sentTimestamp && | ||
Objects.equals(sender, messageId.sender); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(sentTimestamp, sender); | ||
} | ||
} | ||
} |