Skip to content

Commit e461651

Browse files
authored
Use the timeline when marking a room as read (#4319)
* Forward JoinedRoomProxy.markAsRead to the timeline The room version was building a fresh timeline even though we already had one. * Run swiftformat 0.57.0.
1 parent 53774e2 commit e461651

File tree

8 files changed

+98
-11
lines changed

8 files changed

+98
-11
lines changed

ElementX/Sources/Mocks/Generated/GeneratedMocks.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17520,6 +17520,76 @@ class TimelineProxyMock: TimelineProxyProtocol, @unchecked Sendable {
1752017520
return sendReadReceiptForTypeReturnValue
1752117521
}
1752217522
}
17523+
//MARK: - markAsRead
17524+
17525+
var markAsReadReceiptTypeUnderlyingCallsCount = 0
17526+
var markAsReadReceiptTypeCallsCount: Int {
17527+
get {
17528+
if Thread.isMainThread {
17529+
return markAsReadReceiptTypeUnderlyingCallsCount
17530+
} else {
17531+
var returnValue: Int? = nil
17532+
DispatchQueue.main.sync {
17533+
returnValue = markAsReadReceiptTypeUnderlyingCallsCount
17534+
}
17535+
17536+
return returnValue!
17537+
}
17538+
}
17539+
set {
17540+
if Thread.isMainThread {
17541+
markAsReadReceiptTypeUnderlyingCallsCount = newValue
17542+
} else {
17543+
DispatchQueue.main.sync {
17544+
markAsReadReceiptTypeUnderlyingCallsCount = newValue
17545+
}
17546+
}
17547+
}
17548+
}
17549+
var markAsReadReceiptTypeCalled: Bool {
17550+
return markAsReadReceiptTypeCallsCount > 0
17551+
}
17552+
var markAsReadReceiptTypeReceivedReceiptType: ReceiptType?
17553+
var markAsReadReceiptTypeReceivedInvocations: [ReceiptType] = []
17554+
17555+
var markAsReadReceiptTypeUnderlyingReturnValue: Result<Void, TimelineProxyError>!
17556+
var markAsReadReceiptTypeReturnValue: Result<Void, TimelineProxyError>! {
17557+
get {
17558+
if Thread.isMainThread {
17559+
return markAsReadReceiptTypeUnderlyingReturnValue
17560+
} else {
17561+
var returnValue: Result<Void, TimelineProxyError>? = nil
17562+
DispatchQueue.main.sync {
17563+
returnValue = markAsReadReceiptTypeUnderlyingReturnValue
17564+
}
17565+
17566+
return returnValue!
17567+
}
17568+
}
17569+
set {
17570+
if Thread.isMainThread {
17571+
markAsReadReceiptTypeUnderlyingReturnValue = newValue
17572+
} else {
17573+
DispatchQueue.main.sync {
17574+
markAsReadReceiptTypeUnderlyingReturnValue = newValue
17575+
}
17576+
}
17577+
}
17578+
}
17579+
var markAsReadReceiptTypeClosure: ((ReceiptType) async -> Result<Void, TimelineProxyError>)?
17580+
17581+
func markAsRead(receiptType: ReceiptType) async -> Result<Void, TimelineProxyError> {
17582+
markAsReadReceiptTypeCallsCount += 1
17583+
markAsReadReceiptTypeReceivedReceiptType = receiptType
17584+
DispatchQueue.main.async {
17585+
self.markAsReadReceiptTypeReceivedInvocations.append(receiptType)
17586+
}
17587+
if let markAsReadReceiptTypeClosure = markAsReadReceiptTypeClosure {
17588+
return await markAsReadReceiptTypeClosure(receiptType)
17589+
} else {
17590+
return markAsReadReceiptTypeReturnValue
17591+
}
17592+
}
1752317593
//MARK: - sendMessageEventContent
1752417594

1752517595
var sendMessageEventContentUnderlyingCallsCount = 0

ElementX/Sources/Screens/MediaPickerScreen/DocumentPicker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct DocumentPicker: UIViewControllerRepresentable {
6969
}
7070

7171
do {
72-
let _ = url.startAccessingSecurityScopedResource()
72+
_ = url.startAccessingSecurityScopedResource()
7373
let newURL = try FileManager.default.copyFileToTemporaryDirectory(file: url)
7474
url.stopAccessingSecurityScopedResource()
7575
documentPicker.callback(.selectFile(newURL))

ElementX/Sources/Screens/MediaPickerScreen/PhotoLibraryPicker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct PhotoLibraryPicker: UIViewControllerRepresentable {
7878
}
7979

8080
do {
81-
let _ = url.startAccessingSecurityScopedResource()
81+
_ = url.startAccessingSecurityScopedResource()
8282
let newURL = try FileManager.default.copyFileToTemporaryDirectory(file: url)
8383
url.stopAccessingSecurityScopedResource()
8484

ElementX/Sources/Services/Room/JoinedRoomProxy.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,16 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
402402
return .failure(.sdkError(error))
403403
}
404404
}
405-
405+
406406
func markAsRead(receiptType: ReceiptType) async -> Result<Void, RoomProxyError> {
407-
do {
408-
try await room.markAsRead(receiptType: receiptType)
409-
return .success(())
410-
} catch {
411-
MXLog.error("Failed marking room \(id) as read with error: \(error)")
412-
return .failure(.sdkError(error))
407+
// Defer to the timeline here as room.markAsRead will build a fresh timeline.
408+
switch await timeline.markAsRead(receiptType: receiptType) {
409+
case .success:
410+
.success(())
411+
case .failure(.sdkError(let error)):
412+
.failure(.sdkError(error))
413+
case .failure(let error):
414+
.failure(.timelineError(error))
413415
}
414416
}
415417

ElementX/Sources/Services/Room/RoomProxyProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum RoomProxyError: Error {
1717
case eventNotFound
1818
case missingTransactionID
1919
case failedCreatingPinnedTimeline
20+
case timelineError(TimelineProxyError)
2021
}
2122

2223
/// An enum that describes the relationship between the current user and the room, and contains a reference to the specific implementation of the `RoomProxy`.

ElementX/Sources/Services/Timeline/TimelineProxy.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,19 @@ final class TimelineProxy: TimelineProxyProtocol {
514514
}
515515
}
516516

517+
func markAsRead(receiptType: ReceiptType) async -> Result<Void, TimelineProxyError> {
518+
MXLog.info("Marking as \(receiptType)")
519+
520+
do {
521+
try await timeline.markAsRead(receiptType: receiptType)
522+
MXLog.info("Finished marking as read")
523+
return .success(())
524+
} catch {
525+
MXLog.error("Failed marking as \(receiptType) with error: \(error)")
526+
return .failure(.sdkError(error))
527+
}
528+
}
529+
517530
func toggleReaction(_ reaction: String, to eventOrTransactionID: TimelineItemIdentifier.EventOrTransactionID) async -> Result<Void, TimelineProxyError> {
518531
MXLog.info("Toggling reaction \(reaction) for event: \(eventOrTransactionID)")
519532

ElementX/Sources/Services/Timeline/TimelineProxyProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ protocol TimelineProxyProtocol {
119119
requestHandle: @MainActor (SendAttachmentJoinHandleProtocol) -> Void) async -> Result<Void, TimelineProxyError>
120120

121121
func sendReadReceipt(for eventID: String, type: ReceiptType) async -> Result<Void, TimelineProxyError>
122+
func markAsRead(receiptType: ReceiptType) async -> Result<Void, TimelineProxyError>
122123

123124
func sendMessageEventContent(_ messageContent: RoomMessageEventContentWithoutRelation) async -> Result<Void, TimelineProxyError>
124125

UnitTests/Sources/RestorationTokenTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ enum SlidingSyncVersionV1: Equatable {
182182
}
183183

184184
extension SessionV1: Codable {
185-
public init(from decoder: Decoder) throws {
185+
init(from decoder: Decoder) throws {
186186
let container = try decoder.container(keyedBy: CodingKeys.self)
187187
let slidingSyncProxy = try container.decodeIfPresent(String.self, forKey: .slidingSyncProxy)
188188
self = try .init(accessToken: container.decode(String.self, forKey: .accessToken),
@@ -194,7 +194,7 @@ extension SessionV1: Codable {
194194
slidingSyncVersion: slidingSyncProxy.map { .proxy(url: $0) } ?? .native)
195195
}
196196

197-
public func encode(to encoder: Encoder) throws {
197+
func encode(to encoder: Encoder) throws {
198198
var container = encoder.container(keyedBy: CodingKeys.self)
199199
try container.encode(accessToken, forKey: .accessToken)
200200
try container.encode(refreshToken, forKey: .refreshToken)

0 commit comments

Comments
 (0)