ObjMatrix  Check-in [17e299f073]

Overview
Comment:Initial support for sync

Only sends the sync, does not do anything with the response yet. Handling the response will be implemented in the next several commits, piece by piece.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 17e299f073f39e55dfe0b8a4bd1b34c8529400138d147df5a7229336df6ba04f
User & Date: js on 2020-10-03 21:56:23
Other Links: manifest | tags
Context
2020-10-03
22:32
Store next batch check-in: 6ab44895eb user: js tags: trunk
21:56
Initial support for sync check-in: 17e299f073 user: js tags: trunk
19:47
Add support for storage check-in: 3c84d235e5 user: js tags: trunk
Changes

Modified src/MTXClient.h from [2463af8d3c] to [a9c991a2fb].

138
139
140
141
142
143
144








145
146
147
148
149
150
151
- (instancetype)initWithUserID: (OFString *)userID
		      deviceID: (OFString *)deviceID
		   accessToken: (OFString *)accessToken
		    homeserver: (OFURL *)homeserver
		       storage: (id <MTXStorage>)storage
    OF_DESIGNATED_INITIALIZER;









/**
 * @brief Logs out the device and invalidates the access token.
 *
 * @warning The client can no longer be used after this succeeded!
 *
 * @param block A block to call when logging out succeeded or failed
 */







>
>
>
>
>
>
>
>







138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
- (instancetype)initWithUserID: (OFString *)userID
		      deviceID: (OFString *)deviceID
		   accessToken: (OFString *)accessToken
		    homeserver: (OFURL *)homeserver
		       storage: (id <MTXStorage>)storage
    OF_DESIGNATED_INITIALIZER;

/**
 * @brief Performs a sync.
 *
 * @param block A block to call when a sync was performed
 */
- (void)syncWithTimeout: (of_time_interval_t)timeout
		  block: (mtx_client_response_block_t)block;

/**
 * @brief Logs out the device and invalidates the access token.
 *
 * @warning The client can no longer be used after this succeeded!
 *
 * @param block A block to call when logging out succeeded or failed
 */

Modified src/MTXClient.m from [a5246fd62c] to [348fd1db0b].

25
26
27
28
29
30
31

32
33
34
35
36
37
38

#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







>







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

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

static void
validateHomeserver(OFURL *homeserver)
{
	if (![homeserver.scheme isEqual: @"http"] &&
	    ![homeserver.scheme isEqual: @"https"])
		@throw [OFUnsupportedProtocolException
190
191
192
193
194
195
196





























197
198
199
200
201
202
203

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







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







191
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

- (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;
	request.query = [OFString stringWithFormat: @"timeout=%llu", timeoutMs];
	[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;
		}

		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;

Modified src/MTXRequest.h from [c05bb8879a] to [9c39efc51b].

66
67
68
69
70
71
72





73
74
75
76
77
78
79
@property (nonatomic) of_http_request_method_t method;

/**
 * @brief The path of the request.
 */
@property (copy, nonatomic) OFString *path;






/**
 * @brief An optional body to send along with the request.
 *
 * This is a dictionary that gets serialized to JSON when the request is sent.
 */
@property (copy, nullable, nonatomic) OFDictionary<OFString *, id> *body;








>
>
>
>
>







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@property (nonatomic) of_http_request_method_t method;

/**
 * @brief The path of the request.
 */
@property (copy, nonatomic) OFString *path;

/**
 * @brief The query for the request.
 */
@property (copy, nullable, nonatomic) OFString *query;

/**
 * @brief An optional body to send along with the request.
 *
 * This is a dictionary that gets serialized to JSON when the request is sent.
 */
@property (copy, nullable, nonatomic) OFDictionary<OFString *, id> *body;

Modified src/MTXRequest.m from [3b4c617815] to [88ddaafdca].

93
94
95
96
97
98
99

100
101
102
103
104
105
106

	if (_block != nil)
		/* Not the best exception to indicate it's already in-flight. */
		@throw [OFAlreadyConnectedException exception];

	OFMutableURL *requestURL = [[_homeserver mutableCopy] autorelease];
	requestURL.path = _path;


	OFMutableDictionary *headers = [OFMutableDictionary dictionary];
	headers[@"User-Agent"] = @"ObjMatrix";
	if (_accessToken != nil)
		headers[@"Authorization"] =
		    [OFString stringWithFormat: @"Bearer %@", _accessToken];
	if (_body != nil)







>







93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

	if (_block != nil)
		/* Not the best exception to indicate it's already in-flight. */
		@throw [OFAlreadyConnectedException exception];

	OFMutableURL *requestURL = [[_homeserver mutableCopy] autorelease];
	requestURL.path = _path;
	requestURL.query = _query;

	OFMutableDictionary *headers = [OFMutableDictionary dictionary];
	headers[@"User-Agent"] = @"ObjMatrix";
	if (_accessToken != nil)
		headers[@"Authorization"] =
		    [OFString stringWithFormat: @"Bearer %@", _accessToken];
	if (_body != nil)

Modified src/ObjMatrix.h from [45c5ea7f4a] to [0c8f4f9aec].

21
22
23
24
25
26
27

28
29
30
31
32


 */

#import "MTXClient.h"
#import "MTXRequest.h"
#import "MTXSQLite3Storage.h"
#import "MTXStorage.h"


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









>





>
>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 */

#import "MTXClient.h"
#import "MTXRequest.h"
#import "MTXSQLite3Storage.h"
#import "MTXStorage.h"

#import "MTXClientException.h"
#import "MTXFetchRoomListFailedException.h"
#import "MTXJoinRoomFailedException.h"
#import "MTXLeaveRoomFailedException.h"
#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"
#import "MTXSendMessageFailedException.h"
#import "MTXSyncFailedException.h"

Modified src/exceptions/MTXFetchRoomListFailedException.m from [48606353c7] to [ed85cc2a2b].

24
25
26
27
28
29
30
31
32
33
34

#import "MTXClient.h"

@implementation MTXFetchRoomListFailedException
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to fetch room list for %@: %@",
	    self.client.userID, self.response];
}
@end







|
|


24
25
26
27
28
29
30
31
32
33
34

#import "MTXClient.h"

@implementation MTXFetchRoomListFailedException
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to fetch room list for %@ with status code %d: %@",
	    self.client.userID, self.statusCode, self.response];
}
@end

Modified src/exceptions/MTXJoinRoomFailedException.m from [f939691250] to [6d2b4b0f4f].

61
62
63
64
65
66
67
68
69
70
71

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to join room %@ for %@: %@",
	    _room, self.client.userID, self.response];
}
@end







|
|


61
62
63
64
65
66
67
68
69
70
71

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to join room %@ for %@ with status code %d: %@",
	    _room, self.client.userID, self.statusCode, self.response];
}
@end

Modified src/exceptions/MTXLeaveRoomFailedException.m from [4b628c276b] to [1ab07a8b06].

61
62
63
64
65
66
67
68
69
70
71

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to leave room %@ for %@: %@",
	    _roomID, self.client.userID, self.response];
}
@end







|
|


61
62
63
64
65
66
67
68
69
70
71

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to leave room %@ for %@ with status code %d: %@",
	    _roomID, self.client.userID, self.statusCode, self.response];
}
@end

Modified src/exceptions/MTXLoginFailedException.m from [900a59b5fd] to [9df3447195].

62
63
64
65
66
67
68
69
70
71
72

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to log in user %@ on %@: %@",
	    _user, _homeserver, _response];
}
@end







|
|


62
63
64
65
66
67
68
69
70
71
72

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to log in user %@ on %@ with status code %d: %@",
	    _user, _homeserver, _statusCode, _response];
}
@end

Modified src/exceptions/MTXLogoutFailedException.m from [6372c5e6df] to [1b0abaa340].

24
25
26
27
28
29
30
31
32
33
34

#import "MTXClient.h"

@implementation MTXLogoutFailedException
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to log out user %@: %@",
	    self.client.userID, self.response];
}
@end







|
|


24
25
26
27
28
29
30
31
32
33
34

#import "MTXClient.h"

@implementation MTXLogoutFailedException
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to log out user %@ with status code: %@",
	    self.client.userID, self.statusCode, self.response];
}
@end

Modified src/exceptions/MTXSendMessageFailedException.m from [2c6af4070c] to [4d0e5a29bd].

66
67
68
69
70
71
72
73
74
75
76

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to send message to room %@ for %@: %@",
	    _roomID, self.client.userID, self.response];
}
@end







|
|


66
67
68
69
70
71
72
73
74
75
76

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to send message to room %@ for %@ with status code %d: %@",
	    _roomID, self.client.userID, self.statusCode, self.response];
}
@end

Added src/exceptions/MTXSyncFailedException.h version [f96dda2973].

































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copyright (c) 2020, Jonathan Schleifer <js@nil.im>
 *
 * https://fossil.nil.im/objmatrix
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import <ObjFW/ObjFW.h>

#import "MTXClientException.h"

OF_ASSUME_NONNULL_BEGIN

@interface MTXSyncFailedException: MTXClientException
@end

OF_ASSUME_NONNULL_END

Added src/exceptions/MTXSyncFailedException.m version [ddf381e4b2].





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Copyright (c) 2020, Jonathan Schleifer <js@nil.im>
 *
 * https://fossil.nil.im/objmatrix
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import "MTXSyncFailedException.h"

#import "MTXClient.h"

@implementation MTXSyncFailedException
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to sync for user %@ with status code %d: %@",
	    self.client.userID, self.statusCode, self.response];
}
@end

Modified src/exceptions/Makefile from [21b2ee299e] to [3d8ea75409].

1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
include ../../extra.mk

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

SRCS = MTXClientException.m			\
       MTXFetchRoomListFailedException.m	\
       MTXJoinRoomFailedException.m		\
       MTXLeaveRoomFailedException.m		\
       MTXLoginFailedException.m		\
       MTXLogoutFailedException.m		\
       MTXSendMessageFailedException.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
16
17
18
19
include ../../extra.mk

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

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

include ../../buildsys.mk

CPPFLAGS += -I. -I..

Modified tests/tests.m from [9a4477a98b] to [247888a299].

58
59
60
61
62
63
64















65
66
67
68
69
70
71
			of_log(@"Error logging in: %@", exception);
			[OFApplication terminateWithStatus: 1];
		}

		_client = [client retain];
		of_log(@"Logged in client: %@", _client);
















		[self fetchRoomList];
	}];
}

- (void)fetchRoomList
{
	[_client fetchRoomListWithBlock: ^ (OFArray<OFString *> *rooms,







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







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
			of_log(@"Error logging in: %@", exception);
			[OFApplication terminateWithStatus: 1];
		}

		_client = [client retain];
		of_log(@"Logged in client: %@", _client);

		[self sync];
	}];
}

- (void)sync
{
	[_client syncWithTimeout: 5
			   block: ^ (id exception) {
		if (exception != nil) {
			of_log(@"Failed to sync: %@", exception);
			[OFApplication terminateWithStatus: 1];
		}

		of_log(@"Synced");

		[self fetchRoomList];
	}];
}

- (void)fetchRoomList
{
	[_client fetchRoomListWithBlock: ^ (OFArray<OFString *> *rooms,