Move the pinned timeline in line with the other timeline creation methods and have it report errors just like the others

This commit is contained in:
Stefan Ceriu
2025-05-21 22:16:03 +03:00
committed by Stefan Ceriu
parent 909aa086f2
commit 250a7ed451
13 changed files with 180 additions and 121 deletions
@@ -11,12 +11,14 @@ import MatrixRustSDK
class BannedRoomProxy: BannedRoomProxyProtocol {
private let roomListItem: RoomListItemProtocol
private let roomPreview: RoomPreviewProtocol
let info: BaseRoomInfoProxyProtocol
let ownUserID: String
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id = info.id
let ownUserID: String
let info: BaseRoomInfoProxyProtocol
init(roomListItem: RoomListItemProtocol,
roomPreview: RoomPreviewProtocol,
@@ -12,13 +12,15 @@ import UIKit
class InvitedRoomProxy: InvitedRoomProxyProtocol {
private let roomListItem: RoomListItemProtocol
private let roomPreview: RoomPreviewProtocol
let info: BaseRoomInfoProxyProtocol
let ownUserID: String
let inviter: RoomMemberProxyProtocol?
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id: String = info.id
let ownUserID: String
let info: BaseRoomInfoProxyProtocol
let inviter: RoomMemberProxyProtocol?
init(roomListItem: RoomListItemProtocol,
roomPreview: RoomPreviewProtocol,
@@ -14,50 +14,6 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
private let roomListService: RoomListServiceProtocol
private let roomListItem: RoomListItemProtocol
private let room: RoomProtocol
let timeline: TimelineProxyProtocol
private var innerPinnedEventsTimeline: TimelineProxyProtocol?
private var innerPinnedEventsTimelineTask: Task<TimelineProxyProtocol?, Never>?
var pinnedEventsTimeline: TimelineProxyProtocol? {
get async {
// Check if is already available.
if let innerPinnedEventsTimeline {
return innerPinnedEventsTimeline
// Otherwise check if there is already a task loading it, and wait for it.
} else if let innerPinnedEventsTimelineTask,
let value = await innerPinnedEventsTimelineTask.value {
return value
// Else create and store a new task to load it and wait for it.
} else {
let task = Task<TimelineProxyProtocol?, Never> { [weak self] in
guard let self else {
return nil
}
do {
let sdkTimeline = try await room.timelineWithConfiguration(configuration: .init(focus: .pinnedEvents(maxEventsToLoad: 100, maxConcurrentRequests: 10),
filter: .all,
internalIdPrefix: nil,
dateDividerMode: .daily,
trackReadReceipts: false,
reportUtds: true))
let timeline = TimelineProxy(timeline: sdkTimeline, kind: .pinned)
await timeline.subscribeForUpdates()
innerPinnedEventsTimeline = timeline
return timeline
} catch {
MXLog.error("Failed creating pinned events timeline with error: \(error)")
return nil
}
}
innerPinnedEventsTimelineTask = task
return await task.value
}
}
}
// periphery:ignore - required for instance retention in the rust codebase
private var roomInfoObservationToken: TaskHandle?
@@ -68,8 +24,19 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
// periphery:ignore - required for instance retention in the rust codebase
private var knockRequestsChangesObservationToken: TaskHandle?
private var innerPinnedEventsTimeline: TimelineProxyProtocol?
private var innerPinnedEventsTimelineTask: Task<Result<TimelineProxyProtocol, RoomProxyError>, Never>?
private var subscribedForUpdates = false
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id: String = room.id()
var ownUserID: String { room.ownUserId() }
let timeline: TimelineProxyProtocol
private let infoSubject: CurrentValueSubject<RoomInfoProxy, Never>
var infoPublisher: CurrentValuePublisher<RoomInfoProxy, Never> {
infoSubject.asCurrentValuePublisher()
@@ -95,12 +62,6 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
knockRequestsStateSubject.asCurrentValuePublisher()
}
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id: String = room.id()
var ownUserID: String { room.ownUserId() }
var info: RoomInfoProxy { infoSubject.value }
init(roomListService: RoomListServiceProtocol,
roomListItem: RoomListItemProtocol,
room: RoomProtocol) async throws {
@@ -111,7 +72,7 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
infoSubject = try await .init(RoomInfoProxy(roomInfo: room.roomInfo()))
timeline = try await TimelineProxy(timeline: room.timelineWithConfiguration(configuration: .init(focus: .live,
filter: .eventTypeFilter(filter: eventFilters),
filter: .eventTypeFilter(filter: excludedEventsFilter),
internalIdPrefix: nil,
dateDividerMode: .daily,
trackReadReceipts: true,
@@ -131,24 +92,6 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
}
}
private let eventFilters: TimelineEventTypeFilter = {
var stateEventFilters: [StateEventType] = [.roomAliases,
.roomCanonicalAlias,
.roomGuestAccess,
.roomHistoryVisibility,
.roomJoinRules,
.roomPinnedEvents,
.roomPowerLevels,
.roomServerAcl,
.roomTombstone,
.spaceChild,
.spaceParent,
.policyRuleRoom,
.policyRuleServer,
.policyRuleUser]
return .exclude(eventTypes: stateEventFilters.map { FilterTimelineEventType.state(eventType: $0) })
}()
func subscribeForUpdates() async {
guard !subscribedForUpdates else {
MXLog.warning("Room already subscribed for updates")
@@ -273,6 +216,43 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
}
}
func pinnedEventsTimeline() async -> Result<TimelineProxyProtocol, RoomProxyError> {
// Check if is already available.
if let innerPinnedEventsTimeline {
return .success(innerPinnedEventsTimeline)
// Otherwise check if there is already a task loading it, and wait for it.
} else if let innerPinnedEventsTimelineTask {
return await innerPinnedEventsTimelineTask.value
} else { // Else create and store a new task to load it and wait for it.
let task = Task<Result<TimelineProxyProtocol, RoomProxyError>, Never> { [weak self] in
guard let self else {
return .failure(.failedCreatingPinnedTimeline)
}
do {
let sdkTimeline = try await room.timelineWithConfiguration(configuration: .init(focus: .pinnedEvents(maxEventsToLoad: 100, maxConcurrentRequests: 10),
filter: .all,
internalIdPrefix: nil,
dateDividerMode: .daily,
trackReadReceipts: false,
reportUtds: true))
let timeline = TimelineProxy(timeline: sdkTimeline, kind: .pinned)
await timeline.subscribeForUpdates()
innerPinnedEventsTimeline = timeline
return .success(timeline)
} catch {
MXLog.error("Failed creating pinned events timeline with error: \(error)")
return .failure(.sdkError(error))
}
}
innerPinnedEventsTimelineTask = task
return await task.value
}
}
func enableEncryption() async -> Result<Void, RoomProxyError> {
do {
try await room.enableEncryption()
@@ -856,6 +836,24 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
MXLog.error("Failed observing requests to join with error: \(error)")
}
}
private let excludedEventsFilter: TimelineEventTypeFilter = {
var stateEventFilters: [StateEventType] = [.roomAliases,
.roomCanonicalAlias,
.roomGuestAccess,
.roomHistoryVisibility,
.roomJoinRules,
.roomPinnedEvents,
.roomPowerLevels,
.roomServerAcl,
.roomTombstone,
.spaceChild,
.spaceParent,
.policyRuleRoom,
.policyRuleServer,
.policyRuleUser]
return .exclude(eventTypes: stateEventFilters.map { FilterTimelineEventType.state(eventType: $0) })
}()
}
private final class RoomTypingNotificationUpdateListener: TypingNotificationsListener {
@@ -11,12 +11,14 @@ import MatrixRustSDK
class KnockedRoomProxy: KnockedRoomProxyProtocol {
private let roomListItem: RoomListItemProtocol
private let roomPreview: RoomPreviewProtocol
let info: BaseRoomInfoProxyProtocol
let ownUserID: String
// A room identifier is constant and lazy stops it from being fetched
// multiple times over FFI
lazy var id = info.id
let ownUserID: String
let info: BaseRoomInfoProxyProtocol
init(roomListItem: RoomListItemProtocol,
roomPreview: RoomPreviewProtocol,
@@ -16,6 +16,7 @@ enum RoomProxyError: Error {
case invalidMedia
case eventNotFound
case missingTransactionID
case failedCreatingPinnedTimeline
}
/// An enum that describes the relationship between the current user and the room, and contains a reference to the specific implementation of the `RoomProxy`.
@@ -75,8 +76,6 @@ protocol JoinedRoomProxyProtocol: RoomProxyProtocol {
var timeline: TimelineProxyProtocol { get }
var pinnedEventsTimeline: TimelineProxyProtocol? { get async }
func subscribeForUpdates() async
func subscribeToRoomInfoUpdates()
@@ -89,6 +88,8 @@ protocol JoinedRoomProxyProtocol: RoomProxyProtocol {
allowedMessageTypes: [TimelineAllowedMessageType],
presentation: TimelineKind.MediaPresentation) async -> Result<TimelineProxyProtocol, RoomProxyError>
func pinnedEventsTimeline() async -> Result<TimelineProxyProtocol, RoomProxyError>
func enableEncryption() async -> Result<Void, RoomProxyError>
func redact(_ eventID: String) async -> Result<Void, RoomProxyError>