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

Join Room: Support via parameters to better handle federation #690

Merged
merged 6 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changes in Matrix iOS SDK in 0.12.6 (2019-05-)
Improvements:
* MXHTTPClient: support multiple SSL pinning modes (none/public key/certificate)
* MXHTTPClient: Enable the certificate pinning mode by default as soon as some certificates are present in the application bundle.
* Join Room: Support via parameters to better handle federation (vector-im/riot-ios/issues/2547).
* MXEvent: Create a MXEventUnsignedData model for `MXEvent.unsignedData`.
* MXEvent: Add relatesTo property.
* Aggregations: Create MXSession.MXAggregations to manage Matrix aggregations API.
Expand All @@ -19,6 +20,8 @@ Bug Fix:

API break:
* MXEvent: unsignedData is now of type MXEventUnsignedData.
* MXRestClient: Remove the joinRoom method with least parameters.
* MXSession, MXRestClient: Add viaServers parameters to all joinRoom methods.

Changes in Matrix iOS SDK in 0.12.5 (2019-05-03)
===============================================
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/Contrib/Swift/Data/MXRoom.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2017 Avery Pierce
Copyright 2019 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.
Expand Down
10 changes: 4 additions & 6 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
Copyright 2017 Avery Pierce
Copyright 2017 Vector Creations Ltd
Copyright 2019 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.
Expand Down Expand Up @@ -851,18 +852,15 @@ public extension MXRestClient {

- parameters:
- roomIdOrAlias: The id or an alias of the room to join.
- viaServers The server names to try and join through in addition to those that are automatically chosen.
- thirdPartySigned: The signed data obtained by the validation of the 3PID invitation, if 3PID validation is used. The validation is made by `self.signUrl()`.
- completion: A block object called when the operation completes.
- response: Provides the room id on success.

- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func joinRoom(_ roomIdOrAlias: String, withThirdPartySigned dictionary: [String: Any]? = nil, completion: @escaping (_ response: MXResponse<String>) -> Void) -> MXHTTPOperation {
if let dictionary = dictionary {
return __joinRoom(roomIdOrAlias, withThirdPartySigned: dictionary, success: currySuccess(completion), failure: curryFailure(completion))
} else {
return __joinRoom(roomIdOrAlias, success: currySuccess(completion), failure: curryFailure(completion))
}
@nonobjc @discardableResult func joinRoom(_ roomIdOrAlias: String, viaServers: [String]? = nil, withThirdPartySigned dictionary: [String: Any]? = nil, completion: @escaping (_ response: MXResponse<String>) -> Void) -> MXHTTPOperation {
return __joinRoom(roomIdOrAlias, viaServers: viaServers, withThirdPartySigned: dictionary, success: currySuccess(completion), failure: curryFailure(completion))
}

/**
Expand Down
8 changes: 5 additions & 3 deletions MatrixSDK/Contrib/Swift/MXSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by Avery Pierce on 2/11/17.
// Copyright © 2017 matrix.org. All rights reserved.
// Copyright 2019 The Matrix.org Foundation C.I.C
//

import Foundation
Expand Down Expand Up @@ -189,17 +190,18 @@ public extension MXSession {

- parameters:
- roomIdOrAlias: The id or an alias of the room to join.
- viaServers The server names to try and join through in addition to those that are automatically chosen.
- signUrl: the url provided in an invitation.
- completion: A block object called when the operation completes.
- response: Provides the room on success.

- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func joinRoom(_ roomIdOrAlias: String, withSignUrl signUrl: URL? = nil, completion: @escaping (_ response: MXResponse<MXRoom>) -> Void) -> MXHTTPOperation {
@nonobjc @discardableResult func joinRoom(_ roomIdOrAlias: String, viaServers: [String]? = nil, withSignUrl signUrl: URL? = nil, completion: @escaping (_ response: MXResponse<MXRoom>) -> Void) -> MXHTTPOperation {
if let signUrl = signUrl {
return __joinRoom(roomIdOrAlias, withSignUrl: signUrl.absoluteString, success: currySuccess(completion), failure: curryFailure(completion))
return __joinRoom(roomIdOrAlias, viaServers: viaServers, withSignUrl: signUrl.absoluteString, success: currySuccess(completion), failure: curryFailure(completion))
} else {
return __joinRoom(roomIdOrAlias, success: currySuccess(completion), failure: curryFailure(completion))
return __joinRoom(roomIdOrAlias, viaServers: viaServers, success: currySuccess(completion), failure: curryFailure(completion))
}
}

Expand Down
3 changes: 2 additions & 1 deletion MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2014 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 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.
Expand Down Expand Up @@ -632,7 +633,7 @@ FOUNDATION_EXPORT NSString *const kMXRoomDidFlushDataNotification;

/**
Join this room where the user has been invited.

@param success A block object called when the operation is complete.
@param failure A block object called when the operation fails.

Expand Down
5 changes: 4 additions & 1 deletion MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2014 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 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.
Expand Down Expand Up @@ -1624,7 +1625,9 @@ - (MXHTTPOperation*)directoryVisibility:(void (^)(MXRoomDirectoryVisibility dire
- (MXHTTPOperation*)join:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
return [mxSession joinRoom:self.roomId success:^(MXRoom *room) {
// On an invite, there is no need of via parameters.
// The user homeserver already knows other homeservers
return [mxSession joinRoom:self.roomId viaServers:nil success:^(MXRoom *room) {
success();
} failure:failure];
}
Expand Down
20 changes: 5 additions & 15 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -993,27 +993,17 @@ FOUNDATION_EXPORT NSString *const kMXMembersOfRoomParametersNotMembership;
Join a room.

@param roomIdOrAlias the id or an alias of the room to join.
@param viaServers The server names to try and join through in addition to those
that are automatically chosen. Can be nil.
@param thirdPartySigned the signed data obtained by the validation of an 3PID invitation.
The valisation is made by [self signUrl]. Can be nil.
@param success A block object called when the operation succeeds. It provides the room id.
@param failure A block object called when the operation fails.

@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
success:(void (^)(NSString *theRoomId))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

/**
Join a room where the user has been invited by a 3PID invitation.

@param roomIdOrAlias the id or an alias of the room to join.
@param thirdPartySigned the signed data obtained by the validation of the 3PID invitation.
The valisation is made by [self signUrl].
@param success A block object called when the operation succeeds. It provides the room id.
@param failure A block object called when the operation fails.

@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
viaServers:(NSArray<NSString*>*)viaServers
withThirdPartySigned:(NSDictionary*)thirdPartySigned
success:(void (^)(NSString *theRoomId))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;
Expand Down
26 changes: 24 additions & 2 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1835,11 +1835,12 @@ - (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
success:(void (^)(NSString *theRoomId))success
failure:(void (^)(NSError *error))failure
{
return [self joinRoom:roomIdOrAlias withThirdPartySigned:nil success:success failure:failure];
return [self joinRoom:roomIdOrAlias viaServers:nil withThirdPartySigned:nil success:success failure:failure];
}

- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
withThirdPartySigned:(NSDictionary*)thirdPartySigned
viaServers:(NSArray<NSString*>*)viaServers
withThirdPartySigned:(NSDictionary*)thirdPartySigned
success:(void (^)(NSString *theRoomId))success
failure:(void (^)(NSError *error))failure
{
Expand All @@ -1856,6 +1857,27 @@ - (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
apiPathPrefix,
[MXTools encodeURIComponent:roomIdOrAlias]];

// Add all servers as query parameters
if (viaServers.count)
{
NSMutableString *queryParameters;
for (NSString *viaServer in viaServers)
{
NSString *value = [MXTools encodeURIComponent:viaServer];

if (!queryParameters)
{
queryParameters = [NSMutableString stringWithFormat:@"?server_name=%@", value];
}
else
{
[queryParameters appendFormat:@"&server_name=%@", value];
}
}

path = [path stringByAppendingString:queryParameters];
}

MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
path:path
Expand Down
7 changes: 7 additions & 0 deletions MatrixSDK/MXSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2014 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 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.
Expand Down Expand Up @@ -736,20 +737,25 @@ typedef void (^MXOnBackgroundSyncFail)(NSError *error);
Join a room.

@param roomIdOrAlias the id or an alias of the room to join.
@param viaServers The server names to try and join through in addition to those
that are automatically chosen. Can be nil.
@param success A block object called when the operation succeeds. It provides the MXRoom
instance of the joined room.
@param failure A block object called when the operation fails.

@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
viaServers:(NSArray<NSString*>*)viaServers
success:(void (^)(MXRoom *room))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

/**
Join a room where the user has been invited by a 3PID invitation.

@param roomIdOrAlias the id or an alias of the room to join.
@param viaServers The server names to try and join through in addition to those
that are automatically chosen. Can be nil.
@param signUrl the url provided in the invitation.
@param success A block object called when the operation succeeds. It provides the MXRoom
instance of the joined room.
Expand All @@ -758,6 +764,7 @@ typedef void (^MXOnBackgroundSyncFail)(NSError *error);
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
viaServers:(NSArray<NSString*>*)viaServers
withSignUrl:(NSString*)signUrl
success:(void (^)(MXRoom *room))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;
Expand Down
7 changes: 5 additions & 2 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright 2014 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 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.
Expand Down Expand Up @@ -1834,17 +1835,19 @@ - (void)onJoinedRoom:(NSString*)roomId success:(void (^)(MXRoom *room))success
}

- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
viaServers:(NSArray<NSString*>*)viaServers
success:(void (^)(MXRoom *room))success
failure:(void (^)(NSError *error))failure
{
return [matrixRestClient joinRoom:roomIdOrAlias success:^(NSString *theRoomId) {
return [matrixRestClient joinRoom:roomIdOrAlias viaServers:viaServers withThirdPartySigned:nil success:^(NSString *theRoomId) {

[self onJoinedRoom:theRoomId success:success];

} failure:failure];
}

- (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
viaServers:(NSArray<NSString*>*)viaServers
withSignUrl:(NSString*)signUrl
success:(void (^)(MXRoom *room))success
failure:(void (^)(NSError *error))failure
Expand All @@ -1855,7 +1858,7 @@ - (MXHTTPOperation*)joinRoom:(NSString*)roomIdOrAlias
httpOperation = [matrixRestClient signUrl:signUrl success:^(NSDictionary *thirdPartySigned) {
MXStrongifyAndReturnIfNil(self);

MXHTTPOperation *httpOperation2 = [self->matrixRestClient joinRoom:roomIdOrAlias withThirdPartySigned:thirdPartySigned success:^(NSString *theRoomId) {
MXHTTPOperation *httpOperation2 = [self->matrixRestClient joinRoom:roomIdOrAlias viaServers:viaServers withThirdPartySigned:thirdPartySigned success:^(NSString *theRoomId) {

[self onJoinedRoom:theRoomId success:success];

Expand Down
11 changes: 11 additions & 0 deletions MatrixSDK/Utils/MXTools.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
Copyright 2014 OpenMarket Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 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.
Expand Down Expand Up @@ -124,6 +125,16 @@ FOUNDATION_EXPORT NSString *const kMXToolsRegexStringForMatrixGroupIdentifier;
*/
+ (BOOL)isMatrixGroupIdentifier:(NSString *)inputString;

/**
Extract the server name part of a matrix identifier.

For example, this methods returns "matrix.org" from "!anEvent:matrix.org"

@param identifier a Matrix identifier.
@return the server name part.
*/
+ (NSString*)serverNameInMatrixIdentifier:(NSString *)identifier;


#pragma mark - Strings encoding
/**
Expand Down
10 changes: 9 additions & 1 deletion MatrixSDK/Utils/MXTools.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
Copyright 2014 OpenMarket Ltd
Copyright 2018 New Vector Ltd

Copyright 2019 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
Expand Down Expand Up @@ -372,6 +373,13 @@ + (BOOL)isMatrixGroupIdentifier:(NSString *)inputString
return NO;
}

+ (NSString*)serverNameInMatrixIdentifier:(NSString *)identifier
{
// This converts something:example.org into a server domain
// by splitting on colons and ignoring the first entry ("something").
return [identifier componentsSeparatedByString:@":"].lastObject;
}


#pragma mark - Strings encoding
+ (NSString *)encodeURIComponent:(NSString *)string
Expand Down
12 changes: 6 additions & 6 deletions MatrixSDKTests/MXCryptoTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ - (void)testInvitedMemberInACryptedRoom

}];

[bobSession joinRoom:roomId success:nil failure:^(NSError *error) {
[bobSession joinRoom:roomId viaServers:nil success:nil failure:^(NSError *error) {
NSAssert(NO, @"Cannot join a room - error: %@", error);
[expectation fulfill];
}];
Expand Down Expand Up @@ -1637,7 +1637,7 @@ - (void)testInvitedMemberInACryptedRoom2

}];

[bobSession joinRoom:roomId success:^(MXRoom *room) {
[bobSession joinRoom:roomId viaServers:nil success:^(MXRoom *room) {
// Send a second message to Bob who just joins the room
[roomFromAlicePOV sendTextMessage:message2FromAlice success:nil failure:^(NSError *error) {
XCTFail(@"Cannot set up intial test conditions - error: %@", error);
Expand Down Expand Up @@ -1892,7 +1892,7 @@ - (void)testLeftAndJoinedBob

[roomFromAlicePOV enableEncryptionWithAlgorithm:kMXCryptoMegolmAlgorithm success:^{

[bobSession joinRoom:roomFromAlicePOV.roomId success:^(MXRoom *roomFromBobPOV) {
[bobSession joinRoom:roomFromAlicePOV.roomId viaServers:nil success:^(MXRoom *roomFromBobPOV) {

[roomFromBobPOV liveTimeline:^(MXEventTimeline *liveTimeline) {
[liveTimeline listenToEventsOfTypes:@[kMXEventTypeStringRoomMessage] onEvent:^(MXEvent *event, MXTimelineDirection direction, MXRoomState *roomState) {
Expand All @@ -1913,7 +1913,7 @@ - (void)testLeftAndJoinedBob

[MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession = NO;

[bobSession2 joinRoom:roomFromAlicePOV.roomId success:^(MXRoom *roomFromBobPOV2) {
[bobSession2 joinRoom:roomFromAlicePOV.roomId viaServers:nil success:^(MXRoom *roomFromBobPOV2) {

// Bob should be able to receive the message from Alice
[roomFromBobPOV2 liveTimeline:^(MXEventTimeline *liveTimeline) {
Expand Down Expand Up @@ -2009,7 +2009,7 @@ - (void)testLeftBobAndAliceWithNewDevice

observer = [[NSNotificationCenter defaultCenter] addObserverForName:kMXSessionNewRoomNotification object:bobSession queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {

[bobSession joinRoom:note.userInfo[kMXSessionNotificationRoomIdKey] success:^(MXRoom *room) {
[bobSession joinRoom:note.userInfo[kMXSessionNotificationRoomIdKey] viaServers:nil success:^(MXRoom *room) {

// - Bob has an out of date list of Alice's devices
MXRoom *roomFromBobPOV = [bobSession roomWithRoomId:newRoomId];
Expand Down Expand Up @@ -2081,7 +2081,7 @@ - (void)testEnableEncryptionAfterNonCryptedMessages

[aliceSession createRoom:nil visibility:kMXRoomDirectoryVisibilityPublic roomAlias:nil topic:nil success:^(MXRoom *roomFromAlicePOV) {

[bobSession joinRoom:roomFromAlicePOV.roomId success:^(MXRoom *room) {
[bobSession joinRoom:roomFromAlicePOV.roomId viaServers:nil success:^(MXRoom *room) {

[roomFromAlicePOV sendTextMessage:messageFromAlice success:^(NSString *eventId) {

Expand Down
Loading