Load timeline without initial eventId if not found

Change-Id: Ifab011c7730ddd9eeeef89451dda48b3fcec341b
This commit is contained in:
SpiritCroc 2022-03-29 12:45:23 +02:00
parent 3f0920cc23
commit 39c5cb2280
2 changed files with 23 additions and 1 deletions

View File

@ -226,7 +226,15 @@ internal class DefaultTimeline(private val roomId: String,
updateState(direction) {
it.copy(loading = true)
}
val loadMoreResult = strategy.loadMore(count, direction, fetchOnServerIfNeeded)
val loadMoreResult = try {
strategy.loadMore(count, direction, fetchOnServerIfNeeded)
} catch (throwable: Throwable) {
// Timeline could not be loaded with a (likely) permanent issue, such as the
// server now knowing the initialEventId, so we want to show an error message
// and possibly restart without initialEventId.
onTimelineFailure(throwable)
return false
}
Timber.v("$baseLogMessage: result $loadMoreResult")
val hasMoreToLoad = loadMoreResult != LoadMoreResult.REACHED_END
updateState(direction) {
@ -363,6 +371,14 @@ internal class DefaultTimeline(private val roomId: String,
}
}
private fun onTimelineFailure(throwable: Throwable) {
timelineScope.launch(coroutineDispatchers.main) {
listeners.forEach {
tryOrNull { it.onTimelineFailure(throwable) }
}
}
}
private fun buildStrategy(mode: LoadTimelineStrategy.Mode): LoadTimelineStrategy {
return LoadTimelineStrategy(
roomId = roomId,

View File

@ -22,6 +22,8 @@ import io.realm.Realm
import io.realm.RealmResults
import kotlinx.coroutines.CompletableDeferred
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.api.session.room.send.SendState
import org.matrix.android.sdk.api.session.room.timeline.Timeline
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
@ -183,6 +185,10 @@ internal class LoadTimelineStrategy(
getContextLatch?.await()
getContextLatch = null
} catch (failure: Throwable) {
if (failure is Failure.ServerError && failure.error.code == MatrixError.M_NOT_FOUND) {
// This failure is likely permanent, so handle in DefaultTimeline to restart without eventId
throw failure
}
return LoadMoreResult.FAILURE
}
}