Skip to content

Commit

Permalink
Switch the new bulk room subscription API
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Aug 9, 2024
1 parent 65bfd84 commit 4617d0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
11 changes: 9 additions & 2 deletions ElementX/Sources/Services/Client/ClientProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -475,7 +481,8 @@ class ClientProxy: ClientProxyProtocol {
return nil
}

return await RoomProxy(roomListItem: roomListItem,
return await RoomProxy(roomListService: roomListService,
roomListItem: roomListItem,
room: room)
}

Expand Down
13 changes: 10 additions & 3 deletions ElementX/Sources/Services/Room/RoomProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

Expand Down

0 comments on commit 4617d0c

Please sign in to comment.