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 all 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
60 changes: 60 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions MatrixSDK/Contrib/Swift/JSONModels/MXEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum MXEventType: Equatable, Hashable {
case keyVerificationCancel
case keyVerificationDone
case taggedEvents
case spaceChild

case custom(String)

Expand Down Expand Up @@ -116,6 +117,7 @@ public enum MXEventType: Equatable, Hashable {
case .keyVerificationCancel: return kMXEventTypeStringKeyVerificationCancel
case .keyVerificationDone: return kMXEventTypeStringKeyVerificationDone
case .taggedEvents: return kMXEventTypeStringTaggedEvents
case .spaceChild: return kMXEventTypeStringSpaceChild

// Swift converts any constant with the suffix "Notification" as the type `Notification.Name`
// The original value can be reached using the `rawValue` property.
Expand Down
11 changes: 11 additions & 0 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1862,4 +1862,15 @@ public extension MXRestClient {
return __deleteDevice(byDeviceId: deviceId, authParams: authParameters, success: currySuccess(completion), failure: curryFailure(completion))
}

// MARK: - Spaces

/// Get the space children of a given space.
/// - Parameters:
/// - spaceId: The room id of the queried space.
/// - parameters: Space children request parameters.
/// - completion: A closure called when the operation completes.
/// - Returns: a `MXHTTPOperation` instance.
@nonobjc @discardableResult func getSpaceChildrenForSpace(withId spaceId: String, parameters: MXSpaceChildrenRequestParameters?, completion: @escaping (_ response: MXResponse<MXSpaceChildrenResponse>) -> Void) -> MXHTTPOperation {
return __getSpaceChildrenForSpace(withId: spaceId, parameters: parameters, success: currySuccess(completion), failure: curryFailure(completion))
}
}
2 changes: 2 additions & 0 deletions MatrixSDK/JSONModels/MXEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef NS_ENUM(NSInteger, MXEventType)
MXEventTypeSecretSend,
MXEventTypeSecretStorageDefaultKey,
MXEventTypeTaggedEvents,
MXEventTypeSpaceChild,

// The event is a custom event. Refer to its `MXEventTypeString` version
MXEventTypeCustom = 1000
Expand Down Expand Up @@ -141,6 +142,7 @@ FOUNDATION_EXPORT NSString *const kMXEventTypeStringCallRejectReplacement;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringSticker;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringRoomTombStone;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringTaggedEvents;
FOUNDATION_EXPORT NSString *const kMXEventTypeStringSpaceChild;

// Interactive key verification
FOUNDATION_EXPORT NSString *const kMXEventTypeStringKeyVerificationRequest;
Expand Down
3 changes: 3 additions & 0 deletions MatrixSDK/JSONModels/MXEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
NSString *const kMXEventTypeStringSecretSend = @"m.secret.send";
NSString *const kMXEventTypeStringSecretStorageDefaultKey = @"m.secret_storage.default_key";
NSString *const kMXEventTypeStringTaggedEvents = @"m.tagged_events";
//NSString *const kMXEventTypeStringSpaceChild = @"m.space.child";
// Use temporary event type until the MSC approval
NSString *const kMXEventTypeStringSpaceChild = @"org.matrix.msc1772.space.child";

NSString *const kMXMessageTypeText = @"m.text";
NSString *const kMXMessageTypeEmote = @"m.emote";
Expand Down
15 changes: 15 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
#import "MXPusher.h"
#import "MXRoomCreationParameters.h"
#import "MXTurnServerResponse.h"
#import "MXSpaceChildrenResponse.h"

@class MXThirdpartyProtocolsResponse;
@class MXThirdPartyUsersResponse;
@class MXSpaceChildrenRequestParameters;

#pragma mark - Constants definitions
/**
Expand Down Expand Up @@ -2680,4 +2682,17 @@ typedef MXHTTPOperation* (^MXRestClientIdentityServerAccessTokenHandler)(void (^
success:(void (^)(MXAggregationPaginatedResponse *paginatedResponse))success
failure:(void (^)(NSError *error))failure;

#pragma mark - Spaces

/// Get the space children of a given space.
/// @param spaceId The room id of the queried space.
/// @param parameters Space children request parameters.
/// @param success A block object called when the operation succeeds. It provides a `MXSpaceChildrenResponse` object.
/// @param failure A block object called when the operation fails.
/// @return a MXHTTPOperation instance.
- (MXHTTPOperation*)getSpaceChildrenForSpaceWithId:(NSString*)spaceId
parameters:(MXSpaceChildrenRequestParameters*)parameters
success:(void (^)(MXSpaceChildrenResponse *spaceChildrenResponse))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

@end
35 changes: 35 additions & 0 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#import "MXThirdpartyProtocolsResponse.h"
#import "MXThirdPartyUsersResponse.h"

#import "MatrixSDKSwiftHeader.h"

#pragma mark - Constants definitions
/**
Prefix used in path of home server API requests.
Expand Down Expand Up @@ -5332,4 +5334,37 @@ - (MXHTTPOperation*)relationsForEvent:(NSString*)eventId
}];
}

#pragma mark - Spaces

- (MXHTTPOperation*)getSpaceChildrenForSpaceWithId:(NSString*)spaceId
parameters:(MXSpaceChildrenRequestParameters*)parameters
success:(void (^)(MXSpaceChildrenResponse *spaceChildrenResponse))success
failure:(void (^)(NSError *error))failure
{
NSString *path = [NSString stringWithFormat:@"%@/org.matrix.msc2946/rooms/%@/spaces",
kMXAPIPrefixPathUnstable, spaceId];

MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
path:path
parameters:[parameters jsonDictionary] ?: @{}
success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);

if (success)
{
__block MXSpaceChildrenResponse *spaceChildrenResponse;
[self dispatchProcessing:^{
MXJSONModelSetMXJSONModel(spaceChildrenResponse, MXSpaceChildrenResponse, JSONResponse);
} andCompletion:^{
success(spaceChildrenResponse);
}];
}
}
failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);
[self dispatchFailure:error inBlock:failure];
}];
}

@end
2 changes: 2 additions & 0 deletions MatrixSDK/MatrixSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ FOUNDATION_EXPORT NSString *MatrixSDKVersion;
#import "MXAesKeyData.h"
#import "MXRawDataKey.h"

#import "MXSpaceChildContent.h"

// Bridging to Swift
#import "MXCryptoStore.h"
#import "MXRealmCryptoStore.h"
Expand Down
102 changes: 101 additions & 1 deletion MatrixSDK/Space/MXSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@

import Foundation

/// MXSpace operation error
public enum MXSpaceError: Int, Error {
case homeserverNameNotFound
case unknown
}

extension MXSpaceError: CustomNSError {
public static let errorDomain = "org.matrix.sdk.space"

public var errorCode: Int {
return Int(rawValue)
}

public var errorUserInfo: [String: Any] {
return [:]
}
}

/// A Matrix space enables to collect rooms together into groups. Such collections of rooms are referred as "spaces" (see https:/matrix-org/matrix-doc/blob/matthew/msc1772/proposals/1772-groups-as-rooms.md).
public class MXSpace: NSObject {

Expand All @@ -24,15 +42,97 @@ public class MXSpace: NSObject {
/// The underlying room
public let room: MXRoom

/// Shortcut to the room roomId
public var spaceId: String {
return self.room.roomId
}

/// Shortcut to the room summary
public var summary: MXRoomSummary? {
return self.room.summary
}

// MARK: - Setup

init(room: MXRoom) {
public init(room: MXRoom) {
self.room = room
super.init()
}

// MARK: - Public

/// Add child space or child room to the current space.
/// - Parameters:
/// - 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(roomId: String,
viaServers: [String]? = nil,
order: String? = nil,
autoJoin: Bool = false,
suggested: Bool = false,
completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation? {

let finalViaServers: [String]

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]
}

let spaceChild = MXSpaceChildContent()
spaceChild.via = finalViaServers
spaceChild.order = order
spaceChild.autoJoin = autoJoin
spaceChild.suggested = suggested

guard let stateEventContent = spaceChild.jsonDictionary() as? [String: Any] else {
fatalError("[MXSpace] MXSpaceChildContent dictionary cannot be nil")
}

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

// MARK: - Objective-C
extension MXSpace {

/// Add child space or child room to the current space.
/// - Parameters:
/// - 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(roomId: String,
viaServers: [String]?,
order: String?,
autoJoin: Bool,
suggested: Bool,
success: @escaping (String?) -> Void,
failure: @escaping (Error) -> Void) -> MXHTTPOperation? {
return self.addChild(roomId: roomId, viaServers: viaServers, order: order, autoJoin: autoJoin, suggested: suggested) { (response) in
uncurryResponse(response, success: success, failure: failure)
}
}
}
47 changes: 47 additions & 0 deletions MatrixSDK/Space/MXSpaceChildContent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright 2021 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>
#import "MXJSONModel.h"

NS_ASSUME_NONNULL_BEGIN

/// MXSpaceChildContent represents the state event content of space child event type (MXEventType.spaceChild).
@interface MXSpaceChildContent : MXJSONModel

/// Key which gives a list of candidate servers that can be used to join the room
/// Children where via is not present are ignored.
@property (nonatomic, strong, nullable) NSArray<NSString*>* via;

/// The order key is a string which is used to provide a default ordering of siblings in the room list.
/// (Rooms are sorted based on a lexicographic ordering of order values; rooms with no order come last.
/// orders which are not strings, or do not consist solely of ascii characters in the range \x20 (space) to \x7F (~),
/// or consist of more than 50 characters, are forbidden and should be ignored if received.)
@property (nonatomic, strong, nullable) NSString *order;

/// The auto_join flag on a child listing allows a space admin to list the sub-spaces and rooms in that space which should be automatically joined by members of that space.
/// (This is not a force-join, which are descoped for a future MSC; the user can subsequently part these room if they desire.)
/// `NO` by default.
@property (nonatomic) BOOL autoJoin;

/// If `suggested` is set to `true`, that 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.
/// This is should be ignored if `auto_join` is set to `true`.
/// `NO` by default.
@property (nonatomic) BOOL suggested;

@end

NS_ASSUME_NONNULL_END
Loading