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: |
17e299f073f39e55dfe0b8a4bd1b34c8 |
User & Date: | js 2020-10-03 21:56:23 |
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 to src/MTXClient.h.
︙ | ︙ | |||
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 */ |
︙ | ︙ |
Changes to src/MTXClient.m.
︙ | ︙ | |||
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; |
︙ | ︙ |
Changes to src/MTXRequest.h.
︙ | ︙ | |||
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; |
︙ | ︙ |
Changes to src/MTXRequest.m.
︙ | ︙ | |||
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) |
︙ | ︙ |
Changes to src/ObjMatrix.h.
︙ | ︙ | |||
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" |
Changes to src/exceptions/MTXFetchRoomListFailedException.m.
︙ | ︙ | |||
24 25 26 27 28 29 30 | #import "MTXClient.h" @implementation MTXFetchRoomListFailedException - (OFString *)description { return [OFString stringWithFormat: | | | | 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 |
Changes to src/exceptions/MTXJoinRoomFailedException.m.
︙ | ︙ | |||
61 62 63 64 65 66 67 | [super dealloc]; } - (OFString *)description { return [OFString stringWithFormat: | | | | 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 |
Changes to src/exceptions/MTXLeaveRoomFailedException.m.
︙ | ︙ | |||
61 62 63 64 65 66 67 | [super dealloc]; } - (OFString *)description { return [OFString stringWithFormat: | | | | 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 |
Changes to src/exceptions/MTXLoginFailedException.m.
︙ | ︙ | |||
62 63 64 65 66 67 68 | [super dealloc]; } - (OFString *)description { return [OFString stringWithFormat: | | | | 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 |
Changes to src/exceptions/MTXLogoutFailedException.m.
︙ | ︙ | |||
24 25 26 27 28 29 30 | #import "MTXClient.h" @implementation MTXLogoutFailedException - (OFString *)description { return [OFString stringWithFormat: | | | | 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 |
Changes to src/exceptions/MTXSendMessageFailedException.m.
︙ | ︙ | |||
66 67 68 69 70 71 72 | [super dealloc]; } - (OFString *)description { return [OFString stringWithFormat: | | | | 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.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 |
Changes to src/exceptions/Makefile.
1 2 3 4 5 6 7 8 9 10 11 | 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 \ | | > | 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.. |
Changes to tests/tests.m.
︙ | ︙ | |||
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, |
︙ | ︙ |