ObjMatrix  Diff

Differences From Artifact [ec0c7e8ba9]:

To Artifact [90a5b4b816]:


44
45
46
47
48
49
50




51
52
53
54
55
56
57

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

@implementation MTXClient




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







>
>
>
>







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

	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
			 storage: (id <MTXStorage>)storage
{
	return [[[self alloc] initWithUserID: userID
154
155
156
157
158
159
160

161
162
163
164
165
166
167
		validateHomeserver(homeserver);

		_userID = [userID copy];
		_deviceID = [deviceID copy];
		_accessToken = [accessToken copy];
		_homeserver = [homeserver copy];
		_storage = [storage retain];

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

	return self;
}







>







158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		validateHomeserver(homeserver);

		_userID = [userID copy];
		_deviceID = [deviceID copy];
		_accessToken = [accessToken copy];
		_homeserver = [homeserver copy];
		_storage = [storage retain];
		_syncTimeout = 300;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
192
193
194
195
196
197
198
199



200

201
202
203
204
205
206
207
208
209
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
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
- (MTXRequest *)requestWithPath: (OFString *)path
{
	return [MTXRequest requestWithPath: path
			       accessToken: _accessToken
				homeserver: _homeserver];
}

- (void)syncWithTimeout: (of_time_interval_t)timeout



		  block: (mtx_client_response_block_t)block

{
	void *pool = objc_autoreleasePoolPush();
	MTXRequest *request = [self
	    requestWithPath: @"/_matrix/client/r0/sync"];
	unsigned long long timeoutMs = timeout * 1000;
	OFMutableDictionary<OFString *, OFString *> *query =
	    [OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue
					       forKey: @"timeout"];
	query[@"since"] = [_storage nextBatchForDeviceID: _deviceID];
	request.query = query;
	[request performWithBlock: ^ (mtx_response_t response, int statusCode,
				       id exception) {
		if (exception != nil) {

			block(exception);
			return;
		}

		if (statusCode != 200) {

			block([MTXSyncFailedException
			    exceptionWithStatusCode: statusCode
					   response: response
					     client: self]);
			return;
		}

		OFString *nextBatch = response[@"next_batch"];
		if (![nextBatch isKindOfClass: OFString.class]) {


			block([OFInvalidServerReplyException exception]);
			return;
		}

		@try {
			[_storage transactionWithBlock: ^ {
				[_storage setNextBatch: nextBatch
					   forDeviceID: _deviceID];

				[self processRoomsSync: response[@"rooms"]];
				[self processPresenceSync:
				    response[@"presence"]];
				[self processAccountDataSync:
				    response[@"account_data"]];
				[self processToDeviceSync:
				    response[@"to_device"]];

				return true;
			}];
		} @catch (id e) {

			block(e);
			return;
		}

		block(nil);

	}];

	objc_autoreleasePoolPop(pool);
}






- (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;







|
>
>
>
|
>
|



|








>
|




>
|
|
|
|





>
>
|



















>
|



|
>




>
>
>
>
>







197
198
199
200
201
202
203
204
205
206
207
208
209
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
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
- (MTXRequest *)requestWithPath: (OFString *)path
{
	return [MTXRequest requestWithPath: path
			       accessToken: _accessToken
				homeserver: _homeserver];
}

- (void)startSyncLoop
{
	if (_syncing)
		return;

	_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;
	[request performWithBlock: ^ (mtx_response_t response, int statusCode,
				       id exception) {
		if (exception != nil) {
			if (_syncExceptionHandler != NULL)
				_syncExceptionHandler(exception);
			return;
		}

		if (statusCode != 200) {
			if (_syncExceptionHandler != NULL)
				_syncExceptionHandler([MTXSyncFailedException
				    exceptionWithStatusCode: statusCode
						   response: response
						     client: self]);
			return;
		}

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

		@try {
			[_storage transactionWithBlock: ^ {
				[_storage setNextBatch: nextBatch
					   forDeviceID: _deviceID];

				[self processRoomsSync: response[@"rooms"]];
				[self processPresenceSync:
				    response[@"presence"]];
				[self processAccountDataSync:
				    response[@"account_data"]];
				[self processToDeviceSync:
				    response[@"to_device"]];

				return true;
			}];
		} @catch (id e) {
			if (_syncExceptionHandler != NULL)
				_syncExceptionHandler(e);
			return;
		}

		if (_syncing)
			[self startSyncLoop];
	}];

	objc_autoreleasePoolPop(pool);
}

- (void)stopSyncLoop
{
	_syncing = false;
}

- (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;