ObjMatrix  Check-in [b1d8afb546]

Overview
Comment:Add support for joining rooms
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b1d8afb546f8999f2c6aba10ca589d2b31ba5e0df917230e265abb5b11474f65
User & Date: js on 2020-10-03 17:40:47
Other Links: manifest | tags
Context
2020-10-03
17:50
Add support for leaving rooms check-in: 193ebad6ad user: js tags: trunk
17:40
Add support for joining rooms check-in: b1d8afb546 user: js tags: trunk
17:23
Get rid of async prefix check-in: d18fbd29fb user: js tags: trunk
Changes

Modified src/MTXClient.h from [4d54398092] to [dc76466324].

47
48
49
50
51
52
53










54
55
56
57
58
59
60
 *
 * @param rooms An array of joined rooms, or nil on error
 * @param exception An exception if fetching the room list failed
 */
typedef void (^mtx_client_room_list_block_t)(
    OFArray<OFString *> *_Nullable rooms, id _Nullable exception);











/**
 * @brief A class that represents a client.
 */
@interface MTXClient: OFObject
/**
 * @brief The user ID used by the client.
 */







>
>
>
>
>
>
>
>
>
>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 *
 * @param rooms An array of joined rooms, or nil on error
 * @param exception An exception if fetching the room list failed
 */
typedef void (^mtx_client_room_list_block_t)(
    OFArray<OFString *> *_Nullable rooms, id _Nullable exception);

/**
 * @brief A block called when a room was joined.
 *
 * @param roomID The room ID that was joined, or nil on error. This can be used
 *		 to get the room ID if a room alias was joined.
 * @param exception An exception if joining the room failed
 */
typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
    id _Nullable exception);

/**
 * @brief A class that represents a client.
 */
@interface MTXClient: OFObject
/**
 * @brief The user ID used by the client.
 */
120
121
122
123
124
125
126









127
128
129

/**
 * @brief Fetches the list of joined rooms.
 *
 * @param block A block to call with the list of joined room
 */
- (void)fetchRoomListWithBlock: (mtx_client_room_list_block_t)block;









@end

OF_ASSUME_NONNULL_END







>
>
>
>
>
>
>
>
>



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

/**
 * @brief Fetches the list of joined rooms.
 *
 * @param block A block to call with the list of joined room
 */
- (void)fetchRoomListWithBlock: (mtx_client_room_list_block_t)block;

/**
 * @brief Joins the specified room.
 *
 * @param room The room to join. Either a room ID or a room alias.
 * @param block A block to call when the room was joined
 */
- (void)joinRoom: (OFString *)room
	   block: (mtx_client_room_join_block_t)block;
@end

OF_ASSUME_NONNULL_END

Modified src/MTXClient.m from [fbae956347] to [69f2cc536e].

20
21
22
23
24
25
26

27
28
29
30
31
32
33
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import "MTXClient.h"
#import "MTXRequest.h"

#import "MTXFetchRoomListFailedException.h"

#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"

static void
validateHomeserver(OFURL *homeserver)
{
	if (![homeserver.scheme isEqual: @"http"] &&







>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import "MTXClient.h"
#import "MTXRequest.h"

#import "MTXFetchRoomListFailedException.h"
#import "MTXJoinRoomFailedException.h"
#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"

static void
validateHomeserver(OFURL *homeserver)
{
	if (![homeserver.scheme isEqual: @"http"] &&
197
198
199
200
201
202
203
204
205
206

207
208
209
210
211
212
213
		if (exception != nil) {
			block(exception);
			return;
		}

		if (statusCode != 200) {
			block([MTXLogoutFailedException
			    exceptionWithClient: self
				     statusCode: statusCode
				       response: response]);

			return;
		}

		block(nil);
	}];

	objc_autoreleasePoolPop(pool);







<
|
|
>







198
199
200
201
202
203
204

205
206
207
208
209
210
211
212
213
214
		if (exception != nil) {
			block(exception);
			return;
		}

		if (statusCode != 200) {
			block([MTXLogoutFailedException

			    exceptionWithStatusCode: statusCode
					   response: response
					     client: self]);
			return;
		}

		block(nil);
	}];

	objc_autoreleasePoolPop(pool);
223
224
225
226
227
228
229
230
231
232

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253



































254
		if (exception != nil) {
			block(nil, exception);
			return;
		}

		if (statusCode != 200) {
			block(nil, [MTXFetchRoomListFailedException
			    exceptionWithClient: self
				     statusCode: statusCode
				       response: response]);

			return;
		}

		OFArray<OFString *> *joinedRooms = response[@"joined_rooms"];
		if (![joinedRooms isKindOfClass: OFArray.class]) {
			block(nil, [OFInvalidServerReplyException exception]);
			return;
		}
		for (OFString *room in joinedRooms) {
			if (![room isKindOfClass: OFString.class]) {
				block(nil,
				    [OFInvalidServerReplyException exception]);
				return;
			}
		}

		block(response[@"joined_rooms"], nil);
	}];

	objc_autoreleasePoolPop(pool);
}



































@end







<
|
|
>





















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

224
225
226
227
228
229
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
		if (exception != nil) {
			block(nil, exception);
			return;
		}

		if (statusCode != 200) {
			block(nil, [MTXFetchRoomListFailedException

			    exceptionWithStatusCode: statusCode
					   response: response
					     client: self]);
			return;
		}

		OFArray<OFString *> *joinedRooms = response[@"joined_rooms"];
		if (![joinedRooms isKindOfClass: OFArray.class]) {
			block(nil, [OFInvalidServerReplyException exception]);
			return;
		}
		for (OFString *room in joinedRooms) {
			if (![room isKindOfClass: OFString.class]) {
				block(nil,
				    [OFInvalidServerReplyException exception]);
				return;
			}
		}

		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

Modified src/exceptions/MTXClientException.h from [1816edf39c] to [f343668f2c].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41


42
43
44
#import "MTXRequest.h"

OF_ASSUME_NONNULL_BEGIN

@class MTXClient;

@interface MTXClientException: OFException
@property (readonly, nonatomic) MTXClient *client;
@property (readonly, nonatomic) int statusCode;
@property (readonly, nonatomic) mtx_response_t response;

+ (instancetype)exceptionWithClient: (MTXClient *)client
			 statusCode: (int)statusCode
			   response: (mtx_response_t)response;
- (instancetype)initWithClient: (OFString *)user
		    statusCode: (int)statusCode
		      response: (mtx_response_t)response;


@end

OF_ASSUME_NONNULL_END







|
|
|

|
<
|
|
|
|
>
>



25
26
27
28
29
30
31
32
33
34
35
36

37
38
39
40
41
42
43
44
45
#import "MTXRequest.h"

OF_ASSUME_NONNULL_BEGIN

@class MTXClient;

@interface MTXClientException: OFException
@property (readonly, nonatomic) int statusCode;
@property (readonly, nonatomic) mtx_response_t response;
@property (readonly, nonatomic) MTXClient *client;

+ (instancetype)exceptionWithStatusCode: (int)statusCode

			       response: (mtx_response_t)response
				 client: (MTXClient *)client;
- (instancetype)initWithStatusCode: (int)statusCode
			  response: (mtx_response_t)respons
			    client: (MTXClient *)client
    OF_DESIGNATED_INITIALIZER;
@end

OF_ASSUME_NONNULL_END

Modified src/exceptions/MTXClientException.m from [42e31b0030] to [2fdf4e0536].

21
22
23
24
25
26
27
28
29
30

31
32
33
34

35
36
37
38
39

40
41
42
43
44
45
46

47
48
49
50
51
52
53
54
55
56
57
58

59
60
61
62
 */

#import "MTXLogoutFailedException.h"

#import "MTXClient.h"

@implementation MTXClientException
+ (instancetype)exceptionWithClient: (MTXClient *)client
			 statusCode: (int)statusCode
			   response: (mtx_response_t)response

{
	return [[[self alloc] initWithClient: client
				  statusCode: statusCode
				    response: response] autorelease];

}

- (instancetype)initWithClient: (MTXClient *)client
		    statusCode: (int)statusCode
		      response: (mtx_response_t)response

{
	self = [super init];

	@try {
		_client = [client retain];
		_statusCode = statusCode;
		_response = [response copy];

	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_client release];
	[_response release];


	[super dealloc];
}
@end







|
<
|
>

|
<
|
>


<
|
|
>




<


>










<

>




21
22
23
24
25
26
27
28

29
30
31
32

33
34
35
36

37
38
39
40
41
42
43

44
45
46
47
48
49
50
51
52
53
54
55
56

57
58
59
60
61
62
 */

#import "MTXLogoutFailedException.h"

#import "MTXClient.h"

@implementation MTXClientException
+ (instancetype)exceptionWithStatusCode: (int)statusCode

			       response: (mtx_response_t)response
				 client: (MTXClient *)client
{
	return [[[self alloc] initWithStatusCode: statusCode

					response: response
					  client: client] autorelease];
}


- (instancetype)initWithStatusCode: (int)statusCode
			  response: (mtx_response_t)response
			    client: (MTXClient *)client
{
	self = [super init];

	@try {

		_statusCode = statusCode;
		_response = [response copy];
		_client = [client retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{

	[_response release];
	[_client release];

	[super dealloc];
}
@end

Added src/exceptions/MTXJoinRoomFailedException.h version [c24a689f7d].

Added src/exceptions/MTXJoinRoomFailedException.m version [f939691250].

Modified src/exceptions/Makefile from [390f755e9e] to [219a3de686].

1
2
3
4
5
6
7

8
9
10
11
12
13
14
include ../../extra.mk

STATIC_PIC_LIB_NOINST = ${EXCEPTIONS_LIB_A}
STATIC_LIB_NOINST = ${EXCEPTIONS_A}

SRCS = MTXClientException.m			\
       MTXFetchRoomListFailedException.m	\

       MTXLoginFailedException.m		\
       MTXLogoutFailedException.m
INCLUDES = ${SRCS:.m=.h}

include ../../buildsys.mk

CPPFLAGS += -I. -I..







>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include ../../extra.mk

STATIC_PIC_LIB_NOINST = ${EXCEPTIONS_LIB_A}
STATIC_LIB_NOINST = ${EXCEPTIONS_A}

SRCS = MTXClientException.m			\
       MTXFetchRoomListFailedException.m	\
       MTXJoinRoomFailedException.m		\
       MTXLoginFailedException.m		\
       MTXLogoutFailedException.m
INCLUDES = ${SRCS:.m=.h}

include ../../buildsys.mk

CPPFLAGS += -I. -I..

Modified tests/tests.m from [a0b40aeb42] to [8b2bd0de5e].

70
71
72
73
74
75
76















77
78
79
80
81
82
83
		if (exception != nil) {
			of_log(@"Failed to fetch room list: %@", exception);
			[OFApplication terminateWithStatus: 1];
		}

		of_log(@"Fetched room list: %@", rooms);
















		[self logOut];
	}];
}

- (void)logOut
{
	[_client logOutWithBlock: ^ (id exception) {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		if (exception != nil) {
			of_log(@"Failed to fetch room list: %@", exception);
			[OFApplication terminateWithStatus: 1];
		}

		of_log(@"Fetched room list: %@", rooms);

		[self joinRoom];
	}];
}

- (void)joinRoom
{
	[_client joinRoom: @"#test:nil.im"
		    block: ^ (OFString *roomID, id exception) {
		if (exception != nil) {
			of_log(@"Failed to join room: %@", exception);
			[OFApplication terminateWithStatus: 1];
		}

		of_log(@"Joined room %@", roomID);

		[self logOut];
	}];
}

- (void)logOut
{
	[_client logOutWithBlock: ^ (id exception) {