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.

FossilOrigin-Name: 17e299f073f39e55dfe0b8a4bd1b34c8529400138d147df5a7229336df6ba04f
This commit is contained in:
Jonathan Schleifer 2020-10-03 21:56:23 +00:00
parent 0893233697
commit f651e6194c
15 changed files with 142 additions and 13 deletions

View file

@ -142,6 +142,14 @@ typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
storage: (id <MTXStorage>)storage storage: (id <MTXStorage>)storage
OF_DESIGNATED_INITIALIZER; 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. * @brief Logs out the device and invalidates the access token.
* *

View file

@ -29,6 +29,7 @@
#import "MTXLoginFailedException.h" #import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h" #import "MTXLogoutFailedException.h"
#import "MTXSendMessageFailedException.h" #import "MTXSendMessageFailedException.h"
#import "MTXSyncFailedException.h"
static void static void
validateHomeserver(OFURL *homeserver) validateHomeserver(OFURL *homeserver)
@ -195,6 +196,35 @@ validateHomeserver(OFURL *homeserver)
homeserver: _homeserver]; 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)logOutWithBlock: (mtx_client_response_block_t)block
{ {
void *pool = objc_autoreleasePoolPush(); void *pool = objc_autoreleasePoolPush();

View file

@ -70,6 +70,11 @@ typedef void (^mtx_request_block_t)(mtx_response_t _Nullable response,
*/ */
@property (copy, nonatomic) OFString *path; @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. * @brief An optional body to send along with the request.
* *

View file

@ -97,6 +97,7 @@
OFMutableURL *requestURL = [[_homeserver mutableCopy] autorelease]; OFMutableURL *requestURL = [[_homeserver mutableCopy] autorelease];
requestURL.path = _path; requestURL.path = _path;
requestURL.query = _query;
OFMutableDictionary *headers = [OFMutableDictionary dictionary]; OFMutableDictionary *headers = [OFMutableDictionary dictionary];
headers[@"User-Agent"] = @"ObjMatrix"; headers[@"User-Agent"] = @"ObjMatrix";

View file

@ -25,8 +25,11 @@
#import "MTXSQLite3Storage.h" #import "MTXSQLite3Storage.h"
#import "MTXStorage.h" #import "MTXStorage.h"
#import "MTXClientException.h"
#import "MTXFetchRoomListFailedException.h" #import "MTXFetchRoomListFailedException.h"
#import "MTXJoinRoomFailedException.h" #import "MTXJoinRoomFailedException.h"
#import "MTXLeaveRoomFailedException.h" #import "MTXLeaveRoomFailedException.h"
#import "MTXLoginFailedException.h" #import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h" #import "MTXLogoutFailedException.h"
#import "MTXSendMessageFailedException.h"
#import "MTXSyncFailedException.h"

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,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

View file

@ -0,0 +1,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

View file

@ -9,7 +9,8 @@ SRCS = MTXClientException.m \
MTXLeaveRoomFailedException.m \ MTXLeaveRoomFailedException.m \
MTXLoginFailedException.m \ MTXLoginFailedException.m \
MTXLogoutFailedException.m \ MTXLogoutFailedException.m \
MTXSendMessageFailedException.m MTXSendMessageFailedException.m \
MTXSyncFailedException.m
INCLUDES = ${SRCS:.m=.h} INCLUDES = ${SRCS:.m=.h}
include ../../buildsys.mk include ../../buildsys.mk

View file

@ -62,6 +62,21 @@ OF_APPLICATION_DELEGATE(Tests)
_client = [client retain]; _client = [client retain];
of_log(@"Logged in client: %@", _client); 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]; [self fetchRoomList];
}]; }];
} }