Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP2: Adding Rooms to Spaces #5230 #1366

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions MatrixSDK/Space/MXSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,48 @@ public class MXSpace: NSObject {
return childRoomIds.contains(roomId)
}

/// Check if the current user has enough power level to add room to this space
/// - Parameters:
/// - completion: A closure called when the operation completes.
/// - canAddRoom: Indicates wether the user has right or not to add rooms to this space
public func canAddRoom(completion: @escaping (_ canAddRoom: Bool) -> Void) {
guard let userId = session.myUserId else {
MXLog.warning("[MXSpace] canAddRoom: user ID not found")
completion(false)
return
}

guard let summary = self.summary else {
MXLog.warning("[MXSpace] canAddRoom: summary not found")
completion(false)
return
}

guard let room = self.room else {
MXLog.warning("[MXSpace] canAddRoom: room not found")
completion(false)
return
}

guard summary.membership == .join else {
completion(false)
return
}

room.state({ roomState in
guard let powerLevels = roomState?.powerLevels else {
MXLog.warning("[MXSpace] canAddRoom: space power levels not found")
completion(false)
return
}
let userPowerLevel = powerLevels.powerLevelOfUser(withUserID: userId)
let minimumPowerLevel = powerLevels.events["m.space.child"] as? Int ?? powerLevels.stateDefault
let canAddRoom = userPowerLevel >= minimumPowerLevel

completion(canAddRoom)
})
gileluard marked this conversation as resolved.
Show resolved Hide resolved
}

// MARK: - Private

private func updateChildRooms(from space: MXSpace, with directRoomsPerMember: [String : [String]]) {
Expand Down
1 change: 1 addition & 0 deletions changelog.d/5230.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MXSpace: added canAddRoom() method