Skip to content

Commit

Permalink
Restore DecryptionFailureTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Jul 30, 2024
1 parent 986f817 commit 1718c59
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.d/1088.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correction du crash au lancement de l'application sur Android 5 et 6.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ private const val MAX_WAIT_MILLIS = 60_000
class DecryptionFailureTracker @Inject constructor(
private val analyticsTracker: AnalyticsTracker,
private val sessionDataSource: ActiveSessionDataSource,
private val decryptionFailurePersistence: ReportedDecryptionFailurePersistence,
private val clock: Clock
) : Session.Listener, LiveEventListener {

Expand All @@ -77,6 +76,9 @@ class DecryptionFailureTracker @Inject constructor(
// Only accessed on a `post` call, ensuring sequential access
private val trackedEventsMap = mutableMapOf<String, DecryptionFailure>()

// List of eventId that have been reported, to avoid double reporting
private val alreadyReported = mutableListOf<String>()

// Mutex to ensure sequential access to internal state
private val mutex = Mutex()

Expand All @@ -96,16 +98,10 @@ class DecryptionFailureTracker @Inject constructor(
this.scope = scope
}
observeActiveSession()
post {
decryptionFailurePersistence.load()
}
}

fun stop() {
Timber.v("Stop DecryptionFailureTracker")
post {
decryptionFailurePersistence.persist()
}
activeSessionSourceDisposable.cancel(CancellationException("Closing DecryptionFailureTracker"))

activeSession?.removeListener(this)
Expand All @@ -127,7 +123,6 @@ class DecryptionFailureTracker @Inject constructor(
delay(CHECK_INTERVAL)
post {
checkFailures()
decryptionFailurePersistence.persist()
currentTicker = null
if (trackedEventsMap.isNotEmpty()) {
// Reschedule
Expand All @@ -137,19 +132,19 @@ class DecryptionFailureTracker @Inject constructor(
}
}
private fun observeActiveSession() {
activeSessionSourceDisposable = sessionDataSource.stream()
activeSessionSourceDisposable = sessionDataSource.stream()
.distinctUntilChanged()
.onEach {
Timber.v("Active session changed ${it.getOrNull()?.myUserId}")
it.getOrNull()?.let { session ->
it.orNull()?.let { session ->
post {
onSessionActive(session)
}
}
}.launchIn(scope)
}

private suspend fun onSessionActive(session: Session) {
private fun onSessionActive(session: Session) {
Timber.v("onSessionActive ${session.myUserId} previous: ${activeSession?.myUserId}")
val sessionId = session.sessionId
if (sessionId == activeSession?.sessionId) {
Expand Down Expand Up @@ -206,8 +201,7 @@ class DecryptionFailureTracker @Inject constructor(
// already tracked
return
}
if (decryptionFailurePersistence.hasBeenReported(eventId)) {
Timber.v("Event $eventId already reported")
if (alreadyReported.contains(eventId)) {
// already reported
return
}
Expand Down Expand Up @@ -242,7 +236,7 @@ class DecryptionFailureTracker @Inject constructor(
}
}

private suspend fun handleEventDecrypted(eventId: String) {
private fun handleEventDecrypted(eventId: String) {
Timber.v("Handle event decrypted $eventId time: ${clock.epochMillis()}")
// Only consider if it was tracked as a failure
val trackedFailure = trackedEventsMap[eventId] ?: return
Expand All @@ -264,7 +258,7 @@ class DecryptionFailureTracker @Inject constructor(
}
}

fun utdDisplayedInTimeline(event: TimelineEvent) {
fun utdDisplayedInTimeline(event: TimelineEvent) {
post {
// should be tracked (unless already reported)
val eventId = event.root.eventId ?: return@post
Expand All @@ -275,7 +269,7 @@ class DecryptionFailureTracker @Inject constructor(
}

// This will mutate the trackedEventsMap, so don't call it while iterating on it.
private suspend fun reportFailure(decryptionFailure: DecryptionFailure) {
private fun reportFailure(decryptionFailure: DecryptionFailure) {
Timber.v("Report failure for event ${decryptionFailure.failedEventId}")
val error = decryptionFailure.toAnalyticsEvent()

Expand All @@ -284,10 +278,10 @@ class DecryptionFailureTracker @Inject constructor(
// now remove from tracked
trackedEventsMap.remove(decryptionFailure.failedEventId)
// mark as already reported
decryptionFailurePersistence.markAsReported(decryptionFailure.failedEventId)
alreadyReported.add(decryptionFailure.failedEventId)
}

private suspend fun checkFailures() {
private fun checkFailures() {
val now = clock.epochMillis()
Timber.v("Check failures now $now")
// report the definitely failed
Expand Down

0 comments on commit 1718c59

Please sign in to comment.