Skip to content

Commit e7c5ed8

Browse files
committed
Code review + reduce grace period
1 parent 56a4ecf commit e7c5ed8

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmDecryption.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ internal class MXMegolmDecryption(private val userId: String,
136136

137137
throw MXCryptoError.Base(
138138
MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX,
139-
throwable.olmException.message ?: "",
140-
throwable.olmException.message)
139+
"UNKNOWN_MESSAGE_INDEX",
140+
null)
141141
}
142142

143143
val reason = String.format(MXCryptoError.OLM_REASON, throwable.olmException.message)

vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package im.vector.app.features.analytics
1818

1919
import im.vector.app.core.flow.tickerFlow
20+
import im.vector.app.core.time.Clock
2021
import im.vector.app.features.analytics.plan.Error
2122
import kotlinx.coroutines.CoroutineScope
2223
import kotlinx.coroutines.Dispatchers
@@ -26,17 +27,19 @@ import kotlinx.coroutines.flow.launchIn
2627
import kotlinx.coroutines.flow.onEach
2728
import kotlinx.coroutines.launch
2829
import org.matrix.android.sdk.api.session.crypto.MXCryptoError
30+
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
2931
import javax.inject.Inject
3032
import javax.inject.Singleton
3133

32-
data class DecryptionFailure(
33-
val timeStamp: Long = System.currentTimeMillis(),
34+
private data class DecryptionFailure(
35+
val timeStamp: Long,
3436
val roomId: String,
3537
val failedEventId: String,
3638
val error: MXCryptoError.ErrorType
3739
)
3840

39-
private const val GRACE_PERIOD_MILLIS = 20_000
41+
private const val GRACE_PERIOD_MILLIS = 4_000
42+
private const val CHECK_INTERVAL = 2_000L
4043

4144
/**
4245
* Tracks decryption errors that are visible to the user.
@@ -45,7 +48,8 @@ private const val GRACE_PERIOD_MILLIS = 20_000
4548
*/
4649
@Singleton
4750
class DecryptionFailureTracker @Inject constructor(
48-
private val vectorAnalytics: VectorAnalytics
51+
private val vectorAnalytics: VectorAnalytics,
52+
private val clock: Clock
4953
) {
5054

5155
private val scope: CoroutineScope = CoroutineScope(SupervisorJob())
@@ -57,7 +61,7 @@ class DecryptionFailureTracker @Inject constructor(
5761
}
5862

5963
fun start() {
60-
tickerFlow(scope, 5_000)
64+
tickerFlow(scope, CHECK_INTERVAL)
6165
.onEach {
6266
checkFailures()
6367
}.launchIn(scope)
@@ -67,12 +71,13 @@ class DecryptionFailureTracker @Inject constructor(
6771
scope.cancel()
6872
}
6973

70-
fun e2eEventDisplayedInTimeline(roomId: String, eventId: String, error: MXCryptoError.ErrorType?) {
74+
fun e2eEventDisplayedInTimeline(event: TimelineEvent) {
7175
scope.launch(Dispatchers.Default) {
72-
if (error != null) {
73-
addDecryptionFailure(DecryptionFailure(roomId = roomId, failedEventId = eventId, error = error))
76+
val mCryptoError = event.root.mCryptoError
77+
if (mCryptoError != null) {
78+
addDecryptionFailure(DecryptionFailure(clock.epochMillis(), event.roomId, event.eventId, mCryptoError))
7479
} else {
75-
removeFailureForEventId(eventId)
80+
removeFailureForEventId(event.eventId)
7681
}
7782
}
7883
}
@@ -92,7 +97,7 @@ class DecryptionFailureTracker @Inject constructor(
9297
private fun addDecryptionFailure(failure: DecryptionFailure) {
9398
// de duplicate
9499
synchronized(failures) {
95-
if (failures.indexOfFirst { it.failedEventId == failure.failedEventId } == -1) {
100+
if (failures.none { it.failedEventId == failure.failedEventId }) {
96101
failures.add(failure)
97102
}
98103
}
@@ -105,7 +110,7 @@ class DecryptionFailureTracker @Inject constructor(
105110
}
106111

107112
private fun checkFailures() {
108-
val now = System.currentTimeMillis()
113+
val now = clock.epochMillis()
109114
val aggregatedErrors: Map<Error.Name, List<String>>
110115
synchronized(failures) {
111116
val toReport = mutableListOf<DecryptionFailure>()

vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class TimelineItemFactory @Inject constructor(private val messageItemFactory: Me
126126
}
127127
}.also {
128128
if (it != null && event.isEncrypted()) {
129-
decryptionFailureTracker.e2eEventDisplayedInTimeline(event.roomId, event.eventId, event.root.mCryptoError)
129+
decryptionFailureTracker.e2eEventDisplayedInTimeline(event)
130130
}
131131
}
132132
}

0 commit comments

Comments
 (0)