diff --git a/app/src/main/java/org/thoughtcrime/securesms/calls/log/CallEventCache.kt b/app/src/main/java/org/thoughtcrime/securesms/calls/log/CallEventCache.kt index 6ce291ed55..8c3986b6f1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/calls/log/CallEventCache.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/calls/log/CallEventCache.kt @@ -40,6 +40,7 @@ import org.thoughtcrime.securesms.recipients.Recipient import org.thoughtcrime.securesms.recipients.RecipientId import org.whispersystems.signalservice.api.groupsv2.DecryptedGroupUtil import java.util.concurrent.Executor +import kotlin.math.abs import kotlin.math.max import kotlin.math.min import kotlin.time.Duration.Companion.hours @@ -137,7 +138,7 @@ class CallEventCache( } private fun isWithinTimeout(parent: CacheRecord, child: CacheRecord): Boolean { - return (child.timestamp - parent.timestamp) <= 4.hours.inWholeMilliseconds + return abs(parent.timestamp - child.timestamp) <= 4.hours.inWholeMilliseconds } private fun canUserBeginCall(peer: Recipient, decryptedGroup: ByteArray?): CallLogRow.CanStartCall { diff --git a/app/src/test/java/org/thoughtcrime/securesms/calls/log/CallEventCacheTest.kt b/app/src/test/java/org/thoughtcrime/securesms/calls/log/CallEventCacheTest.kt index a322fc4662..c208e7a799 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/calls/log/CallEventCacheTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/calls/log/CallEventCacheTest.kt @@ -22,6 +22,7 @@ import org.thoughtcrime.securesms.recipients.RecipientId import org.thoughtcrime.securesms.service.webrtc.CallLinkPeekInfo import org.thoughtcrime.securesms.service.webrtc.SignalCallManager import kotlin.time.Duration.Companion.days +import kotlin.time.Duration.Companion.hours class CallEventCacheTest { @@ -154,6 +155,42 @@ class CallEventCacheTest { assertThat(result).size().isEqualTo(2) } + @Test + fun `Given two entries in descending timestamp order outside of time threshold, when I clusterCallEvents, then I expect two entries`() { + val testData = listOf( + createCacheRecord( + callId = 1, + timestamp = 1.days.inWholeMilliseconds + ), + createCacheRecord( + callId = 2, + timestamp = 0 + ) + ) + + val filterState = CallEventCache.FilterState() + val result = CallEventCache.clusterCallEvents(testData, filterState) + assertThat(result).size().isEqualTo(2) + } + + @Test + fun `Given two entries in descending timestamp order within time threshold, when I clusterCallEvents, then I expect one entry`() { + val testData = listOf( + createCacheRecord( + callId = 1, + timestamp = 1.hours.inWholeMilliseconds + ), + createCacheRecord( + callId = 2, + timestamp = 0 + ) + ) + + val filterState = CallEventCache.FilterState() + val result = CallEventCache.clusterCallEvents(testData, filterState) + assertThat(result).size().isEqualTo(1) + } + @Test fun `Given two entries with a mismatch between them, when I clusterCallEvents, then I expect three entries`() { val testData = listOf(