Make early receipts work in group messages

For both conversation item view and message details view

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2017-10-02 14:54:55 -07:00
parent 856a4d2860
commit 2c1337b33e
6 changed files with 101 additions and 89 deletions

View File

@@ -1,56 +1,45 @@
package org.thoughtcrime.securesms.database;
import android.support.annotation.NonNull;
import android.util.Log;
import org.thoughtcrime.securesms.util.LRUCache;
import java.util.HashMap;
import java.util.Map;
public class EarlyReceiptCache {
private static final String TAG = EarlyReceiptCache.class.getSimpleName();
private final LRUCache<Placeholder, Long> cache = new LRUCache<>(100);
private final LRUCache<Long, Map<Address, Long>> cache = new LRUCache<>(100);
public synchronized void increment(long timestamp, Address address) {
public synchronized void increment(long timestamp, Address origin) {
Log.w(TAG, this+"");
Log.w(TAG, String.format("Early receipt: %d,%s", timestamp, address));
Placeholder tuple = new Placeholder(timestamp, address);
Long count = cache.get(tuple);
Log.w(TAG, String.format("Early receipt: (%d, %s)", timestamp, origin.serialize()));
Map<Address, Long> receipts = cache.get(timestamp);
if (receipts == null) {
receipts = new HashMap<>();
}
Long count = receipts.get(origin);
if (count != null) {
cache.put(tuple, ++count);
receipts.put(origin, ++count);
} else {
cache.put(tuple, 1L);
receipts.put(origin, 1L);
}
cache.put(timestamp, receipts);
}
public synchronized long remove(long timestamp, Address address) {
Long count = cache.remove(new Placeholder(timestamp, address));
public synchronized Map<Address, Long> remove(long timestamp) {
Map<Address, Long> receipts = cache.remove(timestamp);
Log.w(TAG, this+"");
Log.w(TAG, String.format("Checking early receipts (%d, %s): %d", timestamp, address, count));
return count != null ? count : 0;
}
Log.w(TAG, String.format("Checking early receipts (%d): %d", timestamp, receipts == null ? 0 : receipts.size()));
private class Placeholder {
private final long timestamp;
private final @NonNull Address address;
private Placeholder(long timestamp, @NonNull Address address) {
this.timestamp = timestamp;
this.address = address;
}
@Override
public boolean equals(Object other) {
return other != null && other instanceof Placeholder &&
((Placeholder)other).timestamp == this.timestamp &&
((Placeholder)other).address.equals(this.address);
}
@Override
public int hashCode() {
return (int)timestamp ^ address.hashCode();
}
return receipts != null ? receipts : new HashMap<>();
}
}