@@ -22,10 +22,11 @@ #import "MTXClient.h" #import "MTXRequest.h" #import "MTXFetchRoomListFailedException.h" +#import "MTXJoinRoomFailedException.h" #import "MTXLoginFailedException.h" #import "MTXLogoutFailedException.h" static void validateHomeserver(OFURL *homeserver) @@ -199,13 +200,13 @@ return; } if (statusCode != 200) { block([MTXLogoutFailedException - exceptionWithClient: self - statusCode: statusCode - response: response]); + exceptionWithStatusCode: statusCode + response: response + client: self]); return; } block(nil); }]; @@ -225,13 +226,13 @@ return; } if (statusCode != 200) { block(nil, [MTXFetchRoomListFailedException - exceptionWithClient: self - statusCode: statusCode - response: response]); + exceptionWithStatusCode: statusCode + response: response + client: self]); return; } OFArray *joinedRooms = response[@"joined_rooms"]; if (![joinedRooms isKindOfClass: OFArray.class]) { @@ -246,9 +247,44 @@ } } block(response[@"joined_rooms"], nil); }]; + + objc_autoreleasePoolPop(pool); +} + +- (void)joinRoom: (OFString *)room + block: (mtx_client_room_join_block_t)block +{ + void *pool = objc_autoreleasePoolPush(); + MTXRequest *request = [self requestWithPath: + [OFString stringWithFormat: @"/_matrix/client/r0/join/%@", room]]; + request.method = OF_HTTP_REQUEST_METHOD_POST; + [request performWithBlock: ^ (mtx_response_t response, int statusCode, + id exception) { + if (exception != nil) { + block(nil, exception); + return; + } + + if (statusCode != 200) { + block(nil, [MTXJoinRoomFailedException + exceptionWithRoom: room + statusCode: statusCode + response: response + client: self]); + return; + } + + OFString *roomID = response[@"room_id"]; + if (![roomID isKindOfClass: OFString.class]) { + block(nil, [OFInvalidServerReplyException exception]); + return; + } + + block(roomID, nil); + }]; objc_autoreleasePoolPop(pool); } @end