Skip to content

Commit

Permalink
basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Jul 14, 2023
1 parent bf91e97 commit bb33b35
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
26 changes: 26 additions & 0 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,11 @@ class RoomProxyMock: RoomProxyProtocol {
set(value) { underlyingHasUnreadNotifications = value }
}
var underlyingHasUnreadNotifications: Bool!
var currentUserID: String {
get { return underlyingCurrentUserID }
set(value) { underlyingCurrentUserID = value }
}
var underlyingCurrentUserID: String!
var name: String?
var displayName: String?
var topic: String?
Expand Down Expand Up @@ -1360,6 +1365,27 @@ class RoomProxyMock: RoomProxyProtocol {
return uploadAvatarMediaReturnValue
}
}
//MARK: - canUserRedact

var canUserRedactUserIDCallsCount = 0
var canUserRedactUserIDCalled: Bool {
return canUserRedactUserIDCallsCount > 0
}
var canUserRedactUserIDReceivedUserID: String?
var canUserRedactUserIDReceivedInvocations: [String] = []
var canUserRedactUserIDReturnValue: Result<Bool, RoomProxyError>!
var canUserRedactUserIDClosure: ((String) async -> Result<Bool, RoomProxyError>)?

func canUserRedact(userID: String) async -> Result<Bool, RoomProxyError> {
canUserRedactUserIDCallsCount += 1
canUserRedactUserIDReceivedUserID = userID
canUserRedactUserIDReceivedInvocations.append(userID)
if let canUserRedactUserIDClosure = canUserRedactUserIDClosure {
return await canUserRedactUserIDClosure(userID)
} else {
return canUserRedactUserIDReturnValue
}
}
}
class RoomTimelineProviderMock: RoomTimelineProviderProtocol {
var updatePublisher: AnyPublisher<TimelineProviderUpdate, Never> {
Expand Down
21 changes: 18 additions & 3 deletions ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
private let analytics: AnalyticsService
private unowned let userIndicatorController: UserIndicatorControllerProtocol
private let notificationCenterProtocol: NotificationCenterProtocol

private var canCurrentUserRedact = false

init(timelineController: RoomTimelineControllerProtocol,
mediaProvider: MediaProviderProtocol,
Expand Down Expand Up @@ -104,7 +106,14 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
case .markRoomAsRead:
Task { await markRoomAsRead() }
case .timelineItemMenu(let itemID):
showTimelineItemActionMenu(for: itemID)
Task {
if case let .success(value) = await roomProxy.canUserRedact(userID: roomProxy.currentUserID) {
canCurrentUserRedact = value
} else {
canCurrentUserRedact = false
}
showTimelineItemActionMenu(for: itemID)
}
case .timelineItemMenuAction(let itemID, let action):
processTimelineItemMenuAction(action, itemID: itemID)
case .displayCameraPicker:
Expand Down Expand Up @@ -408,9 +417,11 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol

actions.append(.copyPermalink)

if item.isOutgoing {
if canRedactItem(item) {
actions.append(.redact)
} else {
}

if !item.isOutgoing {
actions.append(.report)
}

Expand All @@ -424,6 +435,10 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol

return .init(actions: actions, debugActions: debugActions)
}

private func canRedactItem(_ item: EventBasedTimelineItemProtocol) -> Bool {
item.isOutgoing || (canCurrentUserRedact && !roomProxy.isDirect)
}

// swiftlint:disable:next cyclomatic_complexity function_body_length
private func processTimelineItemMenuAction(_ action: TimelineItemMenuAction, itemID: TimelineItemIdentifier) {
Expand Down
13 changes: 13 additions & 0 deletions ElementX/Sources/Services/Room/RoomProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class RoomProxy: RoomProxyProtocol {
innerTimelineProvider
}

var currentUserID: String {
room.currentUserId()
}

deinit {
roomTimelineObservationToken?.cancel()
backPaginationStateObservationToken?.cancel()
Expand Down Expand Up @@ -647,6 +651,15 @@ class RoomProxy: RoomProxyProtocol {
}
}

func canUserRedact(userID: String) async -> Result<Bool, RoomProxyError> {
do {
return try await .success(room.canUserRedact(userId: userID))
} catch {
MXLog.error("Failed checking if the user can redact with error: \(error)")
return .failure(.failedCheckingPermission)
}
}

// MARK: - Private

/// Force the timeline to load member details so it can populate sender profiles whenever we add a timeline listener
Expand Down
4 changes: 4 additions & 0 deletions ElementX/Sources/Services/Room/RoomProxyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum RoomProxyError: Error, Equatable {
case failedSettingRoomTopic
case failedRemovingAvatar
case failedUploadingAvatar
case failedCheckingPermission
}

// sourcery: AutoMockable
Expand All @@ -54,6 +55,7 @@ protocol RoomProxyProtocol {
var canonicalAlias: String? { get }
var alternativeAliases: [String] { get }
var hasUnreadNotifications: Bool { get }
var currentUserID: String { get }

var name: String? { get }
var displayName: String? { get }
Expand Down Expand Up @@ -159,6 +161,8 @@ protocol RoomProxyProtocol {
func removeAvatar() async -> Result<Void, RoomProxyError>

func uploadAvatar(media: MediaInfo) async -> Result<Void, RoomProxyError>

func canUserRedact(userID: String) async -> Result<Bool, RoomProxyError>
}

extension RoomProxyProtocol {
Expand Down

0 comments on commit bb33b35

Please sign in to comment.