Add support for storage
FossilOrigin-Name: 3c84d235e55a6f4481f3ccfb4007113716902901544f9a1f011723767ecaa1e4
This commit is contained in:
parent
491d648e3a
commit
0893233697
12 changed files with 194 additions and 12 deletions
|
@ -22,6 +22,8 @@
|
|||
|
||||
#import <ObjFW/ObjFW.h>
|
||||
|
||||
#import "MTXStorage.h"
|
||||
|
||||
OF_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class MTXClient;
|
||||
|
@ -85,39 +87,60 @@ typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
|
|||
*/
|
||||
@property (readonly, nonatomic) OFURL *homeserver;
|
||||
|
||||
/**
|
||||
* @brief The storage used by the client.
|
||||
*/
|
||||
@property (readonly, nonatomic) id <MTXStorage> storage;
|
||||
|
||||
/**
|
||||
* @brief Creates a new client with the specified access token on the specified
|
||||
* homeserver.
|
||||
*
|
||||
* @param userID The user ID for the client
|
||||
* @param deviceID The device ID for the client
|
||||
* @param accessToken The access token for the client
|
||||
* @param homeserver The URL of the homeserver
|
||||
* @param storage The storage the client should use
|
||||
* @return An autoreleased MTXClient
|
||||
*/
|
||||
+ (instancetype)clientWithUserID: (OFString *)userID
|
||||
deviceID: (OFString *)deviceID
|
||||
accessToken: (OFString *)accessToken
|
||||
homeserver: (OFURL *)homeserver;
|
||||
homeserver: (OFURL *)homeserver
|
||||
storage: (id <MTXStorage>)storage;
|
||||
|
||||
/**
|
||||
* @brief Logs into the homeserver and creates a new client.
|
||||
*
|
||||
* @param user The user to log into
|
||||
* @param password The password to log in with
|
||||
* @param homeserver The homeserver to log into
|
||||
* @param storage The storage the client should use
|
||||
* @param block A block to call once login succeeded or failed
|
||||
*/
|
||||
+ (void)logInWithUser: (OFString *)user
|
||||
password: (OFString *)password
|
||||
homeserver: (OFURL *)homeserver
|
||||
storage: (id <MTXStorage>)storage
|
||||
block: (mtx_client_login_block_t)block;
|
||||
|
||||
/**
|
||||
* @brief Initializes an already allocated client with the specified access
|
||||
* token on the specified homeserver.
|
||||
*
|
||||
* @param userID The user ID for the client
|
||||
* @param deviceID The device ID for the client
|
||||
* @param accessToken The access token for the client
|
||||
* @param homeserver The URL of the homeserver
|
||||
* @param storage The storage the client should use
|
||||
* @return An initialized MTXClient
|
||||
*/
|
||||
- (instancetype)initWithUserID: (OFString *)userID
|
||||
deviceID: (OFString *)deviceID
|
||||
accessToken: (OFString *)accessToken
|
||||
homeserver: (OFURL *)homeserver OF_DESIGNATED_INITIALIZER;
|
||||
homeserver: (OFURL *)homeserver
|
||||
storage: (id <MTXStorage>)storage
|
||||
OF_DESIGNATED_INITIALIZER;
|
||||
|
||||
/**
|
||||
* @brief Logs out the device and invalidates the access token.
|
||||
|
|
|
@ -51,16 +51,19 @@ validateHomeserver(OFURL *homeserver)
|
|||
deviceID: (OFString *)deviceID
|
||||
accessToken: (OFString *)accessToken
|
||||
homeserver: (OFURL *)homeserver
|
||||
storage: (id <MTXStorage>)storage
|
||||
{
|
||||
return [[[self alloc] initWithUserID: userID
|
||||
deviceID: deviceID
|
||||
accessToken: accessToken
|
||||
homeserver: homeserver] autorelease];
|
||||
homeserver: homeserver
|
||||
storage: storage] autorelease];
|
||||
}
|
||||
|
||||
+ (void)logInWithUser: (OFString *)user
|
||||
password: (OFString *)password
|
||||
homeserver: (OFURL *)homeserver
|
||||
storage: (id <MTXStorage>)storage
|
||||
block: (mtx_client_login_block_t)block
|
||||
{
|
||||
void *pool = objc_autoreleasePoolPush();
|
||||
|
@ -127,11 +130,11 @@ validateHomeserver(OFURL *homeserver)
|
|||
} else
|
||||
realHomeserver = homeserver;
|
||||
|
||||
MTXClient *client = [MTXClient
|
||||
clientWithUserID: userID
|
||||
deviceID: deviceID
|
||||
accessToken: accessToken
|
||||
homeserver: realHomeserver];
|
||||
MTXClient *client = [MTXClient clientWithUserID: userID
|
||||
deviceID: deviceID
|
||||
accessToken: accessToken
|
||||
homeserver: realHomeserver
|
||||
storage: storage];
|
||||
block(client, nil);
|
||||
}];
|
||||
|
||||
|
@ -142,6 +145,7 @@ validateHomeserver(OFURL *homeserver)
|
|||
deviceID: (OFString *)deviceID
|
||||
accessToken: (OFString *)accessToken
|
||||
homeserver: (OFURL *)homeserver
|
||||
storage: (id <MTXStorage>)storage
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
|
@ -152,6 +156,7 @@ validateHomeserver(OFURL *homeserver)
|
|||
_deviceID = [deviceID copy];
|
||||
_accessToken = [accessToken copy];
|
||||
_homeserver = [homeserver copy];
|
||||
_storage = [storage retain];
|
||||
} @catch (id e) {
|
||||
[self release];
|
||||
@throw e;
|
||||
|
@ -166,6 +171,7 @@ validateHomeserver(OFURL *homeserver)
|
|||
[_deviceID release];
|
||||
[_accessToken release];
|
||||
[_homeserver release];
|
||||
[_storage release];
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
|
48
src/MTXSQLite3Storage.h
Normal file
48
src/MTXSQLite3Storage.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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 "MTXStorage.h"
|
||||
|
||||
OF_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* @brief SQLite3-based storage for @ref MTXClient.
|
||||
*/
|
||||
@interface MTXSQLite3Storage: OFObject <MTXStorage>
|
||||
/**
|
||||
* @brief Creates a new SQLite3-based storage for @ref MTXClient.
|
||||
*
|
||||
* @param path The path for the SQLite3 database
|
||||
* @return An autoreleased MTXSQLite3Storage
|
||||
*/
|
||||
+ (instancetype)storageWithPath: (OFString *)path;
|
||||
|
||||
/**
|
||||
* @brief Initializes an already allocated MTXSQLite3Storage.
|
||||
*
|
||||
* @param path The path for the SQLite3 database
|
||||
* @return An initialized MTXSQLite3Storage
|
||||
*/
|
||||
- (instancetype)initWithPath: (OFString *)path OF_DESIGNATED_INITIALIZER;
|
||||
@end
|
||||
|
||||
OF_ASSUME_NONNULL_END
|
57
src/MTXSQLite3Storage.m
Normal file
57
src/MTXSQLite3Storage.m
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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 <ObjSQLite3/ObjSQLite3.h>
|
||||
|
||||
#import "MTXSQLite3Storage.h"
|
||||
|
||||
@implementation MTXSQLite3Storage
|
||||
{
|
||||
SL3Connection *_conn;
|
||||
}
|
||||
|
||||
+ (instancetype)storageWithPath: (OFString *)path
|
||||
{
|
||||
return [[[self alloc] initWithPath: path] autorelease];
|
||||
}
|
||||
|
||||
- (instancetype)initWithPath: (OFString *)path
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
@try {
|
||||
_conn = [[SL3Connection alloc] initWithPath: path];
|
||||
} @catch (id e) {
|
||||
[self release];
|
||||
@throw e;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[_conn release];
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
@end
|
33
src/MTXStorage.h
Normal file
33
src/MTXStorage.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* 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>
|
||||
|
||||
OF_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/**
|
||||
* @brief A protocol for a storage to be used by @ref MTXClient.
|
||||
*/
|
||||
@protocol MTXStorage <OFObject>
|
||||
@end
|
||||
|
||||
OF_ASSUME_NONNULL_END
|
|
@ -8,9 +8,11 @@ FRAMEWORK = ${OBJMATRIX_FRAMEWORK}
|
|||
LIB_MAJOR = ${OBJMATRIX_LIB_MAJOR}
|
||||
LIB_MINOR = ${OBJMATRIX_LIB_MINOR}
|
||||
|
||||
SRCS = MTXClient.m \
|
||||
SRCS = MTXClient.m \
|
||||
MTXSQLite3Storage.m \
|
||||
MTXRequest.m
|
||||
INCLUDES := ${SRCS:.m=.h} \
|
||||
MTXStorage.h \
|
||||
ObjMatrix.h
|
||||
|
||||
OBJS_EXTRA = ${EXCEPTIONS_EXCEPTIONS_A}
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
|
||||
#import "MTXClient.h"
|
||||
#import "MTXRequest.h"
|
||||
#import "MTXSQLite3Storage.h"
|
||||
#import "MTXStorage.h"
|
||||
|
||||
#import "MTXFetchRoomListFailedException.h"
|
||||
#import "MTXJoinRoomFailedException.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue