From 4617d0c2551fd524fcf93447de9d3fea35a2274d Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Fri, 9 Aug 2024 13:10:01 +0300 Subject: [PATCH] Switch the new bulk room subscription API --- .../Sources/Services/Client/ClientProxy.swift | 11 +++++++++-- ElementX/Sources/Services/Room/RoomProxy.swift | 13 ++++++++++--- .../Room/RoomSummary/RoomSummaryProvider.swift | 18 +++++++++++------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/ElementX/Sources/Services/Client/ClientProxy.swift b/ElementX/Sources/Services/Client/ClientProxy.swift index 313b1e7cc9..a44da83625 100644 --- a/ElementX/Sources/Services/Client/ClientProxy.swift +++ b/ElementX/Sources/Services/Client/ClientProxy.swift @@ -444,11 +444,17 @@ class ClientProxy: ClientProxyProtocol { } func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol? { + guard let roomListService else { + MXLog.error("Failed retrieving room, room list service not set up") + return nil + } + // Try fetching the room from the cold cache (if available) first var (roomListItem, room) = await roomTupleForIdentifier(identifier) if let roomListItem, let room { - return await RoomProxy(roomListItem: roomListItem, + return await RoomProxy(roomListService: roomListService, + roomListItem: roomListItem, room: room) } @@ -475,7 +481,8 @@ class ClientProxy: ClientProxyProtocol { return nil } - return await RoomProxy(roomListItem: roomListItem, + return await RoomProxy(roomListService: roomListService, + roomListItem: roomListItem, room: room) } diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index 86e5c58fbf..16eb1bb891 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -20,6 +20,7 @@ import MatrixRustSDK import UIKit class RoomProxy: RoomProxyProtocol { + private let roomListService: RoomListServiceProtocol private let roomListItem: RoomListItemProtocol private let room: RoomProtocol let timeline: TimelineProxyProtocol @@ -171,9 +172,11 @@ class RoomProxy: RoomProxyProtocol { var activeMembersCount: Int { Int(room.activeMembersCount()) } - - init?(roomListItem: RoomListItemProtocol, + + init?(roomListService: RoomListServiceProtocol, + roomListItem: RoomListItemProtocol, room: RoomProtocol) async { + self.roomListService = roomListService self.roomListItem = roomListItem self.room = room @@ -199,7 +202,11 @@ class RoomProxy: RoomProxyProtocol { let settings = RoomSubscription(requiredState: SlidingSyncConstants.defaultRequiredState, timelineLimit: SlidingSyncConstants.defaultTimelineLimit, includeHeroes: false) // We don't need heroes here as they're already included in the `all_rooms` list - roomListItem.subscribe(settings: settings) + do { + try roomListService.subscribeToRooms(roomIds: [id], settings: settings) + } catch { + MXLog.error("Failed subscribing to room with error: \(error)") + } await timeline.subscribeForUpdates() diff --git a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift index 541d85c0c9..cc57715d9a 100644 --- a/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift +++ b/ElementX/Sources/Services/Room/RoomSummary/RoomSummaryProvider.swift @@ -134,13 +134,17 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol { MXLog.info("\(name): Requesting subscriptions for visible range: \(range)") - for index in range { - guard index < rooms.count else { return } - // Note, we must use the item received by the diff. Asking the roomListService to get - // a new item instance will result in /sync being called when already subscribed. - rooms[index].roomListItem.subscribe(settings: .init(requiredState: SlidingSyncConstants.defaultRequiredState, - timelineLimit: SlidingSyncConstants.defaultTimelineLimit, - includeHeroes: false)) + let roomIDs = range + .filter { $0 < rooms.count } + .map { rooms[$0].id } + + do { + try roomListService.subscribeToRooms(roomIds: roomIDs, + settings: .init(requiredState: SlidingSyncConstants.defaultRequiredState, + timelineLimit: SlidingSyncConstants.defaultTimelineLimit, + includeHeroes: false)) + } catch { + MXLog.error("Failed subscribing to rooms with error: \(error)") } }