From 5f55796b7f59568fd16a33ac27f7e4917d86262e Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 28 Aug 2023 18:39:09 +0300 Subject: [PATCH] Guard against large visible room list ranges --- .../Services/Client/ClientProxyProtocol.swift | 1 + .../Room/RoomSummary/RoomSummaryProvider.swift | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ElementX/Sources/Services/Client/ClientProxyProtocol.swift b/ElementX/Sources/Services/Client/ClientProxyProtocol.swift index 1e5ba50f6..77c914159 100644 --- a/ElementX/Sources/Services/Client/ClientProxyProtocol.swift +++ b/ElementX/Sources/Services/Client/ClientProxyProtocol.swift @@ -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 diff --git a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift index fb27470d9..5a6cc4af5 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift @@ -108,8 +108,16 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { func updateVisibleRange(_ range: Range) { 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)") }