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

Spaces: Add support for spaces Summary API #1072

Merged
merged 22 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5893503
MXEvent: Add space child event type.
SBiOSoftWhare Apr 15, 2021
0cd1e85
Add MXSpaceChildContent: The space child state event content.
SBiOSoftWhare Apr 15, 2021
f8499b5
MXSpace: Add possibility to add a child space.
SBiOSoftWhare Apr 15, 2021
d851212
Add MXSpaceChildInfo that represents space child summary informations.
SBiOSoftWhare Apr 15, 2021
dd1b700
MXRestClient: Add support to space summary API.
SBiOSoftWhare Apr 15, 2021
fc61243
MXSpaceChildContent: Fix predicate issue.
SBiOSoftWhare Apr 15, 2021
9e4d190
MXRestClient: Fix space API issue.
SBiOSoftWhare Apr 15, 2021
2198378
MXSpaceService: Add support of space children API request.
SBiOSoftWhare Apr 15, 2021
94bc509
Add MXSpaceChildContent unit tests.
SBiOSoftWhare Apr 15, 2021
088d3b6
MXSpaceServiceTest: Add a test for getSpaceChildren method.
SBiOSoftWhare Apr 15, 2021
428f16e
Update pbxproj
SBiOSoftWhare Apr 15, 2021
352918d
MXSpace: Update addChild interface.
SBiOSoftWhare Apr 19, 2021
5beb2ab
MXSpaceServiceTest: Update MXSpace.addChild calls and add missing XCT…
SBiOSoftWhare Apr 19, 2021
997ed77
MXSpaceChildSummaryResponse: Update modelFromJSON.
SBiOSoftWhare Apr 19, 2021
bd97741
MXSpaceService: Use a dedicated dispatch queue for data processing.
SBiOSoftWhare Apr 20, 2021
32a76f4
MXSpaceService: Improve MXRoomMembersCount building.
SBiOSoftWhare Apr 20, 2021
5c9111e
MXSpaceChildInfo: Add MXRoomType property.
SBiOSoftWhare Apr 20, 2021
4ff8593
MXSpace: Update addChild method to make it clearer the possibility to…
SBiOSoftWhare Apr 20, 2021
374fd22
MXSpaceServiceTest: Add comments and report error if needed when crea…
SBiOSoftWhare Apr 20, 2021
8180db9
MXSpaceChildSummaryResponse: Return nil from modelFromJSON when roomI…
SBiOSoftWhare Apr 21, 2021
ecfc55d
MXSpace: Update addChild method with default values for Swift interface.
SBiOSoftWhare Apr 21, 2021
0bd5431
MXSpaceServiceTest: Update addChild method usage.
SBiOSoftWhare Apr 21, 2021
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
88 changes: 23 additions & 65 deletions MatrixSDK/Space/MXSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,61 +61,39 @@ public class MXSpace: NSObject {

// MARK: - Public

/// Add child space to the current space.
/// Add child space or child room to the current space.
/// - Parameters:
/// - spaceId: The room id of the child space.
/// - roomId: The room id of the child space or child room.
/// - viaServers: List of candidate servers that can be used to join the space. Children where via is not present are ignored.
/// If nil value is set current homeserver will be used as via server.
/// - order: Is a string which is used to provide a default ordering of siblings in the room list. Orders should be a string of ascii characters in the range \x20 (space) to \x7F (~), and should be less or equal 50 characters.
/// - autoJoin: Allows a space admin to list the sub-spaces and rooms in that space which should be automatically joined by members of that space.
/// - suggested: Indicates that the child should be advertised to members of the space by the client. This could be done by showing them eagerly in the room list.
/// - completion: A closure called when the operation completes. Provides the event id of the event generated on the home server on success.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func addChild(spaceId: String,
viaServers: [String],
public func addChild(roomId: String,
viaServers: [String]?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if can have nil as default value. We are in full swift here, no?

order: String?,
autoJoin: Bool,
suggested: Bool,
completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation? {

let spaceChild = MXSpaceChildContent()
spaceChild.via = viaServers
spaceChild.order = order
spaceChild.autoJoin = autoJoin
spaceChild.suggested = suggested
let finalViaServers: [String]

guard let stateEventContent = spaceChild.jsonDictionary() as? [String: Any] else {
fatalError("[MXSpace] MXSpaceChildContent dictionary cannot be nil")
if let viaServers = viaServers {
finalViaServers = viaServers
} else {
// If viaServers is nil use the current homeserver as via server
guard let homeserverName = self.room.mxSession.credentials.homeServerName() else {
completion(.failure(MXSpaceError.homeserverNameNotFound))
return nil
}
finalViaServers = [homeserverName]
}

return self.room.sendStateEvent(.spaceChild,
content: stateEventContent,
stateKey: spaceId,
completion: completion)
}

/// Add child space to the current space using current homeserver as via server.
/// - Parameters:
/// - spaceId: The room id of the child space.
/// - order: Is a string which is used to provide a default ordering of siblings in the room list. Orders should be a string of ascii characters in the range \x20 (space) to \x7F (~), and should be less or equal 50 characters.
/// - autoJoin: Allows a space admin to list the sub-spaces and rooms in that space which should be automatically joined by members of that space.
/// - suggested: Indicates that the child should be advertised to members of the space by the client. This could be done by showing them eagerly in the room list.
/// - completion: A closure called when the operation completes. Provides the event id of the event generated on the home server on success.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func addChildWithDefaultViaServer(spaceId: String,
order: String?,
autoJoin: Bool,
suggested: Bool,
completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation? {

guard let homeserverName = self.room.mxSession.credentials.homeServerName() else {
completion(.failure(MXSpaceError.homeserverNameNotFound))
return nil
}


let spaceChild = MXSpaceChildContent()
spaceChild.via = [homeserverName]
spaceChild.via = finalViaServers
spaceChild.order = order
spaceChild.autoJoin = autoJoin
spaceChild.suggested = suggested
Expand All @@ -126,54 +104,34 @@ public class MXSpace: NSObject {

return self.room.sendStateEvent(.spaceChild,
content: stateEventContent,
stateKey: spaceId,
stateKey: roomId,
completion: completion)
}
}

// MARK: - Objective-C
extension MXSpace {

/// Add child space to the current space.
/// Add child space or child room to the current space.
/// - Parameters:
/// - spaceId: The room id of the child space.
/// - roomId: The room id of the child space or child room.
/// - viaServers: List of candidate servers that can be used to join the space. Children where via is not present are ignored.
/// If nil value is set current homeserver will be used as via server.
/// - order: Is a string which is used to provide a default ordering of siblings in the room list. Orders should be a string of ascii characters in the range \x20 (space) to \x7F (~), and should be less or equal 50 characters.
/// - autoJoin: Allows a space admin to list the sub-spaces and rooms in that space which should be automatically joined by members of that space.
/// - suggested: Indicates that the child should be advertised to members of the space by the client. This could be done by showing them eagerly in the room list.
/// - success: A closure called when the operation is complete. Provides the event id of the event generated on the home server on success.
/// - failure: A closure called when the operation fails.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func addChild(spaceId: String,
public func addChild(roomId: String,
viaServers: [String],
order: String?,
autoJoin: Bool,
suggested: Bool,
success: @escaping (String?) -> Void,
failure: @escaping (Error) -> Void) -> MXHTTPOperation? {
return self.addChild(spaceId: spaceId, viaServers: viaServers, order: order, autoJoin: autoJoin, suggested: suggested) { (response) in
uncurryResponse(response, success: success, failure: failure)
}
}

/// Add child space to the current space using current homeserver as via server.
/// - Parameters:
/// - spaceId: The room id of the child space.
/// - order: Is a string which is used to provide a default ordering of siblings in the room list. Orders should be a string of ascii characters in the range \x20 (space) to \x7F (~), and should be less or equal 50 characters.
/// - autoJoin: Allows a space admin to list the sub-spaces and rooms in that space which should be automatically joined by members of that space.
/// - suggested: Indicates that the child should be advertised to members of the space by the client. This could be done by showing them eagerly in the room list.
/// - success: A closure called when the operation is complete. Provides the event id of the event generated on the home server on success.
/// - failure: A closure called when the operation fails.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func addChildWithDefaultViaServer(spaceId: String,
order: String?,
autoJoin: Bool,
suggested: Bool,
success: @escaping (String?) -> Void,
failure: @escaping (Error) -> Void) -> MXHTTPOperation? {
return self.addChildWithDefaultViaServer(spaceId: spaceId, order: order, autoJoin: autoJoin, suggested: suggested) { (response) in
return self.addChild(roomId: roomId, viaServers: viaServers, order: order, autoJoin: autoJoin, suggested: suggested) { (response) in
uncurryResponse(response, success: success, failure: failure)
}
}
Expand Down
11 changes: 8 additions & 3 deletions MatrixSDK/Space/MXSpaceChildInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public class MXSpaceChildInfo: NSObject {
/// True to indicate that the space is known.
/// We might not know this child at all, i.e we just know it exists but no info on type/name/etc..
public let isKnown: Bool

/// The room type string value as provided by the server. Can be nil.
public let roomType: String?
public let roomTypeString: String?

/// The locally computed room type derivated from `roomTypeString`.
public let roomType: MXRoomType

/// The space name.
public let name: String?
Expand Down Expand Up @@ -61,7 +64,8 @@ public class MXSpaceChildInfo: NSObject {

public init(childRoomId: String,
isKnown: Bool,
roomType: String?,
roomTypeString: String?,
roomType: MXRoomType,
name: String?,
topic: String?,
avatarUrl: String?,
Expand All @@ -72,6 +76,7 @@ public class MXSpaceChildInfo: NSObject {
parentRoomId: String?) {
self.childRoomId = childRoomId
self.isKnown = isKnown
self.roomTypeString = roomTypeString
self.roomType = roomType
self.name = name
self.topic = topic
Expand Down
11 changes: 8 additions & 3 deletions MatrixSDK/Space/MXSpaceChildSummaryResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

@implementation MXSpaceChildSummaryResponse

+ (id)modelFromJSON:(NSDictionary *)JSONDictionary
+ (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary
{
MXSpaceChildSummaryResponse *spaceChildSummaryResponse = [MXSpaceChildSummaryResponse new];

NSString *roomId;

if (spaceChildSummaryResponse)
MXJSONModelSetString(roomId, JSONDictionary[@"room_id"]);

if (spaceChildSummaryResponse && roomId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return nil if there is no roomId else we can have a non valid MXSpaceChildSummaryResponse instance at runtime.

{
MXJSONModelSetString(spaceChildSummaryResponse.roomId, JSONDictionary[@"room_id"]);
spaceChildSummaryResponse.roomId = roomId;

MXJSONModelSetString(spaceChildSummaryResponse.roomType, JSONDictionary[@"room_type"]);
MXJSONModelSetString(spaceChildSummaryResponse.name, JSONDictionary[@"name"]);
MXJSONModelSetString(spaceChildSummaryResponse.topic, JSONDictionary[@"topic"]);
Expand Down
65 changes: 45 additions & 20 deletions MatrixSDK/Space/MXSpaceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ public class MXSpaceService: NSObject {

private let roomTypeMapper: MXRoomTypeMapper

private let processingQueue: DispatchQueue
private let completionQueue: DispatchQueue

// MARK: - Setup

public init(session: MXSession) {
self.session = session
self.roomTypeMapper = MXRoomTypeMapper(defaultRoomType: .room)
self.processingQueue = DispatchQueue(label: "org.matrix.sdk.MXSpaceService.processingQueue", attributes: .concurrent)
self.completionQueue = DispatchQueue.main
}

// MARK: - Public
Expand Down Expand Up @@ -122,29 +127,41 @@ public class MXSpaceService: NSObject {
return self.session.matrixRestClient.getSpaceChildrenForSpace(withId: spaceId, parameters: parameters) { (response) in
switch response {
case .success(let spaceChildrenResponse):
guard let rooms = spaceChildrenResponse.rooms else {
// We should have at least one room for the requested space
completion(.failure(MXSpaceServiceError.spaceNotFound))
return
}
self.processingQueue.async { [weak self] in
guard let self = self else {
return
}

guard let rooms = spaceChildrenResponse.rooms else {
// We should have at least one room for the requested space
self.completionQueue.async {
completion(.failure(MXSpaceServiceError.spaceNotFound))
}
return
}

guard let rootSpaceChildSummaryResponse = rooms.first(where: { spaceResponse -> Bool in
return spaceResponse.roomId == spaceId
}) else {
// Fail to find root child. We should have at least one room for the requested space
completion(.failure(MXSpaceServiceError.spaceNotFound))
return
}
guard let rootSpaceChildSummaryResponse = rooms.first(where: { spaceResponse -> Bool in
return spaceResponse.roomId == spaceId
}) else {
// Fail to find root child. We should have at least one room for the requested space
self.completionQueue.async {
completion(.failure(MXSpaceServiceError.spaceNotFound))
}
return
}

// Build the queried space summary
let spaceSummary = self.createRoomSummary(with: rootSpaceChildSummaryResponse)
// Build the queried space summary
let spaceSummary = self.createRoomSummary(with: rootSpaceChildSummaryResponse)

// Build the child summaries of the queried space
let childInfos = self.spaceChildInfos(from: spaceChildrenResponse, excludedSpaceId: spaceId)
// Build the child summaries of the queried space
let childInfos = self.spaceChildInfos(from: spaceChildrenResponse, excludedSpaceId: spaceId)

let spaceChildrenSummary = MXSpaceChildrenSummary(spaceSummary: spaceSummary, childInfos: childInfos)
let spaceChildrenSummary = MXSpaceChildrenSummary(spaceSummary: spaceSummary, childInfos: childInfos)

completion(.success(spaceChildrenSummary))
self.completionQueue.async {
completion(.success(spaceChildrenSummary))
}
}
case .failure(let error):
completion(.failure(error))
}
Expand All @@ -162,8 +179,12 @@ public class MXSpaceService: NSObject {
roomSummary.roomTypeString = roomTypeString
roomSummary.roomType = self.roomTypeMapper.roomType(from: roomTypeString)

let joinedMembersCount = UInt(spaceChildSummaryResponse.numJoinedMembers)

let membersCount = MXRoomMembersCount()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about MXRoomMembersCount.members and MXRoomMembersCount.invited?
We should have members >= joined.

membersCount.joined = UInt(spaceChildSummaryResponse.numJoinedMembers)
membersCount.joined = joinedMembersCount
membersCount.members = joinedMembersCount

roomSummary.membersCount = membersCount
roomSummary.displayname = spaceChildSummaryResponse.name
roomSummary.topic = spaceChildSummaryResponse.topic
Expand Down Expand Up @@ -204,9 +225,13 @@ public class MXSpaceService: NSObject {
spaceChildContent = MXSpaceChildContent(fromJSON: stateEventContent)
}

let roomTypeString = spaceChildSummaryResponse.roomType
let roomType = self.roomTypeMapper.roomType(from: roomTypeString)

return MXSpaceChildInfo(childRoomId: spaceChildSummaryResponse.roomId,
isKnown: true,
roomType: spaceChildSummaryResponse.roomType,
roomTypeString: roomTypeString,
roomType: roomType,
name: spaceChildSummaryResponse.name,
topic: spaceChildSummaryResponse.topic,
avatarUrl: spaceChildSummaryResponse.avatarUrl,
Expand Down
Loading