24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#import "MTXRequest.h"
#import "MTXFetchRoomListFailedException.h"
#import "MTXJoinRoomFailedException.h"
#import "MTXLeaveRoomFailedException.h"
#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"
static void
validateHomeserver(OFURL *homeserver)
{
if (![homeserver.scheme isEqual: @"http"] &&
![homeserver.scheme isEqual: @"https"])
@throw [OFUnsupportedProtocolException
|
>
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#import "MTXRequest.h"
#import "MTXFetchRoomListFailedException.h"
#import "MTXJoinRoomFailedException.h"
#import "MTXLeaveRoomFailedException.h"
#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"
#import "MTXSendMessageFailedException.h"
static void
validateHomeserver(OFURL *homeserver)
{
if (![homeserver.scheme isEqual: @"http"] &&
![homeserver.scheme isEqual: @"https"])
@throw [OFUnsupportedProtocolException
|
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
- (MTXRequest *)requestWithPath: (OFString *)path
{
return [MTXRequest requestWithPath: path
accessToken: _accessToken
homeserver: _homeserver];
}
- (void)logOutWithBlock: (mtx_client_logout_block_t)block
{
void *pool = objc_autoreleasePoolPush();
MTXRequest *request =
[self requestWithPath: @"/_matrix/client/r0/logout"];
request.method = OF_HTTP_REQUEST_METHOD_POST;
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
id exception) {
|
|
|
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
- (MTXRequest *)requestWithPath: (OFString *)path
{
return [MTXRequest requestWithPath: path
accessToken: _accessToken
homeserver: _homeserver];
}
- (void)logOutWithBlock: (mtx_client_response_block_t)block
{
void *pool = objc_autoreleasePoolPush();
MTXRequest *request =
[self requestWithPath: @"/_matrix/client/r0/logout"];
request.method = OF_HTTP_REQUEST_METHOD_POST;
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
id exception) {
|
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
block(roomID, nil);
}];
objc_autoreleasePoolPop(pool);
}
- (void)leaveRoom: (OFString *)roomID
block: (mtx_client_room_leave_block_t)block
{
void *pool = objc_autoreleasePoolPush();
MTXRequest *request = [self requestWithPath: [OFString
stringWithFormat: @"/_matrix/client/r0/rooms/%@/leave", roomID]];
request.method = OF_HTTP_REQUEST_METHOD_POST;
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
id exception) {
if (exception != nil) {
block(exception);
return;
}
if (statusCode != 200) {
block([MTXLeaveRoomFailedException
exceptionWithRoomID: roomID
statusCode: statusCode
response: response
client: self]);
return;
}
block(nil);
}];
objc_autoreleasePoolPop(pool);
}
@end
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
block(roomID, nil);
}];
objc_autoreleasePoolPop(pool);
}
- (void)leaveRoom: (OFString *)roomID
block: (mtx_client_response_block_t)block
{
void *pool = objc_autoreleasePoolPush();
MTXRequest *request = [self requestWithPath: [OFString
stringWithFormat: @"/_matrix/client/r0/rooms/%@/leave", roomID]];
request.method = OF_HTTP_REQUEST_METHOD_POST;
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
id exception) {
if (exception != nil) {
block(exception);
return;
}
if (statusCode != 200) {
block([MTXLeaveRoomFailedException
exceptionWithRoomID: roomID
statusCode: statusCode
response: response
client: self]);
return;
}
block(nil);
}];
objc_autoreleasePoolPop(pool);
}
- (void)sendMessage: (OFString *)message
roomID: (OFString *)roomID
block: (mtx_client_response_block_t)block;
{
void *pool = objc_autoreleasePoolPush();
OFString *path = [OFString stringWithFormat:
@"/_matrix/client/r0/rooms/%@/send/m.room.message", roomID];
MTXRequest *request = [self requestWithPath: path];
request.method = OF_HTTP_REQUEST_METHOD_POST;
request.body = @{
@"msgtype": @"m.text",
@"body": message
};
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
id exception) {
if (exception != nil) {
block(exception);
return;
}
if (statusCode != 200) {
block([MTXSendMessageFailedException
exceptionWithMessage: message
roomID: roomID
statusCode: statusCode
response: response
client: self]);
return;
}
block(nil);
}];
objc_autoreleasePoolPop(pool);
}
@end
|