Guard against large visible room list ranges

This commit is contained in:
Stefan Ceriu
2023-08-28 18:39:09 +03:00
committed by Stefan Ceriu
parent 2ccf9de9b8
commit 5f55796b7f
2 changed files with 11 additions and 2 deletions
@@ -49,6 +49,7 @@ enum ClientProxyError: Error {
enum SlidingSyncConstants {
static let defaultTimelineLimit: UInt = 20
static let maximumVisibleRangeSize = 30
}
/// This struct represents the configuration that we are using to register the application through Pusher to Sygnal
@@ -108,8 +108,16 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {
func updateVisibleRange(_ range: Range<Int>) {
Task {
do {
MXLog.info("\(name): Setting visible range to \(range)")
try await roomListService.applyInput(input: .viewport(ranges: [.init(start: UInt32(range.lowerBound), endInclusive: UInt32(range.upperBound))]))
// The scroll view content size based visible range calculations might create large ranges
// This is just a safety check to not overload the backend
var upperBound = range.upperBound
if range.upperBound - range.lowerBound > SlidingSyncConstants.maximumVisibleRangeSize {
upperBound = range.lowerBound + SlidingSyncConstants.maximumVisibleRangeSize
}
MXLog.info("\(name): Setting visible range to \(range.lowerBound)...\(upperBound)")
try await roomListService.applyInput(input: .viewport(ranges: [.init(start: UInt32(range.lowerBound), endInclusive: UInt32(upperBound))]))
} catch {
MXLog.error("Failed updating visible range with error: \(error)")
}