ObjMatrix  Diff

Differences From Artifact [6d2f252461]:

To Artifact [df900b474a]:


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
63
64
65
66
67
68
69
70

71
72
73
74
75
76
77
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
63
64
65
66
67
68
69

70
71
72
73
74
75
76
77







-
+




-
+

















-
+











-
+







#import "MTXLeaveRoomFailedException.h"
#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"
#import "MTXSendMessageFailedException.h"
#import "MTXSyncFailedException.h"

static void
validateHomeserver(OFURL *homeserver)
validateHomeserver(OFIRI *homeserver)
{
	if (![homeserver.scheme isEqual: @"http"] &&
	    ![homeserver.scheme isEqual: @"https"])
		@throw [OFUnsupportedProtocolException
		    exceptionWithURL: homeserver];
		    exceptionWithIRI: homeserver];

	if (homeserver.path != nil && ![homeserver.path isEqual: @"/"])
		@throw [OFInvalidArgumentException exception];

	if (homeserver.user != nil || homeserver.password != nil ||
	    homeserver.query != nil || homeserver.fragment != nil)
		@throw [OFInvalidArgumentException exception];
}

@implementation MTXClient
{
	bool _syncing;
}

+ (instancetype)clientWithUserID: (OFString *)userID
			deviceID: (OFString *)deviceID
		     accessToken: (OFString *)accessToken
		      homeserver: (OFURL *)homeserver
		      homeserver: (OFIRI *)homeserver
			 storage: (id <MTXStorage>)storage
{
	return [[[self alloc] initWithUserID: userID
				    deviceID: deviceID
				 accessToken: accessToken
				  homeserver: homeserver
				     storage: storage] autorelease];
}

+ (void)logInWithUser: (OFString *)user
	     password: (OFString *)password
	   homeserver: (OFURL *)homeserver
	   homeserver: (OFIRI *)homeserver
	      storage: (id <MTXStorage>)storage
		block: (MTXClientLoginBlock)block
{
	void *pool = objc_autoreleasePoolPush();

	validateHomeserver(homeserver);

108
109
110
111
112
113
114
115


116
117
118
119

120
121
122
123




124
125
126
127
128


129
130

131
132
133
134
135
136
137
108
109
110
111
112
113
114

115
116
117
118
119

120
121



122
123
124
125
126
127
128


129
130
131

132
133
134
135
136
137
138
139







-
+
+



-
+

-
-
-
+
+
+
+



-
-
+
+

-
+








		OFString *userID = response[@"user_id"];
		OFString *deviceID = response[@"device_id"];
		OFString *accessToken = response[@"access_token"];
		if (![userID isKindOfClass: OFString.class] ||
		    ![deviceID isKindOfClass: OFString.class] ||
		    ![accessToken isKindOfClass: OFString.class]) {
			block(nil, [OFInvalidServerReplyException exception]);
			block(nil,
			    [OFInvalidServerResponseException exception]);
			return;
		}

		OFString *baseURL =
		OFString *baseIRI =
		    response[@"well_known"][@"m.homeserver"][@"base_url"];
		if (baseURL != nil &&
		    ![baseURL isKindOfClass: OFString.class]) {
			block(nil, [OFInvalidServerReplyException exception]);
		if (baseIRI != nil &&
		    ![baseIRI isKindOfClass: OFString.class]) {
			block(nil,
			    [OFInvalidServerResponseException exception]);
			return;
		}

		OFURL *realHomeserver;
		if (baseURL != nil) {
		OFIRI *realHomeserver;
		if (baseIRI != nil) {
			@try {
				realHomeserver = [OFURL URLWithString: baseURL];
				realHomeserver = [OFIRI IRIWithString: baseIRI];
			} @catch (id e) {
				block(nil, e);
				return;
			}
		} else
			realHomeserver = homeserver;

145
146
147
148
149
150
151
152

153
154
155
156
157
158
159
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161







-
+








	objc_autoreleasePoolPop(pool);
}

- (instancetype)initWithUserID: (OFString *)userID
		      deviceID: (OFString *)deviceID
		   accessToken: (OFString *)accessToken
		    homeserver: (OFURL *)homeserver
		    homeserver: (OFIRI *)homeserver
		       storage: (id <MTXStorage>)storage
{
	self = [super init];

	@try {
		validateHomeserver(homeserver);

208
209
210
211
212
213
214
215
216
217
218
219














220
221
222
223
224
225
226
210
211
212
213
214
215
216





217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237







-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+








	_syncing = true;

	void *pool = objc_autoreleasePoolPush();
	MTXRequest *request = [self
	    requestWithPath: @"/_matrix/client/r0/sync"];
	unsigned long long timeoutMs = _syncTimeout * 1000;
	OFMutableDictionary<OFString *, OFString *> *query =
	    [OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue
					       forKey: @"timeout"];
	query[@"since"] = [_storage nextBatchForDeviceID: _deviceID];
	request.query = query;
	OFMutableArray<OFPair <OFString *, OFString *> *> *queryItems =
	    [OFMutableArray array];
	OFString *since = [_storage nextBatchForDeviceID: _deviceID];

	[queryItems addObject:
	    [OFPair pairWithFirstObject: @"timeout"
			   secondObject: @(timeoutMs).stringValue]];

	if (since != nil)
		[queryItems addObject:
		    [OFPair pairWithFirstObject: @"since"
				   secondObject: since]];

	request.queryItems = queryItems;
	[request performWithBlock: ^ (MTXResponse response, int statusCode,
				       id exception) {
		if (exception != nil) {
			if (_syncExceptionHandler != NULL)
				_syncExceptionHandler(exception);
			return;
		}
234
235
236
237
238
239
240
241


242
243
244
245
246
247
248
245
246
247
248
249
250
251

252
253
254
255
256
257
258
259
260







-
+
+







			return;
		}

		OFString *nextBatch = response[@"next_batch"];
		if (![nextBatch isKindOfClass: OFString.class]) {
			if (_syncExceptionHandler != NULL)
				_syncExceptionHandler(
				    [OFInvalidServerReplyException exception]);
				    [OFInvalidServerResponseException
				    exception]);
			return;
		}

		@try {
			[_storage transactionWithBlock: ^ {
				[_storage setNextBatch: nextBatch
					   forDeviceID: _deviceID];
320
321
322
323
324
325
326
327


328
329
330
331
332
333


334
335
336
337
338
339
340
332
333
334
335
336
337
338

339
340
341
342
343
344
345

346
347
348
349
350
351
352
353
354







-
+
+





-
+
+







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

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

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

361
362
363
364
365
366
367
368


369
370
371
372
373
374
375
375
376
377
378
379
380
381

382
383
384
385
386
387
388
389
390







-
+
+







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

		OFString *roomID = response[@"room_id"];
		if (![roomID isKindOfClass: OFString.class]) {
			block(nil, [OFInvalidServerReplyException exception]);
			block(nil,
			    [OFInvalidServerResponseException exception]);
			return;
		}

		block(roomID, nil);
	}];

	objc_autoreleasePoolPop(pool);