Pin/Unpin Logic (#3084)

This commit is contained in:
Mauro
2024-07-26 20:41:00 +02:00
committed by GitHub
parent 5a4d21ab7a
commit 0da2d4917b
24 changed files with 659 additions and 69 deletions
+16 -2
View File
@@ -16,9 +16,8 @@
import Combine
import Foundation
import UIKit
import MatrixRustSDK
import UIKit
class RoomProxy: RoomProxyProtocol {
private let roomListItem: RoomListItemProtocol
@@ -87,6 +86,12 @@ class RoomProxy: RoomProxyProtocol {
}
}
var pinnedEventIDs: [String] {
get async {
await (try? room.roomInfo().pinnedEventIds) ?? []
}
}
var hasOngoingCall: Bool {
room.hasActiveRoomCall()
}
@@ -493,6 +498,15 @@ class RoomProxy: RoomProxyProtocol {
}
}
func canUserPinOrUnpin(userID: String) async -> Result<Bool, RoomProxyError> {
do {
return try await .success(room.canUserPinUnpin(userId: userID))
} catch {
MXLog.error("Failed checking if the user can pin or unnpin: \(error)")
return .failure(.sdkError(error))
}
}
// MARK: - Moderation
func kickUser(_ userID: String) async -> Result<Void, RoomProxyError> {
@@ -38,6 +38,7 @@ protocol RoomProxyProtocol {
var isSpace: Bool { get }
var isEncrypted: Bool { get }
var isFavourite: Bool { get async }
var pinnedEventIDs: [String] { get async }
var membership: Membership { get }
var hasOngoingCall: Bool { get }
var canonicalAlias: String? { get }
@@ -121,6 +122,7 @@ protocol RoomProxyProtocol {
func canUserKick(userID: String) async -> Result<Bool, RoomProxyError>
func canUserBan(userID: String) async -> Result<Bool, RoomProxyError>
func canUserTriggerRoomNotification(userID: String) async -> Result<Bool, RoomProxyError>
func canUserPinOrUnpin(userID: String) async -> Result<Bool, RoomProxyError>
// MARK: - Moderation
@@ -100,6 +100,10 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
func redact(_ itemID: TimelineItemIdentifier) async { }
func pin(eventID: String) async { }
func unpin(eventID: String) async { }
func messageEventContent(for itemID: TimelineItemIdentifier) -> RoomMessageEventContentWithoutRelation? {
.init(noPointer: .init())
}
@@ -237,6 +237,36 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
}
}
func pin(eventID: String) async {
MXLog.info("Pinning event \(eventID) in \(roomID)")
switch await activeTimeline.pin(eventID: eventID) {
case .success(let value):
if value {
MXLog.info("Finished pinning event \(eventID)")
} else {
MXLog.error("Failed pinning event \(eventID) because is already pinned")
}
case .failure(let error):
MXLog.error("Failed pinning event \(eventID) with error: \(error)")
}
}
func unpin(eventID: String) async {
MXLog.info("Unpinning event \(eventID) in \(roomID)")
switch await activeTimeline.unpin(eventID: eventID) {
case .success(let value):
if value {
MXLog.info("Finished unpinning event \(eventID)")
} else {
MXLog.error("Failed unpinning event \(eventID) because is not pinned")
}
case .failure(let error):
MXLog.error("Failed unpinning event \(eventID) with error: \(error)")
}
}
func messageEventContent(for timelineItemID: TimelineItemIdentifier) async -> RoomMessageEventContentWithoutRelation? {
await activeTimeline.messageEventContent(for: timelineItemID)
}
@@ -68,6 +68,10 @@ protocol RoomTimelineControllerProtocol {
func redact(_ itemID: TimelineItemIdentifier) async
func pin(eventID: String) async
func unpin(eventID: String) async
func messageEventContent(for itemID: TimelineItemIdentifier) async -> RoomMessageEventContentWithoutRelation?
func debugInfo(for itemID: TimelineItemIdentifier) -> TimelineItemDebugInfo
@@ -202,6 +202,24 @@ final class TimelineProxy: TimelineProxyProtocol {
}
}
func pin(eventID: String) async -> Result<Bool, TimelineProxyError> {
do {
return try await .success(timeline.pinEvent(eventId: eventID))
} catch {
MXLog.error("Failed to pin the event \(eventID) with error: \(error)")
return .failure(.sdkError(error))
}
}
func unpin(eventID: String) async -> Result<Bool, TimelineProxyError> {
do {
return try await .success(timeline.unpinEvent(eventId: eventID))
} catch {
MXLog.error("Failed to unpin the event \(eventID) with error: \(error)")
return .failure(.sdkError(error))
}
}
// MARK: - Sending
func sendAudio(url: URL,
@@ -46,6 +46,10 @@ protocol TimelineProxyProtocol {
func redact(_ timelineItemID: TimelineItemIdentifier,
reason: String?) async -> Result<Void, TimelineProxyError>
func pin(eventID: String) async -> Result<Bool, TimelineProxyError>
func unpin(eventID: String) async -> Result<Bool, TimelineProxyError>
// MARK: - Sending
func sendAudio(url: URL,